blob: 87cfb5bf5866faa7729f916fc9bfcb8de8461ae8 [file] [log] [blame]
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01001//
Jim Flynn83d08a92020-07-09 13:48:16 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01003// SPDX-License-Identifier: MIT
4//
5
Jim Flynn64063552020-02-14 10:18:08 +00006#include "ProfilingMocks.hpp"
Finn Williamsa0de0562020-04-22 12:27:37 +01007#include "ProfilingTestUtils.hpp"
Ferran Balaguer1b941722019-08-28 16:57:18 +01008#include "SendCounterPacketTests.hpp"
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01009
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010010#include <BufferManager.hpp>
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010011#include <CounterDirectory.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010012#include <common/include/EncodeVersion.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010013#include <ProfilingUtils.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014#include <SendCounterPacket.hpp>
Rob Hughesbdee4262020-01-07 17:05:24 +000015#include <Processes.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010016
Ferran Balaguer73882172019-09-02 16:39:42 +010017#include <armnn/Exceptions.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010018#include <armnn/Conversion.hpp>
Rob Hughes122f3252020-01-09 12:46:21 +000019#include <armnn/Utils.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010020
Finn Williamse09fc822020-04-29 13:17:30 +010021#include <common/include/Constants.hpp>
22
23
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010024#include <boost/test/unit_test.hpp>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010025#include <boost/numeric/conversion/cast.hpp>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010026
Francis Murtagh3a161982019-09-04 15:25:02 +010027#include <chrono>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010028
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010029using namespace armnn::profiling;
30
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010031namespace
32{
33
Colm Donelan2ba48d22019-11-29 09:10:59 +000034// A short delay to wait for the thread to process a packet.
Finn Williams09ad6f92019-12-19 17:05:18 +000035uint16_t constexpr WAIT_UNTIL_READABLE_MS = 20;
Colm Donelan2ba48d22019-11-29 09:10:59 +000036
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010037void SetNotConnectedProfilingState(ProfilingStateMachine& profilingStateMachine)
38{
39 ProfilingState currentState = profilingStateMachine.GetCurrentState();
40 switch (currentState)
41 {
42 case ProfilingState::WaitingForAck:
43 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000044 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010045 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000046 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010047 case ProfilingState::Active:
48 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000049 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010050 case ProfilingState::NotConnected:
51 return;
52 default:
53 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
54 }
55}
56
57void SetWaitingForAckProfilingState(ProfilingStateMachine& profilingStateMachine)
58{
59 ProfilingState currentState = profilingStateMachine.GetCurrentState();
60 switch (currentState)
61 {
62 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000063 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010064 case ProfilingState::Active:
65 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000066 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010067 case ProfilingState::NotConnected:
68 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000069 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010070 case ProfilingState::WaitingForAck:
71 return;
72 default:
73 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
74 }
75}
76
77void SetActiveProfilingState(ProfilingStateMachine& profilingStateMachine)
78{
79 ProfilingState currentState = profilingStateMachine.GetCurrentState();
80 switch (currentState)
81 {
82 case ProfilingState::Uninitialised:
83 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000084 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010085 case ProfilingState::NotConnected:
86 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000087 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010088 case ProfilingState::WaitingForAck:
89 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000090 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010091 case ProfilingState::Active:
92 return;
93 default:
94 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
95 }
96}
97
98} // Anonymous namespace
99
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100100BOOST_AUTO_TEST_SUITE(SendCounterPacketTests)
101
Finn Williams09ad6f92019-12-19 17:05:18 +0000102using PacketType = MockProfilingConnection::PacketType;
103
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100104BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest)
105{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100106 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100107 MockSendCounterPacket mockSendCounterPacket(mockBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100108
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100109 mockSendCounterPacket.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100110
111 auto packetBuffer = mockBuffer.GetReadableBuffer();
112 const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100113
114 BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
115
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100116 mockBuffer.MarkRead(packetBuffer);
117
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100118 CounterDirectory counterDirectory;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100119 mockSendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100120
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100121 packetBuffer = mockBuffer.GetReadableBuffer();
122 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
123
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100124 BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
125
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100126 mockBuffer.MarkRead(packetBuffer);
127
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100128 uint64_t timestamp = 0;
Finn Williams032bc742020-02-12 11:02:34 +0000129 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100130
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100131 mockSendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100132
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100133 packetBuffer = mockBuffer.GetReadableBuffer();
134 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
135
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100136 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
137
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100138 mockBuffer.MarkRead(packetBuffer);
139
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100140 uint32_t capturePeriod = 0;
141 std::vector<uint16_t> selectedCounterIds;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100142 mockSendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100143
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100144 packetBuffer = mockBuffer.GetReadableBuffer();
145 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
146
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100147 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100148
149 mockBuffer.MarkRead(packetBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100150}
151
Ferran Balaguer73882172019-09-02 16:39:42 +0100152BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest)
153{
154 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100155 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000156 SendCounterPacket sendPacket1(mockBuffer1);
Ferran Balaguer73882172019-09-02 16:39:42 +0100157
158 uint32_t capturePeriod = 1000;
159 std::vector<uint16_t> selectedCounterIds;
160 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds),
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100161 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100162
163 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100164 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000165 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer73882172019-09-02 16:39:42 +0100166
167 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100168 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100169
170 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
171 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
172 uint32_t period = ReadUint32(readBuffer2, 8);
173
174 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
175 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
176 BOOST_TEST(headerWord1 == 4); // data lenght
177 BOOST_TEST(period == 1000); // capture period
178
179 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100180 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000181 SendCounterPacket sendPacket3(mockBuffer3);
Ferran Balaguer73882172019-09-02 16:39:42 +0100182
183 selectedCounterIds.reserve(5);
184 selectedCounterIds.emplace_back(100);
185 selectedCounterIds.emplace_back(200);
186 selectedCounterIds.emplace_back(300);
187 selectedCounterIds.emplace_back(400);
188 selectedCounterIds.emplace_back(500);
189 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100190 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100191
192 headerWord0 = ReadUint32(readBuffer3, 0);
193 headerWord1 = ReadUint32(readBuffer3, 4);
194 period = ReadUint32(readBuffer3, 8);
195
196 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
197 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
198 BOOST_TEST(headerWord1 == 14); // data lenght
199 BOOST_TEST(period == 1000); // capture period
200
201 uint16_t counterId = 0;
202 uint32_t offset = 12;
203
204 // Counter Ids
205 for(const uint16_t& id : selectedCounterIds)
206 {
207 counterId = ReadUint16(readBuffer3, offset);
208 BOOST_TEST(counterId == id);
209 offset += 2;
210 }
211}
212
Francis Murtagh3a161982019-09-04 15:25:02 +0100213BOOST_AUTO_TEST_CASE(SendPeriodicCounterCapturePacketTest)
214{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100215 ProfilingStateMachine profilingStateMachine;
216
Francis Murtagh3a161982019-09-04 15:25:02 +0100217 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100218 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000219 SendCounterPacket sendPacket1(mockBuffer1);
Francis Murtagh3a161982019-09-04 15:25:02 +0100220
221 auto captureTimestamp = std::chrono::steady_clock::now();
222 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
Finn Williams032bc742020-02-12 11:02:34 +0000223 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100224
225 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
226 BufferExhaustion);
227
228 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100229 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000230 SendCounterPacket sendPacket2(mockBuffer2);
Francis Murtagh3a161982019-09-04 15:25:02 +0100231
232 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100233 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100234
235 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
236 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
237 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
238
Jim Flynnfc365622019-12-04 10:07:20 +0000239 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100240 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
241 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
242 BOOST_TEST(headerWord1 == 8); // data length
243 BOOST_TEST(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100244
245 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100246 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000247 SendCounterPacket sendPacket3(mockBuffer3);
Francis Murtagh3a161982019-09-04 15:25:02 +0100248
249 indexValuePairs.reserve(5);
Finn Williams032bc742020-02-12 11:02:34 +0000250 indexValuePairs.emplace_back(CounterValue{0, 100});
251 indexValuePairs.emplace_back(CounterValue{1, 200});
252 indexValuePairs.emplace_back(CounterValue{2, 300});
253 indexValuePairs.emplace_back(CounterValue{3, 400});
254 indexValuePairs.emplace_back(CounterValue{4, 500});
Francis Murtagh3a161982019-09-04 15:25:02 +0100255 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100256 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100257
258 headerWord0 = ReadUint32(readBuffer3, 0);
259 headerWord1 = ReadUint32(readBuffer3, 4);
260 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
261
Jim Flynnfc365622019-12-04 10:07:20 +0000262 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100263 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
264 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
265 BOOST_TEST(headerWord1 == 38); // data length
266 BOOST_TEST(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100267
268 uint16_t counterIndex = 0;
269 uint32_t counterValue = 100;
270 uint32_t offset = 16;
271
272 // Counter Ids
273 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
274 {
275 // Check Counter Index
276 uint16_t readIndex = ReadUint16(readBuffer3, offset);
277 BOOST_TEST(counterIndex == readIndex);
278 counterIndex++;
279 offset += 2;
280
281 // Check Counter Value
282 uint32_t readValue = ReadUint32(readBuffer3, offset);
283 BOOST_TEST(counterValue == readValue);
284 counterValue += 100;
285 offset += 4;
286 }
287
288}
289
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100290BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest)
291{
292 using boost::numeric_cast;
293
294 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
295
296 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100297 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000298 SendCounterPacket sendPacket1(mockBuffer1);
Matteo Martincigh149528e2019-09-05 12:02:04 +0100299 BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100300
301 // Full metadata packet
302
303 std::string processName = GetProcessName().substr(0, 60);
304
Finn Williamsa0de0562020-04-22 12:27:37 +0100305 uint32_t infoSize = numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1;
306 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1;
307 uint32_t softwareVersionSize = numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1;
308 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100309
Jim Flynn83d08a92020-07-09 13:48:16 +0100310 // Supported Packets
311 // Packet Encoding version 1.0.0
312 // Control packet family
313 // Stream metadata packet (packet family=0; packet id=0)
314 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
315 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
316 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
317 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
318 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
319 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
320 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
321 // Counter Packet Family
322 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
323 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
324 // Timeline Packet Family
325 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
326 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
327 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100328 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
329 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
330 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
331 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
332 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
333 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
334 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
335 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
336 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
337 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
338 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
339 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
340 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Jim Flynn83d08a92020-07-09 13:48:16 +0100341
342 uint32_t packetEntries = static_cast<uint32_t>(packetVersions.size());
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100343
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100344 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000345 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100346 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100347 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100348
349 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
350 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
351
352 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
353 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
354
355 uint32_t totalLength = numeric_cast<uint32_t>(2 * sizeUint32 + 10 * sizeUint32 + infoSize + hardwareVersionSize +
356 softwareVersionSize + processNameSize + sizeUint32 +
357 2 * packetEntries * sizeUint32);
358
359 BOOST_TEST(headerWord1 == totalLength - (2 * sizeUint32)); // data length
360
361 uint32_t offset = sizeUint32 * 2;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100362 BOOST_TEST(ReadUint32(readBuffer2, offset) == arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100363 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100364 BOOST_TEST(ReadUint32(readBuffer2, offset) == arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100365 offset += sizeUint32;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100366 BOOST_TEST(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100367 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000368 int pid = armnnUtils::Processes::GetCurrentId();
369 BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100370 offset += sizeUint32;
371 uint32_t poolOffset = 10 * sizeUint32;
Finn Williamsa0de0562020-04-22 12:27:37 +0100372 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100373 offset += sizeUint32;
374 poolOffset += infoSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100375 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100376 offset += sizeUint32;
377 poolOffset += hardwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100378 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100379 offset += sizeUint32;
380 poolOffset += softwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100381 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100382 offset += sizeUint32;
383 poolOffset += processNameSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100384 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100385 offset += sizeUint32;
386 BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved
387
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100388 const unsigned char* readData2 = readBuffer2->GetReadableData();
389
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100390 offset += sizeUint32;
391 if (infoSize)
392 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100393 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100394 offset += infoSize;
395 }
396
397 if (hardwareVersionSize)
398 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100399 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100400 offset += hardwareVersionSize;
401 }
402
403 if (softwareVersionSize)
404 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100405 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100406 offset += softwareVersionSize;
407 }
408
409 if (processNameSize)
410 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100411 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100412 offset += processNameSize;
413 }
414
415 if (packetEntries)
416 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100417 uint32_t numberOfEntries = ReadUint32(readBuffer2, offset);
418 BOOST_TEST((numberOfEntries >> 16) == packetEntries);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100419 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100420 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100421 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100422 uint32_t readPacketId = ReadUint32(readBuffer2, offset);
423 BOOST_TEST(packetVersion.first == readPacketId);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100424 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100425 uint32_t readVersion = ReadUint32(readBuffer2, offset);
426 BOOST_TEST(packetVersion.second == readVersion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100427 offset += sizeUint32;
428 }
429 }
430
431 BOOST_TEST(offset == totalLength);
432}
433
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100434BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest)
435{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100436 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000437 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100438
439 // Create a device for testing
440 uint16_t deviceUid = 27;
441 const std::string deviceName = "some_device";
442 uint16_t deviceCores = 3;
443 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
444
445 // Create a device record
446 SendCounterPacket::DeviceRecord deviceRecord;
447 std::string errorMessage;
448 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
449
450 BOOST_CHECK(result);
451 BOOST_CHECK(errorMessage.empty());
452 BOOST_CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
453
454 uint16_t deviceRecordWord0[]
455 {
456 static_cast<uint16_t>(deviceRecord[0] >> 16),
457 static_cast<uint16_t>(deviceRecord[0])
458 };
459 BOOST_CHECK(deviceRecordWord0[0] == deviceUid); // uid
460 BOOST_CHECK(deviceRecordWord0[1] == deviceCores); // cores
Finn Williams6be1e9b2020-05-15 11:21:54 +0100461 BOOST_CHECK(deviceRecord[1] == 8); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100462 BOOST_CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
463 BOOST_CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
464}
465
466BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest)
467{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100468 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000469 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100470
471 // Create a device for testing
472 uint16_t deviceUid = 27;
473 const std::string deviceName = "some€£invalid‡device";
474 uint16_t deviceCores = 3;
475 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
476
477 // Create a device record
478 SendCounterPacket::DeviceRecord deviceRecord;
479 std::string errorMessage;
480 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
481
482 BOOST_CHECK(!result);
483 BOOST_CHECK(!errorMessage.empty());
484 BOOST_CHECK(deviceRecord.empty());
485}
486
487BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest)
488{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100489 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000490 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100491
492 // Create a counter set for testing
493 uint16_t counterSetUid = 27;
494 const std::string counterSetName = "some_counter_set";
495 uint16_t counterSetCount = 3421;
496 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
497
498 // Create a counter set record
499 SendCounterPacket::CounterSetRecord counterSetRecord;
500 std::string errorMessage;
501 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
502
503 BOOST_CHECK(result);
504 BOOST_CHECK(errorMessage.empty());
505 BOOST_CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
506
507 uint16_t counterSetRecordWord0[]
508 {
509 static_cast<uint16_t>(counterSetRecord[0] >> 16),
510 static_cast<uint16_t>(counterSetRecord[0])
511 };
512 BOOST_CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
513 BOOST_CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
Finn Williams6be1e9b2020-05-15 11:21:54 +0100514 BOOST_CHECK(counterSetRecord[1] == 8); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100515 BOOST_CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
516 BOOST_CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
517}
518
519BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest)
520{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100521 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000522 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100523
524 // Create a counter set for testing
525 uint16_t counterSetUid = 27;
526 const std::string counterSetName = "some invalid_counter€£set";
527 uint16_t counterSetCount = 3421;
528 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
529
530 // Create a counter set record
531 SendCounterPacket::CounterSetRecord counterSetRecord;
532 std::string errorMessage;
533 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
534
535 BOOST_CHECK(!result);
536 BOOST_CHECK(!errorMessage.empty());
537 BOOST_CHECK(counterSetRecord.empty());
538}
539
540BOOST_AUTO_TEST_CASE(CreateEventRecordTest)
541{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100542 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000543 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100544
545 // Create a counter for testing
546 uint16_t counterUid = 7256;
547 uint16_t maxCounterUid = 132;
548 uint16_t deviceUid = 132;
549 uint16_t counterSetUid = 4497;
550 uint16_t counterClass = 1;
551 uint16_t counterInterpolation = 1;
552 double counterMultiplier = 1234.567f;
553 const std::string counterName = "some_valid_counter";
554 const std::string counterDescription = "a_counter_for_testing";
555 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000556 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
557 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100558 maxCounterUid,
559 counterClass,
560 counterInterpolation,
561 counterMultiplier,
562 counterName,
563 counterDescription,
564 counterUnits,
565 deviceUid,
566 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100567 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100568
569 // Create an event record
570 SendCounterPacket::EventRecord eventRecord;
571 std::string errorMessage;
572 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
573
574 BOOST_CHECK(result);
575 BOOST_CHECK(errorMessage.empty());
576 BOOST_CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
577
578 uint16_t eventRecordWord0[]
579 {
580 static_cast<uint16_t>(eventRecord[0] >> 16),
581 static_cast<uint16_t>(eventRecord[0])
582 };
583 uint16_t eventRecordWord1[]
584 {
585 static_cast<uint16_t>(eventRecord[1] >> 16),
586 static_cast<uint16_t>(eventRecord[1])
587 };
588 uint16_t eventRecordWord2[]
589 {
590 static_cast<uint16_t>(eventRecord[2] >> 16),
591 static_cast<uint16_t>(eventRecord[2])
592 };
593 uint32_t eventRecordWord34[]
594 {
595 eventRecord[3],
596 eventRecord[4]
597 };
Finn Williamsd44815f2020-05-01 13:25:55 +0100598
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100599 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
600 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
601 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
Finn Williamsd44815f2020-05-01 13:25:55 +0100602
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100603 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
604 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
605 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
606 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
607
608 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100609 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
610 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100611 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
612 4u + // Counter name length (uint32_t)
613 counterName.size() + // 18u
614 1u + // Null-terminator
615 1u; // Rounding to the next word
Finn Williamsd44815f2020-05-01 13:25:55 +0100616
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100617 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
618 4u + // Counter description length (uint32_t)
619 counterDescription.size() + // 21u
620 1u + // Null-terminator
Finn Williamsd44815f2020-05-01 13:25:55 +0100621 2u; // Rounding to the next word
622
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100623 ARMNN_NO_CONVERSION_WARN_END
624
625 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
626 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
627 BOOST_CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
628
Finn Williamsd44815f2020-05-01 13:25:55 +0100629 // Offsets are relative to the start of the eventRecord
630 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100631 size_t uint32_t_size = sizeof(uint32_t);
632
633 // The length of the SWTrace string (name)
634 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
635 // The counter name
636 BOOST_CHECK(std::memcmp(eventRecordPool +
637 counterNameOffset + // Offset
638 uint32_t_size /* The length of the name */,
639 counterName.data(),
640 counterName.size()) == 0); // name
641 // The null-terminator at the end of the name
642 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
643
644 // The length of the SWTrace string (description)
645 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
646 // The counter description
647 BOOST_CHECK(std::memcmp(eventRecordPool +
648 counterDescriptionOffset + // Offset
649 uint32_t_size /* The length of the description */,
650 counterDescription.data(),
651 counterDescription.size()) == 0); // description
652 // The null-terminator at the end of the description
653 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
654
655 // The length of the SWTrace namestring (units)
656 BOOST_CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
657 // The counter units
658 BOOST_CHECK(std::memcmp(eventRecordPool +
659 counterUnitsOffset + // Offset
660 uint32_t_size /* The length of the units */,
661 counterUnits.data(),
662 counterUnits.size()) == 0); // units
663 // The null-terminator at the end of the units
664 BOOST_CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
665}
666
667BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest)
668{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100669 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000670 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100671
672 // Create a counter for testing
673 uint16_t counterUid = 44312;
674 uint16_t maxCounterUid = 345;
675 uint16_t deviceUid = 101;
676 uint16_t counterSetUid = 34035;
677 uint16_t counterClass = 0;
678 uint16_t counterInterpolation = 1;
679 double counterMultiplier = 4435.0023f;
680 const std::string counterName = "some_valid_counter";
681 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000682 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
683 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100684 maxCounterUid,
685 counterClass,
686 counterInterpolation,
687 counterMultiplier,
688 counterName,
689 counterDescription,
690 "",
691 deviceUid,
692 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100693 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100694
695 // Create an event record
696 SendCounterPacket::EventRecord eventRecord;
697 std::string errorMessage;
698 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
699
700 BOOST_CHECK(result);
701 BOOST_CHECK(errorMessage.empty());
702 BOOST_CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
703
704 uint16_t eventRecordWord0[]
705 {
706 static_cast<uint16_t>(eventRecord[0] >> 16),
707 static_cast<uint16_t>(eventRecord[0])
708 };
709 uint16_t eventRecordWord1[]
710 {
711 static_cast<uint16_t>(eventRecord[1] >> 16),
712 static_cast<uint16_t>(eventRecord[1])
713 };
714 uint16_t eventRecordWord2[]
715 {
716 static_cast<uint16_t>(eventRecord[2] >> 16),
717 static_cast<uint16_t>(eventRecord[2])
718 };
719 uint32_t eventRecordWord34[]
720 {
721 eventRecord[3],
722 eventRecord[4]
723 };
724 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
725 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
726 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
727 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
728 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
729 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
730 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
731
732 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100733 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
734 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100735 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
736 4u + // Counter name length (uint32_t)
737 counterName.size() + // 18u
738 1u + // Null-terminator
739 1u; // Rounding to the next word
740 ARMNN_NO_CONVERSION_WARN_END
741
742 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
743 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
744 BOOST_CHECK(eventRecord[7] == 0); // units_offset
745
Finn Williamsd44815f2020-05-01 13:25:55 +0100746 // Offsets are relative to the start of the eventRecord
747 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100748 size_t uint32_t_size = sizeof(uint32_t);
749
750 // The length of the SWTrace string (name)
751 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
752 // The counter name
753 BOOST_CHECK(std::memcmp(eventRecordPool +
754 counterNameOffset + // Offset
755 uint32_t_size, // The length of the name
756 counterName.data(),
757 counterName.size()) == 0); // name
758 // The null-terminator at the end of the name
759 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
760
761 // The length of the SWTrace string (description)
762 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
763 // The counter description
764 BOOST_CHECK(std::memcmp(eventRecordPool +
765 counterDescriptionOffset + // Offset
766 uint32_t_size, // The length of the description
767 counterDescription.data(),
768 counterDescription.size()) == 0); // description
769 // The null-terminator at the end of the description
770 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
771}
772
773BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1)
774{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100775 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000776 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100777
778 // Create a counter for testing
779 uint16_t counterUid = 7256;
780 uint16_t maxCounterUid = 132;
781 uint16_t deviceUid = 132;
782 uint16_t counterSetUid = 4497;
783 uint16_t counterClass = 1;
784 uint16_t counterInterpolation = 1;
785 double counterMultiplier = 1234.567f;
786 const std::string counterName = "some_invalid_counter £££"; // Invalid name
787 const std::string counterDescription = "a_counter_for_testing";
788 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000789 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
790 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791 maxCounterUid,
792 counterClass,
793 counterInterpolation,
794 counterMultiplier,
795 counterName,
796 counterDescription,
797 counterUnits,
798 deviceUid,
799 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100800 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100801
802 // Create an event record
803 SendCounterPacket::EventRecord eventRecord;
804 std::string errorMessage;
805 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
806
807 BOOST_CHECK(!result);
808 BOOST_CHECK(!errorMessage.empty());
809 BOOST_CHECK(eventRecord.empty());
810}
811
812BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2)
813{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100814 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000815 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100816
817 // Create a counter for testing
818 uint16_t counterUid = 7256;
819 uint16_t maxCounterUid = 132;
820 uint16_t deviceUid = 132;
821 uint16_t counterSetUid = 4497;
822 uint16_t counterClass = 1;
823 uint16_t counterInterpolation = 1;
824 double counterMultiplier = 1234.567f;
825 const std::string counterName = "some_invalid_counter";
826 const std::string counterDescription = "an invalid d€scription"; // Invalid description
827 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000828 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
829 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100830 maxCounterUid,
831 counterClass,
832 counterInterpolation,
833 counterMultiplier,
834 counterName,
835 counterDescription,
836 counterUnits,
837 deviceUid,
838 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100839 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100840
841 // Create an event record
842 SendCounterPacket::EventRecord eventRecord;
843 std::string errorMessage;
844 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
845
846 BOOST_CHECK(!result);
847 BOOST_CHECK(!errorMessage.empty());
848 BOOST_CHECK(eventRecord.empty());
849}
850
851BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3)
852{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100853 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000854 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100855
856 // Create a counter for testing
857 uint16_t counterUid = 7256;
858 uint16_t maxCounterUid = 132;
859 uint16_t deviceUid = 132;
860 uint16_t counterSetUid = 4497;
861 uint16_t counterClass = 1;
862 uint16_t counterInterpolation = 1;
863 double counterMultiplier = 1234.567f;
864 const std::string counterName = "some_invalid_counter";
865 const std::string counterDescription = "a valid description";
866 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000867 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
868 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100869 maxCounterUid,
870 counterClass,
871 counterInterpolation,
872 counterMultiplier,
873 counterName,
874 counterDescription,
875 counterUnits,
876 deviceUid,
877 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100878 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100879
880 // Create an event record
881 SendCounterPacket::EventRecord eventRecord;
882 std::string errorMessage;
883 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
884
885 BOOST_CHECK(!result);
886 BOOST_CHECK(!errorMessage.empty());
887 BOOST_CHECK(eventRecord.empty());
888}
889
890BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest)
891{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100892 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000893 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100894
895 // Create a category for testing
896 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +0000897 const CategoryPtr category = std::make_unique<Category>(categoryName);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100898 ARMNN_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100899 category->m_Counters = { 11u, 23u, 5670u };
900
901 // Create a collection of counters
902 Counters counters;
903 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000904 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100905 0,
Keith Davise394bd92019-12-02 15:12:19 +0000906 11,
907 0,
908 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100909 534.0003f,
910 "counter1",
911 "the first counter",
912 "millipi2",
913 0,
914 0))));
915 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000916 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100917 1,
Keith Davise394bd92019-12-02 15:12:19 +0000918 23,
919 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100920 1,
921 534.0003f,
922 "this is counter 2",
923 "the second counter",
924 "",
925 0,
926 0))));
927 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000928 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
929 2,
930 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100931 0,
932 0,
933 534.0003f,
934 "and this is number 3",
935 "the third counter",
936 "blah_per_second",
937 0,
938 0))));
939 Counter* counter1 = counters.find(11)->second.get();
940 Counter* counter2 = counters.find(23)->second.get();
941 Counter* counter3 = counters.find(5670)->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100942 ARMNN_ASSERT(counter1);
943 ARMNN_ASSERT(counter2);
944 ARMNN_ASSERT(counter3);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100945 uint16_t categoryEventCount = boost::numeric_cast<uint16_t>(counters.size());
946
947 // Create a category record
948 SendCounterPacket::CategoryRecord categoryRecord;
949 std::string errorMessage;
950 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
951
952 BOOST_CHECK(result);
953 BOOST_CHECK(errorMessage.empty());
Sadik Armagan4c998992020-02-25 12:44:44 +0000954 BOOST_CHECK(categoryRecord.size() == 79); // Size in words: header [3] + event pointer table [3] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100955 // category name [5] + event records [68 = 22 + 20 + 26]
956
Sadik Armagan4c998992020-02-25 12:44:44 +0000957 uint16_t categoryRecordWord1[]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100958 {
959 static_cast<uint16_t>(categoryRecord[0] >> 16),
960 static_cast<uint16_t>(categoryRecord[0])
961 };
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100962 BOOST_CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
963 BOOST_CHECK(categoryRecordWord1[1] == 0); // reserved
964
965 size_t uint32_t_size = sizeof(uint32_t);
966
967 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100968 uint32_t categoryRecordBlockSize = 3u * uint32_t_size;
969 uint32_t eventPointerTableOffset = categoryRecordBlockSize; // The event pointer table is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100970 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
971 categoryEventCount * uint32_t_size; // The size of the event pointer table
972 ARMNN_NO_CONVERSION_WARN_END
973
Sadik Armagan4c998992020-02-25 12:44:44 +0000974 BOOST_CHECK(categoryRecord[1] == eventPointerTableOffset); // event_pointer_table_offset
975 BOOST_CHECK(categoryRecord[2] == categoryNameOffset); // name_offset
Finn Williamsd44815f2020-05-01 13:25:55 +0100976 // Offsets are relative to the start of the category record
977 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100978
979 // The event pointer table
980 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
981 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
982 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
983 BOOST_CHECK(eventRecord0Offset == 32);
984 BOOST_CHECK(eventRecord1Offset == 120);
985 BOOST_CHECK(eventRecord2Offset == 200);
986
987 // The length of the SWTrace namestring (name)
988 BOOST_CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
989 // The category name
990 BOOST_CHECK(std::memcmp(categoryRecordPool +
991 categoryNameOffset + // Offset
992 uint32_t_size, // The length of the name
993 categoryName.data(),
994 categoryName.size()) == 0); // name
995 // The null-terminator at the end of the name
996 BOOST_CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
997
998 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
999 // as the event records already have a number of unit tests dedicated to them
1000
1001 // Counter1 UID and max counter UID
1002 uint16_t eventRecord0Word0[2] = { 0u, 0u };
Finn Williamsd44815f2020-05-01 13:25:55 +01001003 std::memcpy(eventRecord0Word0, categoryRecordPool + categoryRecordBlockSize + eventRecord0Offset,
1004 sizeof(eventRecord0Word0));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001005 BOOST_CHECK(eventRecord0Word0[0] == counter1->m_Uid);
1006 BOOST_CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
1007
1008 // Counter1 name
1009 uint32_t counter1NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001010 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001011 BOOST_CHECK(counter1NameOffset == 0);
1012 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001013 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
1014 categoryRecordBlockSize + // Offset to the end of the category record block
1015 8u * uint32_t_size + // Offset to the event record pool
1016 counter1NameOffset // Offset to the name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001017 ] == counter1->m_Name.size() + 1); // The length of the name including the
1018 // null-terminator
1019 // The counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001020 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1021 categoryRecordBlockSize + // Offset to the end of the category record block
1022 eventRecord0Offset + // Offset to the event record
1023 8u * uint32_t_size + // Offset to the event record pool
1024 counter1NameOffset + // Offset to the name of the counter
1025 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001026 counter1->m_Name.data(),
1027 counter1->m_Name.size()) == 0); // name
1028 // The null-terminator at the end of the counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001029 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
1030 categoryRecordBlockSize + // Offset to the end of the category record block
1031 8u * uint32_t_size + // Offset to the event record pool
1032 counter1NameOffset + // Offset to the name of the counter
1033 uint32_t_size + // The length of the name
1034 counter1->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001035 ] == '\0');
1036
1037 // Counter2 name
1038 uint32_t counter2NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001039 std::memcpy(&counter2NameOffset, categoryRecordPool +
1040 categoryRecordBlockSize +
1041 eventRecord1Offset +
1042 5u * uint32_t_size,
1043 uint32_t_size);
1044 BOOST_CHECK(counter2NameOffset == 8u * uint32_t_size );
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001045 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001046
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001047 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001048 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001049 counter2NameOffset // Offset to the name of the counter
1050 ] == counter2->m_Name.size() + 1); // The length of the name including the
1051 // null-terminator
1052 // The counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001053 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1054 categoryRecordBlockSize + // Offset to the end of the category record block
1055 eventRecord1Offset + // Offset to the event record
1056 counter2NameOffset + // Offset to the name of the counter
1057 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001058 counter2->m_Name.data(),
1059 counter2->m_Name.size()) == 0); // name
Finn Williamsd44815f2020-05-01 13:25:55 +01001060
1061
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001062 // The null-terminator at the end of the counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001063 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
1064 categoryRecordBlockSize + // Offset to the end of the category record block
1065 counter2NameOffset + // Offset to the name of the counter
1066 uint32_t_size + // The length of the name
1067 counter2->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001068 ] == '\0');
1069
1070 // Counter3 name
1071 uint32_t counter3NameOffset = 0;
1072 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
1073 BOOST_CHECK(counter3NameOffset == 0);
1074 // The length of the SWTrace string (name)
1075 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001076 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001077 8u * uint32_t_size + // Offset to the event record pool
1078 counter3NameOffset // Offset to the name of the counter
1079 ] == counter3->m_Name.size() + 1); // The length of the name including the
1080 // null-terminator
1081 // The counter3 name
1082 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001083 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001084 eventRecord2Offset + // Offset to the event record
1085 8u * uint32_t_size + // Offset to the event record pool
1086 counter3NameOffset + // Offset to the name of the counter
1087 uint32_t_size, // The length of the name
1088 counter3->m_Name.data(),
1089 counter3->m_Name.size()) == 0); // name
1090 // The null-terminator at the end of the counter3 name
1091 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001092 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001093 8u * uint32_t_size + // Offset to the event record pool
1094 counter3NameOffset + // Offset to the name of the counter
1095 uint32_t_size + // The length of the name
1096 counter3->m_Name.size() // The name of the counter
1097 ] == '\0');
1098}
1099
1100BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1)
1101{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001102 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001103 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001104
1105 // Create a category for testing
1106 const std::string categoryName = "some invalid category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001107 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001108 BOOST_CHECK(category);
1109
1110 // Create a category record
1111 Counters counters;
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(CreateInvalidCategoryRecordTest2)
1122{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001123 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001124 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001125
1126 // Create a category for testing
1127 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001128 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001129 BOOST_CHECK(category);
1130 category->m_Counters = { 11u, 23u, 5670u };
1131
1132 // Create a collection of counters
1133 Counters counters;
1134 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001135 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1136 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001137 1234,
1138 0,
1139 1,
1140 534.0003f,
1141 "count€r1", // Invalid name
1142 "the first counter",
1143 "millipi2",
1144 0,
1145 0))));
1146
1147 Counter* counter1 = counters.find(11)->second.get();
1148 BOOST_CHECK(counter1);
1149
1150 // Create a category record
1151 SendCounterPacket::CategoryRecord categoryRecord;
1152 std::string errorMessage;
1153 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1154
1155 BOOST_CHECK(!result);
1156 BOOST_CHECK(!errorMessage.empty());
1157 BOOST_CHECK(categoryRecord.empty());
1158}
1159
1160BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest1)
1161{
1162 // The counter directory used for testing
1163 CounterDirectory counterDirectory;
1164
1165 // Register a device
1166 const std::string device1Name = "device1";
1167 const Device* device1 = nullptr;
1168 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1169 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1170 BOOST_CHECK(device1);
1171
1172 // Register a device
1173 const std::string device2Name = "device2";
1174 const Device* device2 = nullptr;
1175 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1176 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1177 BOOST_CHECK(device2);
1178
1179 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001180 MockBufferManager mockBuffer(10);
Sadik Armagan3896b472020-02-10 12:24:15 +00001181 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001182 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
1183 armnn::profiling::BufferExhaustion);
1184}
1185
1186BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest2)
1187{
1188 // The counter directory used for testing
1189 CounterDirectory counterDirectory;
1190
1191 // Register a device
1192 const std::string device1Name = "device1";
1193 const Device* device1 = nullptr;
1194 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1195 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1196 BOOST_CHECK(device1);
1197
1198 // Register a device
1199 const std::string device2Name = "device2";
1200 const Device* device2 = nullptr;
1201 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1202 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1203 BOOST_CHECK(device2);
1204
1205 // Register a counter set
1206 const std::string counterSet1Name = "counterset1";
1207 const CounterSet* counterSet1 = nullptr;
1208 BOOST_CHECK_NO_THROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1209 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1210 BOOST_CHECK(counterSet1);
1211
1212 // Register a category associated to "device1" and "counterset1"
1213 const std::string category1Name = "category1";
1214 const Category* category1 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001215 BOOST_CHECK_NO_THROW(category1 = counterDirectory.RegisterCategory(category1Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001216 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1217 BOOST_CHECK(category1);
1218
1219 // Register a category not associated to "device2" but no counter set
1220 const std::string category2Name = "category2";
1221 const Category* category2 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001222 BOOST_CHECK_NO_THROW(category2 = counterDirectory.RegisterCategory(category2Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001223 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1224 BOOST_CHECK(category2);
1225
Keith Davis33ed2212020-03-30 10:43:41 +01001226 uint16_t numberOfCores = 4;
Sadik Armagan4c998992020-02-25 12:44:44 +00001227
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001228 // Register a counter associated to "category1"
1229 const Counter* counter1 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001230 BOOST_CHECK_NO_THROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1231 0,
1232 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001233 0,
1234 1,
1235 123.45f,
1236 "counter1",
1237 "counter1description",
Sadik Armagan4c998992020-02-25 12:44:44 +00001238 std::string("counter1units"),
1239 numberOfCores));
Keith Davis33ed2212020-03-30 10:43:41 +01001240 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001241 BOOST_CHECK(counter1);
1242
1243 // Register a counter associated to "category1"
1244 const Counter* counter2 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001245 BOOST_CHECK_NO_THROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1246 4,
1247 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001248 1,
1249 0,
1250 330.1245656765f,
1251 "counter2",
1252 "counter2description",
1253 std::string("counter2units"),
1254 armnn::EmptyOptional(),
1255 device2->m_Uid,
1256 0));
Keith Davis33ed2212020-03-30 10:43:41 +01001257 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001258 BOOST_CHECK(counter2);
1259
1260 // Register a counter associated to "category2"
1261 const Counter* counter3 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001262 BOOST_CHECK_NO_THROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1263 5,
1264 category2Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001265 1,
1266 1,
1267 0.0000045399f,
1268 "counter3",
1269 "counter3description",
1270 armnn::EmptyOptional(),
Keith Davis33ed2212020-03-30 10:43:41 +01001271 numberOfCores,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001272 device2->m_Uid,
1273 counterSet1->m_Uid));
1274 BOOST_CHECK(counterDirectory.GetCounterCount() == 9);
1275 BOOST_CHECK(counter3);
1276
1277 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001278 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001279 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001280 BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
1281
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001282 // Get the readable buffer
1283 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001284
1285 // Check the packet header
Finn Williams985fecf2020-04-30 11:06:43 +01001286 const uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1287 const uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001288 BOOST_TEST(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1289 BOOST_TEST(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
Keith Davis33ed2212020-03-30 10:43:41 +01001290 BOOST_TEST(packetHeaderWord1 == 432); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001291
1292 // Check the body header
Finn Williams985fecf2020-04-30 11:06:43 +01001293 const uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1294 const uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1295 const uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1296 const uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1297 const uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1298 const uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1299 const uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1300 const uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1301 const uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
1302 BOOST_TEST(deviceRecordCount == 2); // device_records_count
1303 BOOST_TEST(bodyHeaderWord1 == bodyHeaderSize * 4); // device_records_pointer_table_offset
1304 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
1305 BOOST_TEST(bodyHeaderWord3 == 8 + bodyHeaderSize * 4); // counter_set_pointer_table_offset
1306 BOOST_TEST(categoryRecordCount == 2); // categories_count
1307 BOOST_TEST(bodyHeaderWord5 == 12 + bodyHeaderSize * 4); // categories_pointer_table_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001308
1309 // Check the device records pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001310 const uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1311 const uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
Finn Williamsd44815f2020-05-01 13:25:55 +01001312 BOOST_TEST(deviceRecordOffset0 == 20); // Device record offset for "device1"
1313 BOOST_TEST(deviceRecordOffset1 == 40); // Device record offset for "device2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001314
1315 // Check the counter set pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001316 const uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
Finn Williamsd44815f2020-05-01 13:25:55 +01001317 BOOST_TEST(counterSetRecordOffset0 == 52); // Counter set record offset for "counterset1"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001318
1319 // Check the category pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001320 const uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1321 const uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
Finn Williamsd44815f2020-05-01 13:25:55 +01001322 BOOST_TEST(categoryRecordOffset0 == 72); // Category record offset for "category1"
1323 BOOST_TEST(categoryRecordOffset1 == 176); // Category record offset for "category2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001324
1325 // Get the device record pool offset
Finn Williams985fecf2020-04-30 11:06:43 +01001326 const uint32_t uint32_t_size = sizeof(uint32_t);
Finn Williamsd44815f2020-05-01 13:25:55 +01001327 const uint32_t packetHeaderSize = 2u * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001328
1329 // Device record structure/collection used for testing
1330 struct DeviceRecord
1331 {
1332 uint16_t uid;
1333 uint16_t cores;
1334 uint32_t name_offset;
1335 uint32_t name_length;
1336 std::string name;
1337 };
1338 std::vector<DeviceRecord> deviceRecords;
Finn Williamsd44815f2020-05-01 13:25:55 +01001339 const uint32_t deviceRecordsPointerTableOffset = packetHeaderSize +
Finn Williams985fecf2020-04-30 11:06:43 +01001340 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001341
1342 const unsigned char* readData = readBuffer->GetReadableData();
1343
Finn Williamsd44815f2020-05-01 13:25:55 +01001344 uint32_t offset = 0;
1345 std::vector<uint32_t> data(800);
1346
1347 for (uint32_t i = 0; i < 800; i+=uint32_t_size)
1348 {
1349 data[i] = ReadUint32(readBuffer, offset);
1350 offset += uint32_t_size;
1351 }
1352
1353 std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount);
1354 offset = deviceRecordsPointerTableOffset;
1355 for (uint32_t i = 0; i < deviceRecordCount; ++i)
1356 {
1357 // deviceRecordOffset is relative to the start of the deviceRecordsPointerTable
1358 deviceRecordOffsets[i] = ReadUint32(readBuffer, offset) + deviceRecordsPointerTableOffset;
1359 offset += uint32_t_size;
1360 }
1361
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001362 for (uint32_t i = 0; i < deviceRecordCount; i++)
1363 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001364 // Collect the data for the device record
Finn Williamsd44815f2020-05-01 13:25:55 +01001365 const uint32_t deviceRecordWord0 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 0 * uint32_t_size);
1366 const uint32_t deviceRecordWord1 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001367 DeviceRecord deviceRecord;
1368 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1369 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1370 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1371
Finn Williamsd44815f2020-05-01 13:25:55 +01001372 uint32_t deviceRecordPoolOffset = deviceRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001373 deviceRecord.name_offset; // Device name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001374 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1375 deviceRecord.name_length = deviceRecordNameLength; // name_length
1376 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1377 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
1378 BOOST_CHECK(deviceRecordNameNullTerminator == '\0');
1379 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1380 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001381 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001382 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1383
1384 deviceRecords.push_back(deviceRecord);
1385 }
1386
1387 // Check that the device records are correct
1388 BOOST_CHECK(deviceRecords.size() == 2);
1389 for (const DeviceRecord& deviceRecord : deviceRecords)
1390 {
1391 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
1392 BOOST_CHECK(device);
1393 BOOST_CHECK(device->m_Uid == deviceRecord.uid);
1394 BOOST_CHECK(device->m_Cores == deviceRecord.cores);
1395 BOOST_CHECK(device->m_Name == deviceRecord.name);
1396 }
1397
Finn Williamsd44815f2020-05-01 13:25:55 +01001398
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001399 // Counter set record structure/collection used for testing
1400 struct CounterSetRecord
1401 {
1402 uint16_t uid;
1403 uint16_t count;
1404 uint32_t name_offset;
1405 uint32_t name_length;
1406 std::string name;
1407 };
1408 std::vector<CounterSetRecord> counterSetRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001409 const uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1410 bodyHeaderWord3; // counter_set_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001411
1412 offset = counterSetRecordsPointerTableOffset;
1413 std::vector<uint32_t> counterSetRecordOffsets(counterSetRecordCount);
1414
1415 for (uint32_t i = 0; i < counterSetRecordCount; ++i)
1416 {
1417 // counterSetRecordOffset is relative to the start of the dcounterSetRecordsPointerTable
1418 counterSetRecordOffsets[i] = ReadUint32(readBuffer, offset) + counterSetRecordsPointerTableOffset;
1419 offset += uint32_t_size;
1420 }
1421
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001422 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1423 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001424 // Collect the data for the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +01001425 const uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 0 * uint32_t_size);
1426 const uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001427 CounterSetRecord counterSetRecord;
1428 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1429 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1430 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1431
Finn Williamsd44815f2020-05-01 13:25:55 +01001432 uint32_t counterSetRecordPoolOffset = counterSetRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001433 counterSetRecord.name_offset; // Counter set name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001434 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1435 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1436 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1437 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
1438 BOOST_CHECK(counterSetRecordNameNullTerminator == '\0');
1439 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1440 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001441 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001442 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1443
1444 counterSetRecords.push_back(counterSetRecord);
1445 }
1446
1447 // Check that the counter set records are correct
1448 BOOST_CHECK(counterSetRecords.size() == 1);
1449 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1450 {
1451 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
1452 BOOST_CHECK(counterSet);
1453 BOOST_CHECK(counterSet->m_Uid == counterSetRecord.uid);
1454 BOOST_CHECK(counterSet->m_Count == counterSetRecord.count);
1455 BOOST_CHECK(counterSet->m_Name == counterSetRecord.name);
1456 }
1457
1458 // Event record structure/collection used for testing
1459 struct EventRecord
1460 {
1461 uint16_t counter_uid;
1462 uint16_t max_counter_uid;
1463 uint16_t device;
1464 uint16_t counter_set;
1465 uint16_t counter_class;
1466 uint16_t interpolation;
1467 double multiplier;
1468 uint32_t name_offset;
1469 uint32_t name_length;
1470 std::string name;
1471 uint32_t description_offset;
1472 uint32_t description_length;
1473 std::string description;
1474 uint32_t units_offset;
1475 uint32_t units_length;
1476 std::string units;
1477 };
1478 // Category record structure/collection used for testing
1479 struct CategoryRecord
1480 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001481 uint16_t event_count;
1482 uint32_t event_pointer_table_offset;
1483 uint32_t name_offset;
1484 uint32_t name_length;
1485 std::string name;
1486 std::vector<uint32_t> event_pointer_table;
1487 std::vector<EventRecord> event_records;
1488 };
1489 std::vector<CategoryRecord> categoryRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001490 const uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1491 bodyHeaderWord5; // categories_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001492
1493 offset = categoryRecordsPointerTableOffset;
1494 std::vector<uint32_t> categoryRecordOffsets(categoryRecordCount);
1495 for (uint32_t i = 0; i < categoryRecordCount; ++i)
1496 {
1497 // categoryRecordOffset is relative to the start of the categoryRecordsPointerTable
1498 categoryRecordOffsets[i] = ReadUint32(readBuffer, offset) + categoryRecordsPointerTableOffset;
1499 offset += uint32_t_size;
1500 }
1501
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001502 for (uint32_t i = 0; i < categoryRecordCount; i++)
1503 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001504 // Collect the data for the category record
Finn Williamsd44815f2020-05-01 13:25:55 +01001505 const uint32_t categoryRecordWord1 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 0 * uint32_t_size);
1506 const uint32_t categoryRecordWord2 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 1 * uint32_t_size);
1507 const uint32_t categoryRecordWord3 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 2 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001508 CategoryRecord categoryRecord;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001509 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1510 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1511 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1512
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001513 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001514 categoryRecordOffsets[i] + categoryRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001515 categoryRecord.name_length = categoryRecordNameLength; // name_length
1516 unsigned char categoryRecordNameNullTerminator =
1517 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001518 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001519 categoryRecord.name_offset +
1520 uint32_t_size +
1521 categoryRecordNameLength - 1); // name null-terminator
1522 BOOST_CHECK(categoryRecordNameNullTerminator == '\0');
1523 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1524 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001525 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001526 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001527 categoryRecord.name_offset +
1528 uint32_t_size,
1529 categoryRecordNameBuffer.size());
1530 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1531
1532 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
Finn Williamsd44815f2020-05-01 13:25:55 +01001533 offset = categoryRecordOffsets[i] + categoryRecord.event_pointer_table_offset;
1534 for (uint32_t eventOffsetIndex = 0; eventOffsetIndex < categoryRecord.event_count; ++eventOffsetIndex)
1535 {
1536 // eventRecordOffset is relative to the start of the event pointer table
1537 categoryRecord.event_pointer_table[eventOffsetIndex] = ReadUint32(readBuffer, offset) +
1538 categoryRecordOffsets[i] +
1539 categoryRecord.event_pointer_table_offset;
1540 offset += uint32_t_size;
1541 }
1542
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001543 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1544 {
Finn Williamsd44815f2020-05-01 13:25:55 +01001545 const uint32_t eventOffset = categoryRecord.event_pointer_table[eventIndex];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001546 // Collect the data for the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001547 const uint32_t eventRecordWord0 = ReadUint32(readBuffer, eventOffset + 0 * uint32_t_size);
1548 const uint32_t eventRecordWord1 = ReadUint32(readBuffer, eventOffset + 1 * uint32_t_size);
1549 const uint32_t eventRecordWord2 = ReadUint32(readBuffer, eventOffset + 2 * uint32_t_size);
1550 const uint64_t eventRecordWord34 = ReadUint64(readBuffer, eventOffset + 3 * uint32_t_size);
1551 const uint32_t eventRecordWord5 = ReadUint32(readBuffer, eventOffset + 5 * uint32_t_size);
1552 const uint32_t eventRecordWord6 = ReadUint32(readBuffer, eventOffset + 6 * uint32_t_size);
1553 const uint32_t eventRecordWord7 = ReadUint32(readBuffer, eventOffset + 7 * uint32_t_size);
1554
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001555 EventRecord eventRecord;
1556 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1557 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1558 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1559 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1560 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1561 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1562 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1563 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1564 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1565 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1566
Finn Williamsd44815f2020-05-01 13:25:55 +01001567 uint32_t eventRecordNameLength = ReadUint32(readBuffer, eventOffset + eventRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001568 eventRecord.name_length = eventRecordNameLength; // name_length
1569 unsigned char eventRecordNameNullTerminator =
1570 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001571 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001572 eventRecord.name_offset +
1573 uint32_t_size +
1574 eventRecordNameLength - 1); // name null-terminator
1575 BOOST_CHECK(eventRecordNameNullTerminator == '\0');
1576 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1577 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001578 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001579 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001580 eventRecord.name_offset +
1581 uint32_t_size,
1582 eventRecordNameBuffer.size());
1583 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1584
1585 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001586 eventOffset + eventRecord.description_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001587 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1588 unsigned char eventRecordDescriptionNullTerminator =
1589 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001590 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001591 eventRecord.description_offset +
1592 uint32_t_size +
1593 eventRecordDescriptionLength - 1); // description null-terminator
1594 BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0');
1595 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1596 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001597 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001598 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001599 eventRecord.description_offset +
1600 uint32_t_size,
1601 eventRecordDescriptionBuffer.size());
1602 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1603 eventRecordDescriptionBuffer.end()); // description
1604
1605 if (eventRecord.units_offset > 0)
1606 {
1607 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001608 eventOffset + eventRecord.units_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001609 eventRecord.units_length = eventRecordUnitsLength; // units_length
1610 unsigned char eventRecordUnitsNullTerminator =
1611 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001612 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001613 eventRecord.units_offset +
1614 uint32_t_size +
1615 eventRecordUnitsLength - 1); // units null-terminator
1616 BOOST_CHECK(eventRecordUnitsNullTerminator == '\0');
1617 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1618 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001619 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001620 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001621 eventRecord.units_offset +
1622 uint32_t_size,
1623 eventRecordUnitsBuffer.size());
1624 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1625 }
1626
1627 categoryRecord.event_records.push_back(eventRecord);
1628 }
1629
1630 categoryRecords.push_back(categoryRecord);
1631 }
1632
1633 // Check that the category records are correct
1634 BOOST_CHECK(categoryRecords.size() == 2);
1635 for (const CategoryRecord& categoryRecord : categoryRecords)
1636 {
1637 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
1638 BOOST_CHECK(category);
1639 BOOST_CHECK(category->m_Name == categoryRecord.name);
Keith Davis33ed2212020-03-30 10:43:41 +01001640 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
1641 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001642
1643 // Check that the event records are correct
1644 for (const EventRecord& eventRecord : categoryRecord.event_records)
1645 {
1646 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
1647 BOOST_CHECK(counter);
1648 BOOST_CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1649 BOOST_CHECK(counter->m_DeviceUid == eventRecord.device);
1650 BOOST_CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1651 BOOST_CHECK(counter->m_Class == eventRecord.counter_class);
1652 BOOST_CHECK(counter->m_Interpolation == eventRecord.interpolation);
1653 BOOST_CHECK(counter->m_Multiplier == eventRecord.multiplier);
1654 BOOST_CHECK(counter->m_Name == eventRecord.name);
1655 BOOST_CHECK(counter->m_Description == eventRecord.description);
1656 BOOST_CHECK(counter->m_Units == eventRecord.units);
1657 }
1658 }
1659}
1660
1661BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest3)
1662{
1663 // Using a mock counter directory that allows to register invalid objects
1664 MockCounterDirectory counterDirectory;
1665
1666 // Register an invalid device
1667 const std::string deviceName = "inv@lid dev!c€";
1668 const Device* device = nullptr;
1669 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1670 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1671 BOOST_CHECK(device);
1672
1673 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001674 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001675 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001676 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1677}
1678
1679BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest4)
1680{
1681 // Using a mock counter directory that allows to register invalid objects
1682 MockCounterDirectory counterDirectory;
1683
1684 // Register an invalid counter set
1685 const std::string counterSetName = "inv@lid count€rs€t";
1686 const CounterSet* counterSet = nullptr;
1687 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1688 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1689 BOOST_CHECK(counterSet);
1690
1691 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001692 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001693 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001694 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1695}
1696
1697BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest5)
1698{
1699 // Using a mock counter directory that allows to register invalid objects
1700 MockCounterDirectory counterDirectory;
1701
1702 // Register an invalid category
1703 const std::string categoryName = "c@t€gory";
1704 const Category* category = nullptr;
1705 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
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(SendCounterDirectoryPacketTest6)
1716{
1717 // Using a mock counter directory that allows to register invalid objects
1718 MockCounterDirectory counterDirectory;
1719
1720 // Register an invalid device
1721 const std::string deviceName = "inv@lid dev!c€";
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 invalid counter set
1728 const std::string counterSetName = "inv@lid count€rs€t";
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 invalid category associated to an invalid device and an invalid counter set
1735 const std::string categoryName = "c@t€gory";
1736 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001737 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001738 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1739 BOOST_CHECK(category);
1740
1741 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001742 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001743 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001744 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1745}
1746
1747BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest7)
1748{
1749 // Using a mock counter directory that allows to register invalid objects
1750 MockCounterDirectory counterDirectory;
1751
1752 // Register an valid device
1753 const std::string deviceName = "valid device";
1754 const Device* device = nullptr;
1755 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1756 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1757 BOOST_CHECK(device);
1758
1759 // Register an valid counter set
1760 const std::string counterSetName = "valid counterset";
1761 const CounterSet* counterSet = nullptr;
1762 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1763 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1764 BOOST_CHECK(counterSet);
1765
1766 // Register an valid category associated to a valid device and a valid counter set
1767 const std::string categoryName = "category";
1768 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001769 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001770 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1771 BOOST_CHECK(category);
1772
1773 // Register an invalid counter associated to a valid category
1774 const Counter* counter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001775 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1776 0,
1777 categoryName,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001778 0,
1779 1,
1780 123.45f,
1781 "counter",
1782 "counter description",
1783 std::string("invalid counter units"),
1784 5,
1785 device->m_Uid,
1786 counterSet->m_Uid));
1787 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1788 BOOST_CHECK(counter);
1789
1790 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001791 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001792 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001793 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1794}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001795
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001796BOOST_AUTO_TEST_CASE(SendThreadTest0)
1797{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001798 ProfilingStateMachine profilingStateMachine;
1799 SetActiveProfilingState(profilingStateMachine);
1800
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001801 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001802 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001803 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1804 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001805
1806 // Try to start the send thread many times, it must only start once
1807
Sadik Armagan3896b472020-02-10 12:24:15 +00001808 sendThread.Start(mockProfilingConnection);
1809 BOOST_CHECK(sendThread.IsRunning());
1810 sendThread.Start(mockProfilingConnection);
1811 sendThread.Start(mockProfilingConnection);
1812 sendThread.Start(mockProfilingConnection);
1813 sendThread.Start(mockProfilingConnection);
1814 BOOST_CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001815
Sadik Armagan3896b472020-02-10 12:24:15 +00001816 sendThread.Stop();
1817 BOOST_CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001818}
1819
1820BOOST_AUTO_TEST_CASE(SendThreadTest1)
1821{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001822 ProfilingStateMachine profilingStateMachine;
1823 SetActiveProfilingState(profilingStateMachine);
1824
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001825 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001826
1827 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001828 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001829 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1830 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1831 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001832
1833 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1834 // something to become available for reading
1835
Colm Donelan2ba48d22019-11-29 09:10:59 +00001836 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001837
1838 CounterDirectory counterDirectory;
1839 sendCounterPacket.SendStreamMetaDataPacket();
1840
Finn Williamsa0de0562020-04-22 12:27:37 +01001841 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001842
Sadik Armagan3896b472020-02-10 12:24:15 +00001843 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001844
Colm Donelan2ba48d22019-11-29 09:10:59 +00001845 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001846
1847 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1848
1849 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001850 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001851 totalWrittenSize += counterDirectoryPacketSize;
1852
Sadik Armagan3896b472020-02-10 12:24:15 +00001853 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001854
Colm Donelan2ba48d22019-11-29 09:10:59 +00001855 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001856
1857 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1858 {
1859 { 1u, 23u },
1860 { 33u, 1207623u }
1861 });
1862
1863 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001864 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001865 totalWrittenSize += periodicCounterCapturePacketSize;
1866
Sadik Armagan3896b472020-02-10 12:24:15 +00001867 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001868
Colm Donelan2ba48d22019-11-29 09:10:59 +00001869 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001870
1871 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1872 {
1873 { 211u, 923u }
1874 });
1875
1876 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001877 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001878 totalWrittenSize += periodicCounterCapturePacketSize;
1879
1880 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1881 {
1882 { 555u, 23u },
1883 { 556u, 6u },
1884 { 557u, 893454u },
1885 { 558u, 1456623u },
1886 { 559u, 571090u }
1887 });
1888
1889 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001890 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001891 totalWrittenSize += periodicCounterCapturePacketSize;
1892
1893 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1894 {
1895 { 88u, 11u },
1896 { 96u, 22u },
1897 { 97u, 33u },
1898 { 999u, 444u }
1899 });
1900
1901 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001902 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001903 totalWrittenSize += periodicCounterCapturePacketSize;
1904
Sadik Armagan3896b472020-02-10 12:24:15 +00001905 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001906
Colm Donelan2ba48d22019-11-29 09:10:59 +00001907 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001908
1909 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1910
1911 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001912 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001913 totalWrittenSize += periodicCounterCapturePacketSize;
1914
Sadik Armagan3896b472020-02-10 12:24:15 +00001915 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001916
Finn Williams109c05b2019-11-29 13:56:33 +00001917 // 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 +01001918 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001919 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001920
Sadik Armagan3896b472020-02-10 12:24:15 +00001921 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001922
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001923 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001924 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1925 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001926}
1927
1928BOOST_AUTO_TEST_CASE(SendThreadTest2)
1929{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001930 ProfilingStateMachine profilingStateMachine;
1931 SetActiveProfilingState(profilingStateMachine);
1932
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001933 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001934
1935 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001936 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001937 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1938 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1939 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001940
1941 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
1942 // capable of handling unnecessary read requests
1943
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
Sadik Armagan3896b472020-02-10 12:24:15 +00001946 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001947
1948 CounterDirectory counterDirectory;
1949 sendCounterPacket.SendStreamMetaDataPacket();
1950
Finn Williamsa0de0562020-04-22 12:27:37 +01001951 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001952
Sadik Armagan3896b472020-02-10 12:24:15 +00001953 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001954
Colm Donelan2ba48d22019-11-29 09:10:59 +00001955 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001956
1957 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1958
1959 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001960 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001961 totalWrittenSize += counterDirectoryPacketSize;
1962
Sadik Armagan3896b472020-02-10 12:24:15 +00001963 sendThread.SetReadyToRead();
1964 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001965
Colm Donelan2ba48d22019-11-29 09:10:59 +00001966 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001967
1968 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1969 {
1970 { 1u, 23u },
1971 { 33u, 1207623u }
1972 });
1973
1974 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001975 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001976 totalWrittenSize += periodicCounterCapturePacketSize;
1977
Sadik Armagan3896b472020-02-10 12:24:15 +00001978 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001979
Colm Donelan2ba48d22019-11-29 09:10:59 +00001980 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001981
Sadik Armagan3896b472020-02-10 12:24:15 +00001982 sendThread.SetReadyToRead();
1983 sendThread.SetReadyToRead();
1984 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001985
Colm Donelan2ba48d22019-11-29 09:10:59 +00001986 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001987
Sadik Armagan3896b472020-02-10 12:24:15 +00001988 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001989 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1990 {
1991 { 211u, 923u }
1992 });
1993
1994 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001995 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001996 totalWrittenSize += periodicCounterCapturePacketSize;
1997
1998 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1999 {
2000 { 555u, 23u },
2001 { 556u, 6u },
2002 { 557u, 893454u },
2003 { 558u, 1456623u },
2004 { 559u, 571090u }
2005 });
2006
2007 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002008 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002009 totalWrittenSize += periodicCounterCapturePacketSize;
2010
Sadik Armagan3896b472020-02-10 12:24:15 +00002011 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002012 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2013 {
2014 { 88u, 11u },
2015 { 96u, 22u },
2016 { 97u, 33u },
2017 { 999u, 444u }
2018 });
2019
2020 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002021 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002022 totalWrittenSize += periodicCounterCapturePacketSize;
2023
Sadik Armagan3896b472020-02-10 12:24:15 +00002024 sendThread.SetReadyToRead();
2025 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002026
Colm Donelan2ba48d22019-11-29 09:10:59 +00002027 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002028
2029 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2030
2031 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002032 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002033 totalWrittenSize += periodicCounterCapturePacketSize;
2034
Sadik Armagan3896b472020-02-10 12:24:15 +00002035 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002036
Finn Williams109c05b2019-11-29 13:56:33 +00002037 // 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 +01002038 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002039 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002040
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002041 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002042 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2043 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002044}
2045
2046BOOST_AUTO_TEST_CASE(SendThreadTest3)
2047{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002048 ProfilingStateMachine profilingStateMachine;
2049 SetActiveProfilingState(profilingStateMachine);
2050
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002051 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002052
2053 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002054 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002055 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
2056 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2057 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002058
2059 // Not using pauses or "grace periods" to stress test the send thread
2060
Sadik Armagan3896b472020-02-10 12:24:15 +00002061 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002062
2063 CounterDirectory counterDirectory;
2064 sendCounterPacket.SendStreamMetaDataPacket();
2065
Finn Williamsa0de0562020-04-22 12:27:37 +01002066 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002067
Sadik Armagan3896b472020-02-10 12:24:15 +00002068 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002069 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2070
2071 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002072 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002073 totalWrittenSize += counterDirectoryPacketSize;
2074
Sadik Armagan3896b472020-02-10 12:24:15 +00002075 sendThread.SetReadyToRead();
2076 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002077 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2078 {
2079 { 1u, 23u },
2080 { 33u, 1207623u }
2081 });
2082
2083 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002084 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002085 totalWrittenSize += periodicCounterCapturePacketSize;
2086
Sadik Armagan3896b472020-02-10 12:24:15 +00002087 sendThread.SetReadyToRead();
2088 sendThread.SetReadyToRead();
2089 sendThread.SetReadyToRead();
2090 sendThread.SetReadyToRead();
2091 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002092 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2093 {
2094 { 211u, 923u }
2095 });
2096
2097 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002098 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002099 totalWrittenSize += periodicCounterCapturePacketSize;
2100
2101 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2102 {
2103 { 555u, 23u },
2104 { 556u, 6u },
2105 { 557u, 893454u },
2106 { 558u, 1456623u },
2107 { 559u, 571090u }
2108 });
2109
2110 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002111 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002112 totalWrittenSize += periodicCounterCapturePacketSize;
2113
Sadik Armagan3896b472020-02-10 12:24:15 +00002114 sendThread.SetReadyToRead();
2115 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002116 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2117 {
2118 { 88u, 11u },
2119 { 96u, 22u },
2120 { 97u, 33u },
2121 { 999u, 444u }
2122 });
2123
2124 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002125 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002126 totalWrittenSize += periodicCounterCapturePacketSize;
2127
Sadik Armagan3896b472020-02-10 12:24:15 +00002128 sendThread.SetReadyToRead();
2129 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002130 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2131
2132 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002133 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002134 totalWrittenSize += periodicCounterCapturePacketSize;
2135
Sadik Armagan3896b472020-02-10 12:24:15 +00002136 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002137
2138 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2139 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002140 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002141
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002142 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2143 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2144 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2145 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2146 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002147}
2148
Sadik Armagan3896b472020-02-10 12:24:15 +00002149BOOST_AUTO_TEST_CASE(SendCounterPacketTestWithSendThread)
2150{
2151 ProfilingStateMachine profilingStateMachine;
2152 SetWaitingForAckProfilingState(profilingStateMachine);
2153
2154 MockProfilingConnection mockProfilingConnection;
2155 BufferManager bufferManager(1, 1024);
2156 SendCounterPacket sendCounterPacket(bufferManager);
2157 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2158 sendThread.Start(mockProfilingConnection);
2159
Finn Williamsa0de0562020-04-22 12:27:37 +01002160 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan3896b472020-02-10 12:24:15 +00002161
2162 sendThread.Stop();
2163
2164 // check for packet in ProfilingConnection
2165 BOOST_CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
2166
2167 SetActiveProfilingState(profilingStateMachine);
2168 sendThread.Start(mockProfilingConnection);
2169
2170 // SendCounterDirectoryPacket
2171 CounterDirectory counterDirectory;
2172 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2173
2174 sendThread.Stop();
2175 unsigned int counterDirectoryPacketSize = 32;
2176 // check for packet in ProfilingConnection
2177 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2178 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2179
2180 sendThread.Start(mockProfilingConnection);
2181
2182 // SendPeriodicCounterCapturePacket
2183 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2184 {
2185 { 1u, 23u },
2186 { 33u, 1207623u }
2187 });
2188
2189 sendThread.Stop();
2190
2191 unsigned int periodicCounterCapturePacketSize = 28;
2192 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2193 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2194}
2195
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002196BOOST_AUTO_TEST_CASE(SendThreadBufferTest)
2197{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002198 ProfilingStateMachine profilingStateMachine;
2199 SetActiveProfilingState(profilingStateMachine);
2200
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002201 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002202 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002203 SendCounterPacket sendCounterPacket(bufferManager);
2204 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2205 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002206
2207 // SendStreamMetaDataPacket
2208 sendCounterPacket.SendStreamMetaDataPacket();
2209
2210 // Read data from the buffer
2211 // Buffer should become readable after commit by SendStreamMetaDataPacket
2212 auto packetBuffer = bufferManager.GetReadableBuffer();
2213 BOOST_TEST(packetBuffer.get());
2214
Finn Williamsa0de0562020-04-22 12:27:37 +01002215 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002216 BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize);
2217
2218 // Recommit to be read by sendCounterPacket
2219 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2220
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002221 // SendCounterDirectoryPacket
2222 CounterDirectory counterDirectory;
2223 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2224
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002225 // SendPeriodicCounterCapturePacket
2226 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2227 {
2228 { 1u, 23u },
2229 { 33u, 1207623u }
2230 });
2231
Sadik Armagan3896b472020-02-10 12:24:15 +00002232 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002233
2234 // The buffer is read by the send thread so it should not be in the readable buffer.
2235 auto readBuffer = bufferManager.GetReadableBuffer();
2236 BOOST_TEST(!readBuffer);
2237
2238 // Successfully reserved the buffer with requested size
2239 unsigned int reservedSize = 0;
2240 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
2241 BOOST_TEST(reservedSize == 512);
2242 BOOST_TEST(reservedBuffer.get());
2243
Finn Williams09ad6f92019-12-19 17:05:18 +00002244 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2245 const auto metaDataPacketCount =
2246 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2247
2248 BOOST_TEST(metaDataPacketCount >= 1);
2249 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2250 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
2251 // Check that we only received the packets we expected
2252 BOOST_TEST(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002253}
2254
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002255BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket1)
2256{
2257 ProfilingStateMachine profilingStateMachine;
2258
2259 MockProfilingConnection mockProfilingConnection;
2260 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002261 SendCounterPacket sendCounterPacket(bufferManager);
2262 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2263 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002264
2265 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002266 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002267}
2268
2269BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket2)
2270{
2271 ProfilingStateMachine profilingStateMachine;
2272 SetNotConnectedProfilingState(profilingStateMachine);
2273
2274 MockProfilingConnection mockProfilingConnection;
2275 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002276 SendCounterPacket sendCounterPacket(bufferManager);
2277 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2278 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002279
2280 // The profiling state is set to "NotConnected", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002281 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002282}
2283
2284BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket3)
2285{
2286 ProfilingStateMachine profilingStateMachine;
2287 SetWaitingForAckProfilingState(profilingStateMachine);
2288
Finn Williamsa0de0562020-04-22 12:27:37 +01002289 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002290
2291 MockProfilingConnection mockProfilingConnection;
2292 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002293 SendCounterPacket sendCounterPacket(bufferManager);
2294 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2295 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002296
2297 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002298 // Wait for sendThread to join
2299 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002300
Finn Williams09ad6f92019-12-19 17:05:18 +00002301 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2302 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2303
Rob Hughesbb46dde2020-05-20 15:27:37 +01002304 BOOST_TEST(writtenDataSize >= 1u);
Finn Williams09ad6f92019-12-19 17:05:18 +00002305 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2306 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002307}
2308
2309BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket4)
2310{
2311 ProfilingStateMachine profilingStateMachine;
2312 SetWaitingForAckProfilingState(profilingStateMachine);
2313
Finn Williamsa0de0562020-04-22 12:27:37 +01002314 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002315
2316 MockProfilingConnection mockProfilingConnection;
2317 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002318 SendCounterPacket sendCounterPacket(bufferManager);
2319 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2320 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002321
2322 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002323 // Wait for sendThread to join
2324 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002325
Sadik Armagan3896b472020-02-10 12:24:15 +00002326 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002327 // Check that the profiling state is still "WaitingForAck"
2328 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2329
Finn Williams3e2969d2019-12-06 17:47:36 +00002330 // Check that the buffer contains at least one Stream Metadata packet
Finn Williams09ad6f92019-12-19 17:05:18 +00002331 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002332
2333 mockProfilingConnection.Clear();
2334
Sadik Armagan3896b472020-02-10 12:24:15 +00002335 sendThread.Stop();
2336 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002337
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002338 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002339 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002340
Sadik Armagan3896b472020-02-10 12:24:15 +00002341 // Wait for sendThread to join
2342 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002343
2344 // Check that the profiling state is still "WaitingForAck"
2345 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2346
Finn Williams09ad6f92019-12-19 17:05:18 +00002347 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2348 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2349
Rob Hughesbb46dde2020-05-20 15:27:37 +01002350 BOOST_TEST(writtenDataSize >= 1u);
Finn Williams09ad6f92019-12-19 17:05:18 +00002351 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2352 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002353}
2354
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01002355BOOST_AUTO_TEST_SUITE_END()