blob: 950f8ffad0e969d400ed1b28ed5ba47f81daf396 [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
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010023#include <boost/test/unit_test.hpp>
24
Francis Murtagh3a161982019-09-04 15:25:02 +010025#include <chrono>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010026
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010027using namespace armnn::profiling;
28
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010029namespace
30{
31
Colm Donelan2ba48d22019-11-29 09:10:59 +000032// A short delay to wait for the thread to process a packet.
Finn Williams09ad6f92019-12-19 17:05:18 +000033uint16_t constexpr WAIT_UNTIL_READABLE_MS = 20;
Colm Donelan2ba48d22019-11-29 09:10:59 +000034
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010035void SetNotConnectedProfilingState(ProfilingStateMachine& profilingStateMachine)
36{
37 ProfilingState currentState = profilingStateMachine.GetCurrentState();
38 switch (currentState)
39 {
40 case ProfilingState::WaitingForAck:
41 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000042 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010043 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000044 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010045 case ProfilingState::Active:
46 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000047 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010048 case ProfilingState::NotConnected:
49 return;
50 default:
51 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
52 }
53}
54
55void SetWaitingForAckProfilingState(ProfilingStateMachine& profilingStateMachine)
56{
57 ProfilingState currentState = profilingStateMachine.GetCurrentState();
58 switch (currentState)
59 {
60 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000061 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010062 case ProfilingState::Active:
63 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000064 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010065 case ProfilingState::NotConnected:
66 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000067 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010068 case ProfilingState::WaitingForAck:
69 return;
70 default:
71 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
72 }
73}
74
75void SetActiveProfilingState(ProfilingStateMachine& profilingStateMachine)
76{
77 ProfilingState currentState = profilingStateMachine.GetCurrentState();
78 switch (currentState)
79 {
80 case ProfilingState::Uninitialised:
81 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000082 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010083 case ProfilingState::NotConnected:
84 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000085 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010086 case ProfilingState::WaitingForAck:
87 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000088 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010089 case ProfilingState::Active:
90 return;
91 default:
92 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
93 }
94}
95
96} // Anonymous namespace
97
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010098BOOST_AUTO_TEST_SUITE(SendCounterPacketTests)
99
Finn Williams09ad6f92019-12-19 17:05:18 +0000100using PacketType = MockProfilingConnection::PacketType;
101
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100102BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest)
103{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100104 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100105 MockSendCounterPacket mockSendCounterPacket(mockBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100106
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100107 mockSendCounterPacket.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100108
109 auto packetBuffer = mockBuffer.GetReadableBuffer();
110 const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100111
112 BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
113
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100114 mockBuffer.MarkRead(packetBuffer);
115
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100116 CounterDirectory counterDirectory;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100117 mockSendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100118
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100119 packetBuffer = mockBuffer.GetReadableBuffer();
120 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
121
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100122 BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
123
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100124 mockBuffer.MarkRead(packetBuffer);
125
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100126 uint64_t timestamp = 0;
Finn Williams032bc742020-02-12 11:02:34 +0000127 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100128
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100129 mockSendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100130
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100131 packetBuffer = mockBuffer.GetReadableBuffer();
132 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
133
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100134 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
135
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100136 mockBuffer.MarkRead(packetBuffer);
137
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100138 uint32_t capturePeriod = 0;
139 std::vector<uint16_t> selectedCounterIds;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100140 mockSendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100141
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100142 packetBuffer = mockBuffer.GetReadableBuffer();
143 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
144
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100145 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100146
147 mockBuffer.MarkRead(packetBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100148}
149
Ferran Balaguer73882172019-09-02 16:39:42 +0100150BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest)
151{
152 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100153 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000154 SendCounterPacket sendPacket1(mockBuffer1);
Ferran Balaguer73882172019-09-02 16:39:42 +0100155
156 uint32_t capturePeriod = 1000;
157 std::vector<uint16_t> selectedCounterIds;
158 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds),
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100159 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100160
161 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100162 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000163 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer73882172019-09-02 16:39:42 +0100164
165 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100166 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100167
168 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
169 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
170 uint32_t period = ReadUint32(readBuffer2, 8);
171
172 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
173 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
174 BOOST_TEST(headerWord1 == 4); // data lenght
175 BOOST_TEST(period == 1000); // capture period
176
177 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100178 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000179 SendCounterPacket sendPacket3(mockBuffer3);
Ferran Balaguer73882172019-09-02 16:39:42 +0100180
181 selectedCounterIds.reserve(5);
182 selectedCounterIds.emplace_back(100);
183 selectedCounterIds.emplace_back(200);
184 selectedCounterIds.emplace_back(300);
185 selectedCounterIds.emplace_back(400);
186 selectedCounterIds.emplace_back(500);
187 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100188 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100189
190 headerWord0 = ReadUint32(readBuffer3, 0);
191 headerWord1 = ReadUint32(readBuffer3, 4);
192 period = ReadUint32(readBuffer3, 8);
193
194 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
195 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
196 BOOST_TEST(headerWord1 == 14); // data lenght
197 BOOST_TEST(period == 1000); // capture period
198
199 uint16_t counterId = 0;
200 uint32_t offset = 12;
201
202 // Counter Ids
203 for(const uint16_t& id : selectedCounterIds)
204 {
205 counterId = ReadUint16(readBuffer3, offset);
206 BOOST_TEST(counterId == id);
207 offset += 2;
208 }
209}
210
Francis Murtagh3a161982019-09-04 15:25:02 +0100211BOOST_AUTO_TEST_CASE(SendPeriodicCounterCapturePacketTest)
212{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100213 ProfilingStateMachine profilingStateMachine;
214
Francis Murtagh3a161982019-09-04 15:25:02 +0100215 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100216 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000217 SendCounterPacket sendPacket1(mockBuffer1);
Francis Murtagh3a161982019-09-04 15:25:02 +0100218
219 auto captureTimestamp = std::chrono::steady_clock::now();
220 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
Finn Williams032bc742020-02-12 11:02:34 +0000221 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100222
223 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
224 BufferExhaustion);
225
226 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100227 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000228 SendCounterPacket sendPacket2(mockBuffer2);
Francis Murtagh3a161982019-09-04 15:25:02 +0100229
230 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100231 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100232
233 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
234 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
235 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
236
Jim Flynnfc365622019-12-04 10:07:20 +0000237 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100238 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
239 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
240 BOOST_TEST(headerWord1 == 8); // data length
241 BOOST_TEST(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100242
243 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100244 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000245 SendCounterPacket sendPacket3(mockBuffer3);
Francis Murtagh3a161982019-09-04 15:25:02 +0100246
247 indexValuePairs.reserve(5);
Finn Williams032bc742020-02-12 11:02:34 +0000248 indexValuePairs.emplace_back(CounterValue{0, 100});
249 indexValuePairs.emplace_back(CounterValue{1, 200});
250 indexValuePairs.emplace_back(CounterValue{2, 300});
251 indexValuePairs.emplace_back(CounterValue{3, 400});
252 indexValuePairs.emplace_back(CounterValue{4, 500});
Francis Murtagh3a161982019-09-04 15:25:02 +0100253 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100254 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100255
256 headerWord0 = ReadUint32(readBuffer3, 0);
257 headerWord1 = ReadUint32(readBuffer3, 4);
258 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
259
Jim Flynnfc365622019-12-04 10:07:20 +0000260 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100261 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
262 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
263 BOOST_TEST(headerWord1 == 38); // data length
264 BOOST_TEST(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100265
266 uint16_t counterIndex = 0;
267 uint32_t counterValue = 100;
268 uint32_t offset = 16;
269
270 // Counter Ids
271 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
272 {
273 // Check Counter Index
274 uint16_t readIndex = ReadUint16(readBuffer3, offset);
275 BOOST_TEST(counterIndex == readIndex);
276 counterIndex++;
277 offset += 2;
278
279 // Check Counter Value
280 uint32_t readValue = ReadUint32(readBuffer3, offset);
281 BOOST_TEST(counterValue == readValue);
282 counterValue += 100;
283 offset += 4;
284 }
285
286}
287
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100288BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest)
289{
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100290 uint32_t sizeUint32 = armnn::numeric_cast<uint32_t>(sizeof(uint32_t));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100291
292 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100293 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000294 SendCounterPacket sendPacket1(mockBuffer1);
Matteo Martincigh149528e2019-09-05 12:02:04 +0100295 BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100296
297 // Full metadata packet
298
299 std::string processName = GetProcessName().substr(0, 60);
300
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100301 uint32_t infoSize = armnn::numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1;
302 uint32_t hardwareVersionSize = armnn::numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1;
303 uint32_t softwareVersionSize = armnn::numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1;
304 uint32_t processNameSize = armnn::numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100305
Jim Flynn83d08a92020-07-09 13:48:16 +0100306 // Supported Packets
307 // Packet Encoding version 1.0.0
308 // Control packet family
309 // Stream metadata packet (packet family=0; packet id=0)
310 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
311 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
312 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
313 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
314 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
315 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
316 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
317 // Counter Packet Family
318 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
319 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
320 // Timeline Packet Family
321 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
322 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
323 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100324 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
325 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
326 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
327 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
328 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
329 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
330 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
331 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
332 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
333 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
334 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
335 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
336 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Jim Flynn83d08a92020-07-09 13:48:16 +0100337
338 uint32_t packetEntries = static_cast<uint32_t>(packetVersions.size());
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100339
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100340 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000341 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100342 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100343 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100344
345 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
346 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
347
348 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
349 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
350
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100351 uint32_t totalLength = armnn::numeric_cast<uint32_t>(2 * sizeUint32 +
352 10 * sizeUint32 + infoSize +
353 hardwareVersionSize + softwareVersionSize +
354 processNameSize + sizeUint32 +
355 2 * packetEntries * sizeUint32);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100356
357 BOOST_TEST(headerWord1 == totalLength - (2 * sizeUint32)); // data length
358
359 uint32_t offset = sizeUint32 * 2;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100360 BOOST_TEST(ReadUint32(readBuffer2, offset) == arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100361 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100362 BOOST_TEST(ReadUint32(readBuffer2, offset) == arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100363 offset += sizeUint32;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100364 BOOST_TEST(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100365 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000366 int pid = armnnUtils::Processes::GetCurrentId();
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100367 BOOST_TEST(ReadUint32(readBuffer2, offset) == armnn::numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100368 offset += sizeUint32;
369 uint32_t poolOffset = 10 * sizeUint32;
Finn Williamsa0de0562020-04-22 12:27:37 +0100370 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100371 offset += sizeUint32;
372 poolOffset += infoSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100373 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100374 offset += sizeUint32;
375 poolOffset += hardwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100376 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100377 offset += sizeUint32;
378 poolOffset += softwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100379 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100380 offset += sizeUint32;
381 poolOffset += processNameSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100382 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100383 offset += sizeUint32;
384 BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved
385
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100386 const unsigned char* readData2 = readBuffer2->GetReadableData();
387
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100388 offset += sizeUint32;
389 if (infoSize)
390 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100391 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100392 offset += infoSize;
393 }
394
395 if (hardwareVersionSize)
396 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100397 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100398 offset += hardwareVersionSize;
399 }
400
401 if (softwareVersionSize)
402 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100403 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100404 offset += softwareVersionSize;
405 }
406
407 if (processNameSize)
408 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100409 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100410 offset += processNameSize;
411 }
412
413 if (packetEntries)
414 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100415 uint32_t numberOfEntries = ReadUint32(readBuffer2, offset);
416 BOOST_TEST((numberOfEntries >> 16) == packetEntries);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100417 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100418 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100419 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100420 uint32_t readPacketId = ReadUint32(readBuffer2, offset);
421 BOOST_TEST(packetVersion.first == readPacketId);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100422 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100423 uint32_t readVersion = ReadUint32(readBuffer2, offset);
424 BOOST_TEST(packetVersion.second == readVersion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100425 offset += sizeUint32;
426 }
427 }
428
429 BOOST_TEST(offset == totalLength);
430}
431
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100432BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest)
433{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100434 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000435 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100436
437 // Create a device for testing
438 uint16_t deviceUid = 27;
439 const std::string deviceName = "some_device";
440 uint16_t deviceCores = 3;
441 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
442
443 // Create a device record
444 SendCounterPacket::DeviceRecord deviceRecord;
445 std::string errorMessage;
446 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
447
448 BOOST_CHECK(result);
449 BOOST_CHECK(errorMessage.empty());
450 BOOST_CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
451
452 uint16_t deviceRecordWord0[]
453 {
454 static_cast<uint16_t>(deviceRecord[0] >> 16),
455 static_cast<uint16_t>(deviceRecord[0])
456 };
457 BOOST_CHECK(deviceRecordWord0[0] == deviceUid); // uid
458 BOOST_CHECK(deviceRecordWord0[1] == deviceCores); // cores
Finn Williams6be1e9b2020-05-15 11:21:54 +0100459 BOOST_CHECK(deviceRecord[1] == 8); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100460 BOOST_CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
461 BOOST_CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
462}
463
464BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest)
465{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100466 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000467 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100468
469 // Create a device for testing
470 uint16_t deviceUid = 27;
471 const std::string deviceName = "some€£invalid‡device";
472 uint16_t deviceCores = 3;
473 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
474
475 // Create a device record
476 SendCounterPacket::DeviceRecord deviceRecord;
477 std::string errorMessage;
478 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
479
480 BOOST_CHECK(!result);
481 BOOST_CHECK(!errorMessage.empty());
482 BOOST_CHECK(deviceRecord.empty());
483}
484
485BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest)
486{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100487 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000488 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100489
490 // Create a counter set for testing
491 uint16_t counterSetUid = 27;
492 const std::string counterSetName = "some_counter_set";
493 uint16_t counterSetCount = 3421;
494 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
495
496 // Create a counter set record
497 SendCounterPacket::CounterSetRecord counterSetRecord;
498 std::string errorMessage;
499 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
500
501 BOOST_CHECK(result);
502 BOOST_CHECK(errorMessage.empty());
503 BOOST_CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
504
505 uint16_t counterSetRecordWord0[]
506 {
507 static_cast<uint16_t>(counterSetRecord[0] >> 16),
508 static_cast<uint16_t>(counterSetRecord[0])
509 };
510 BOOST_CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
511 BOOST_CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
Finn Williams6be1e9b2020-05-15 11:21:54 +0100512 BOOST_CHECK(counterSetRecord[1] == 8); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100513 BOOST_CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
514 BOOST_CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
515}
516
517BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest)
518{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100519 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000520 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100521
522 // Create a counter set for testing
523 uint16_t counterSetUid = 27;
524 const std::string counterSetName = "some invalid_counter€£set";
525 uint16_t counterSetCount = 3421;
526 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
527
528 // Create a counter set record
529 SendCounterPacket::CounterSetRecord counterSetRecord;
530 std::string errorMessage;
531 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
532
533 BOOST_CHECK(!result);
534 BOOST_CHECK(!errorMessage.empty());
535 BOOST_CHECK(counterSetRecord.empty());
536}
537
538BOOST_AUTO_TEST_CASE(CreateEventRecordTest)
539{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100540 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000541 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100542
543 // Create a counter for testing
544 uint16_t counterUid = 7256;
545 uint16_t maxCounterUid = 132;
546 uint16_t deviceUid = 132;
547 uint16_t counterSetUid = 4497;
548 uint16_t counterClass = 1;
549 uint16_t counterInterpolation = 1;
550 double counterMultiplier = 1234.567f;
551 const std::string counterName = "some_valid_counter";
552 const std::string counterDescription = "a_counter_for_testing";
553 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000554 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
555 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100556 maxCounterUid,
557 counterClass,
558 counterInterpolation,
559 counterMultiplier,
560 counterName,
561 counterDescription,
562 counterUnits,
563 deviceUid,
564 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100565 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100566
567 // Create an event record
568 SendCounterPacket::EventRecord eventRecord;
569 std::string errorMessage;
570 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
571
572 BOOST_CHECK(result);
573 BOOST_CHECK(errorMessage.empty());
574 BOOST_CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
575
576 uint16_t eventRecordWord0[]
577 {
578 static_cast<uint16_t>(eventRecord[0] >> 16),
579 static_cast<uint16_t>(eventRecord[0])
580 };
581 uint16_t eventRecordWord1[]
582 {
583 static_cast<uint16_t>(eventRecord[1] >> 16),
584 static_cast<uint16_t>(eventRecord[1])
585 };
586 uint16_t eventRecordWord2[]
587 {
588 static_cast<uint16_t>(eventRecord[2] >> 16),
589 static_cast<uint16_t>(eventRecord[2])
590 };
591 uint32_t eventRecordWord34[]
592 {
593 eventRecord[3],
594 eventRecord[4]
595 };
Finn Williamsd44815f2020-05-01 13:25:55 +0100596
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100597 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
598 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
599 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
Finn Williamsd44815f2020-05-01 13:25:55 +0100600
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100601 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
602 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
603 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
604 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
605
606 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100607 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
608 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100609 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
610 4u + // Counter name length (uint32_t)
611 counterName.size() + // 18u
612 1u + // Null-terminator
613 1u; // Rounding to the next word
Finn Williamsd44815f2020-05-01 13:25:55 +0100614
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100615 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
616 4u + // Counter description length (uint32_t)
617 counterDescription.size() + // 21u
618 1u + // Null-terminator
Finn Williamsd44815f2020-05-01 13:25:55 +0100619 2u; // Rounding to the next word
620
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100621 ARMNN_NO_CONVERSION_WARN_END
622
623 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
624 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
625 BOOST_CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
626
Finn Williamsd44815f2020-05-01 13:25:55 +0100627 // Offsets are relative to the start of the eventRecord
628 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100629 size_t uint32_t_size = sizeof(uint32_t);
630
631 // The length of the SWTrace string (name)
632 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
633 // The counter name
634 BOOST_CHECK(std::memcmp(eventRecordPool +
635 counterNameOffset + // Offset
636 uint32_t_size /* The length of the name */,
637 counterName.data(),
638 counterName.size()) == 0); // name
639 // The null-terminator at the end of the name
640 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
641
642 // The length of the SWTrace string (description)
643 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
644 // The counter description
645 BOOST_CHECK(std::memcmp(eventRecordPool +
646 counterDescriptionOffset + // Offset
647 uint32_t_size /* The length of the description */,
648 counterDescription.data(),
649 counterDescription.size()) == 0); // description
650 // The null-terminator at the end of the description
651 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
652
653 // The length of the SWTrace namestring (units)
654 BOOST_CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
655 // The counter units
656 BOOST_CHECK(std::memcmp(eventRecordPool +
657 counterUnitsOffset + // Offset
658 uint32_t_size /* The length of the units */,
659 counterUnits.data(),
660 counterUnits.size()) == 0); // units
661 // The null-terminator at the end of the units
662 BOOST_CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
663}
664
665BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest)
666{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100667 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000668 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100669
670 // Create a counter for testing
671 uint16_t counterUid = 44312;
672 uint16_t maxCounterUid = 345;
673 uint16_t deviceUid = 101;
674 uint16_t counterSetUid = 34035;
675 uint16_t counterClass = 0;
676 uint16_t counterInterpolation = 1;
677 double counterMultiplier = 4435.0023f;
678 const std::string counterName = "some_valid_counter";
679 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000680 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
681 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100682 maxCounterUid,
683 counterClass,
684 counterInterpolation,
685 counterMultiplier,
686 counterName,
687 counterDescription,
688 "",
689 deviceUid,
690 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100691 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100692
693 // Create an event record
694 SendCounterPacket::EventRecord eventRecord;
695 std::string errorMessage;
696 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
697
698 BOOST_CHECK(result);
699 BOOST_CHECK(errorMessage.empty());
700 BOOST_CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
701
702 uint16_t eventRecordWord0[]
703 {
704 static_cast<uint16_t>(eventRecord[0] >> 16),
705 static_cast<uint16_t>(eventRecord[0])
706 };
707 uint16_t eventRecordWord1[]
708 {
709 static_cast<uint16_t>(eventRecord[1] >> 16),
710 static_cast<uint16_t>(eventRecord[1])
711 };
712 uint16_t eventRecordWord2[]
713 {
714 static_cast<uint16_t>(eventRecord[2] >> 16),
715 static_cast<uint16_t>(eventRecord[2])
716 };
717 uint32_t eventRecordWord34[]
718 {
719 eventRecord[3],
720 eventRecord[4]
721 };
722 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
723 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
724 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
725 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
726 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
727 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
728 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
729
730 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100731 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
732 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100733 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
734 4u + // Counter name length (uint32_t)
735 counterName.size() + // 18u
736 1u + // Null-terminator
737 1u; // Rounding to the next word
738 ARMNN_NO_CONVERSION_WARN_END
739
740 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
741 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
742 BOOST_CHECK(eventRecord[7] == 0); // units_offset
743
Finn Williamsd44815f2020-05-01 13:25:55 +0100744 // Offsets are relative to the start of the eventRecord
745 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100746 size_t uint32_t_size = sizeof(uint32_t);
747
748 // The length of the SWTrace string (name)
749 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
750 // The counter name
751 BOOST_CHECK(std::memcmp(eventRecordPool +
752 counterNameOffset + // Offset
753 uint32_t_size, // The length of the name
754 counterName.data(),
755 counterName.size()) == 0); // name
756 // The null-terminator at the end of the name
757 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
758
759 // The length of the SWTrace string (description)
760 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
761 // The counter description
762 BOOST_CHECK(std::memcmp(eventRecordPool +
763 counterDescriptionOffset + // Offset
764 uint32_t_size, // The length of the description
765 counterDescription.data(),
766 counterDescription.size()) == 0); // description
767 // The null-terminator at the end of the description
768 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
769}
770
771BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1)
772{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100773 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000774 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100775
776 // Create a counter for testing
777 uint16_t counterUid = 7256;
778 uint16_t maxCounterUid = 132;
779 uint16_t deviceUid = 132;
780 uint16_t counterSetUid = 4497;
781 uint16_t counterClass = 1;
782 uint16_t counterInterpolation = 1;
783 double counterMultiplier = 1234.567f;
784 const std::string counterName = "some_invalid_counter £££"; // Invalid name
785 const std::string counterDescription = "a_counter_for_testing";
786 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000787 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
788 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100789 maxCounterUid,
790 counterClass,
791 counterInterpolation,
792 counterMultiplier,
793 counterName,
794 counterDescription,
795 counterUnits,
796 deviceUid,
797 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100798 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100799
800 // Create an event record
801 SendCounterPacket::EventRecord eventRecord;
802 std::string errorMessage;
803 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
804
805 BOOST_CHECK(!result);
806 BOOST_CHECK(!errorMessage.empty());
807 BOOST_CHECK(eventRecord.empty());
808}
809
810BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2)
811{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100812 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000813 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100814
815 // Create a counter for testing
816 uint16_t counterUid = 7256;
817 uint16_t maxCounterUid = 132;
818 uint16_t deviceUid = 132;
819 uint16_t counterSetUid = 4497;
820 uint16_t counterClass = 1;
821 uint16_t counterInterpolation = 1;
822 double counterMultiplier = 1234.567f;
823 const std::string counterName = "some_invalid_counter";
824 const std::string counterDescription = "an invalid d€scription"; // Invalid description
825 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000826 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
827 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100828 maxCounterUid,
829 counterClass,
830 counterInterpolation,
831 counterMultiplier,
832 counterName,
833 counterDescription,
834 counterUnits,
835 deviceUid,
836 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100837 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100838
839 // Create an event record
840 SendCounterPacket::EventRecord eventRecord;
841 std::string errorMessage;
842 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
843
844 BOOST_CHECK(!result);
845 BOOST_CHECK(!errorMessage.empty());
846 BOOST_CHECK(eventRecord.empty());
847}
848
849BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3)
850{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100851 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000852 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100853
854 // Create a counter for testing
855 uint16_t counterUid = 7256;
856 uint16_t maxCounterUid = 132;
857 uint16_t deviceUid = 132;
858 uint16_t counterSetUid = 4497;
859 uint16_t counterClass = 1;
860 uint16_t counterInterpolation = 1;
861 double counterMultiplier = 1234.567f;
862 const std::string counterName = "some_invalid_counter";
863 const std::string counterDescription = "a valid description";
864 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000865 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
866 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100867 maxCounterUid,
868 counterClass,
869 counterInterpolation,
870 counterMultiplier,
871 counterName,
872 counterDescription,
873 counterUnits,
874 deviceUid,
875 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100876 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100877
878 // Create an event record
879 SendCounterPacket::EventRecord eventRecord;
880 std::string errorMessage;
881 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
882
883 BOOST_CHECK(!result);
884 BOOST_CHECK(!errorMessage.empty());
885 BOOST_CHECK(eventRecord.empty());
886}
887
888BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest)
889{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100890 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000891 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100892
893 // Create a category for testing
894 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +0000895 const CategoryPtr category = std::make_unique<Category>(categoryName);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100896 ARMNN_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100897 category->m_Counters = { 11u, 23u, 5670u };
898
899 // Create a collection of counters
900 Counters counters;
901 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000902 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100903 0,
Keith Davise394bd92019-12-02 15:12:19 +0000904 11,
905 0,
906 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100907 534.0003f,
908 "counter1",
909 "the first counter",
910 "millipi2",
911 0,
912 0))));
913 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000914 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100915 1,
Keith Davise394bd92019-12-02 15:12:19 +0000916 23,
917 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100918 1,
919 534.0003f,
920 "this is counter 2",
921 "the second counter",
922 "",
923 0,
924 0))));
925 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000926 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
927 2,
928 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100929 0,
930 0,
931 534.0003f,
932 "and this is number 3",
933 "the third counter",
934 "blah_per_second",
935 0,
936 0))));
937 Counter* counter1 = counters.find(11)->second.get();
938 Counter* counter2 = counters.find(23)->second.get();
939 Counter* counter3 = counters.find(5670)->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100940 ARMNN_ASSERT(counter1);
941 ARMNN_ASSERT(counter2);
942 ARMNN_ASSERT(counter3);
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100943 uint16_t categoryEventCount = armnn::numeric_cast<uint16_t>(counters.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100944
945 // Create a category record
946 SendCounterPacket::CategoryRecord categoryRecord;
947 std::string errorMessage;
948 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
949
950 BOOST_CHECK(result);
951 BOOST_CHECK(errorMessage.empty());
Sadik Armagan4c998992020-02-25 12:44:44 +0000952 BOOST_CHECK(categoryRecord.size() == 79); // Size in words: header [3] + event pointer table [3] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100953 // category name [5] + event records [68 = 22 + 20 + 26]
954
Sadik Armagan4c998992020-02-25 12:44:44 +0000955 uint16_t categoryRecordWord1[]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100956 {
957 static_cast<uint16_t>(categoryRecord[0] >> 16),
958 static_cast<uint16_t>(categoryRecord[0])
959 };
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100960 BOOST_CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
961 BOOST_CHECK(categoryRecordWord1[1] == 0); // reserved
962
963 size_t uint32_t_size = sizeof(uint32_t);
964
965 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100966 uint32_t categoryRecordBlockSize = 3u * uint32_t_size;
967 uint32_t eventPointerTableOffset = categoryRecordBlockSize; // The event pointer table is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100968 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
969 categoryEventCount * uint32_t_size; // The size of the event pointer table
970 ARMNN_NO_CONVERSION_WARN_END
971
Sadik Armagan4c998992020-02-25 12:44:44 +0000972 BOOST_CHECK(categoryRecord[1] == eventPointerTableOffset); // event_pointer_table_offset
973 BOOST_CHECK(categoryRecord[2] == categoryNameOffset); // name_offset
Finn Williamsd44815f2020-05-01 13:25:55 +0100974 // Offsets are relative to the start of the category record
975 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100976
977 // The event pointer table
978 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
979 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
980 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
981 BOOST_CHECK(eventRecord0Offset == 32);
982 BOOST_CHECK(eventRecord1Offset == 120);
983 BOOST_CHECK(eventRecord2Offset == 200);
984
985 // The length of the SWTrace namestring (name)
986 BOOST_CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
987 // The category name
988 BOOST_CHECK(std::memcmp(categoryRecordPool +
989 categoryNameOffset + // Offset
990 uint32_t_size, // The length of the name
991 categoryName.data(),
992 categoryName.size()) == 0); // name
993 // The null-terminator at the end of the name
994 BOOST_CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
995
996 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
997 // as the event records already have a number of unit tests dedicated to them
998
999 // Counter1 UID and max counter UID
1000 uint16_t eventRecord0Word0[2] = { 0u, 0u };
Finn Williamsd44815f2020-05-01 13:25:55 +01001001 std::memcpy(eventRecord0Word0, categoryRecordPool + categoryRecordBlockSize + eventRecord0Offset,
1002 sizeof(eventRecord0Word0));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001003 BOOST_CHECK(eventRecord0Word0[0] == counter1->m_Uid);
1004 BOOST_CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
1005
1006 // Counter1 name
1007 uint32_t counter1NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001008 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001009 BOOST_CHECK(counter1NameOffset == 0);
1010 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001011 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
1012 categoryRecordBlockSize + // Offset to the end of the category record block
1013 8u * uint32_t_size + // Offset to the event record pool
1014 counter1NameOffset // Offset to the name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001015 ] == counter1->m_Name.size() + 1); // The length of the name including the
1016 // null-terminator
1017 // The counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001018 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1019 categoryRecordBlockSize + // Offset to the end of the category record block
1020 eventRecord0Offset + // Offset to the event record
1021 8u * uint32_t_size + // Offset to the event record pool
1022 counter1NameOffset + // Offset to the name of the counter
1023 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001024 counter1->m_Name.data(),
1025 counter1->m_Name.size()) == 0); // name
1026 // The null-terminator at the end of the counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001027 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
1028 categoryRecordBlockSize + // Offset to the end of the category record block
1029 8u * uint32_t_size + // Offset to the event record pool
1030 counter1NameOffset + // Offset to the name of the counter
1031 uint32_t_size + // The length of the name
1032 counter1->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001033 ] == '\0');
1034
1035 // Counter2 name
1036 uint32_t counter2NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001037 std::memcpy(&counter2NameOffset, categoryRecordPool +
1038 categoryRecordBlockSize +
1039 eventRecord1Offset +
1040 5u * uint32_t_size,
1041 uint32_t_size);
1042 BOOST_CHECK(counter2NameOffset == 8u * uint32_t_size );
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001043 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001044
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001045 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001046 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001047 counter2NameOffset // Offset to the name of the counter
1048 ] == counter2->m_Name.size() + 1); // The length of the name including the
1049 // null-terminator
1050 // The counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001051 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1052 categoryRecordBlockSize + // Offset to the end of the category record block
1053 eventRecord1Offset + // Offset to the event record
1054 counter2NameOffset + // Offset to the name of the counter
1055 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001056 counter2->m_Name.data(),
1057 counter2->m_Name.size()) == 0); // name
Finn Williamsd44815f2020-05-01 13:25:55 +01001058
1059
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001060 // The null-terminator at the end of the counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001061 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
1062 categoryRecordBlockSize + // Offset to the end of the category record block
1063 counter2NameOffset + // Offset to the name of the counter
1064 uint32_t_size + // The length of the name
1065 counter2->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001066 ] == '\0');
1067
1068 // Counter3 name
1069 uint32_t counter3NameOffset = 0;
1070 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
1071 BOOST_CHECK(counter3NameOffset == 0);
1072 // The length of the SWTrace string (name)
1073 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001074 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001075 8u * uint32_t_size + // Offset to the event record pool
1076 counter3NameOffset // Offset to the name of the counter
1077 ] == counter3->m_Name.size() + 1); // The length of the name including the
1078 // null-terminator
1079 // The counter3 name
1080 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001081 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001082 eventRecord2Offset + // Offset to the event record
1083 8u * uint32_t_size + // Offset to the event record pool
1084 counter3NameOffset + // Offset to the name of the counter
1085 uint32_t_size, // The length of the name
1086 counter3->m_Name.data(),
1087 counter3->m_Name.size()) == 0); // name
1088 // The null-terminator at the end of the counter3 name
1089 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001090 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001091 8u * uint32_t_size + // Offset to the event record pool
1092 counter3NameOffset + // Offset to the name of the counter
1093 uint32_t_size + // The length of the name
1094 counter3->m_Name.size() // The name of the counter
1095 ] == '\0');
1096}
1097
1098BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1)
1099{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001100 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001101 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001102
1103 // Create a category for testing
1104 const std::string categoryName = "some invalid category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001105 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001106 BOOST_CHECK(category);
1107
1108 // Create a category record
1109 Counters counters;
1110 SendCounterPacket::CategoryRecord categoryRecord;
1111 std::string errorMessage;
1112 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1113
1114 BOOST_CHECK(!result);
1115 BOOST_CHECK(!errorMessage.empty());
1116 BOOST_CHECK(categoryRecord.empty());
1117}
1118
1119BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest2)
1120{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001121 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001122 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001123
1124 // Create a category for testing
1125 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001126 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001127 BOOST_CHECK(category);
1128 category->m_Counters = { 11u, 23u, 5670u };
1129
1130 // Create a collection of counters
1131 Counters counters;
1132 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001133 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1134 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001135 1234,
1136 0,
1137 1,
1138 534.0003f,
1139 "count€r1", // Invalid name
1140 "the first counter",
1141 "millipi2",
1142 0,
1143 0))));
1144
1145 Counter* counter1 = counters.find(11)->second.get();
1146 BOOST_CHECK(counter1);
1147
1148 // Create a category record
1149 SendCounterPacket::CategoryRecord categoryRecord;
1150 std::string errorMessage;
1151 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1152
1153 BOOST_CHECK(!result);
1154 BOOST_CHECK(!errorMessage.empty());
1155 BOOST_CHECK(categoryRecord.empty());
1156}
1157
1158BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest1)
1159{
1160 // The counter directory used for testing
1161 CounterDirectory counterDirectory;
1162
1163 // Register a device
1164 const std::string device1Name = "device1";
1165 const Device* device1 = nullptr;
1166 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1167 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1168 BOOST_CHECK(device1);
1169
1170 // Register a device
1171 const std::string device2Name = "device2";
1172 const Device* device2 = nullptr;
1173 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1174 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1175 BOOST_CHECK(device2);
1176
1177 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001178 MockBufferManager mockBuffer(10);
Sadik Armagan3896b472020-02-10 12:24:15 +00001179 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001180 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
1181 armnn::profiling::BufferExhaustion);
1182}
1183
1184BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest2)
1185{
1186 // The counter directory used for testing
1187 CounterDirectory counterDirectory;
1188
1189 // Register a device
1190 const std::string device1Name = "device1";
1191 const Device* device1 = nullptr;
1192 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1193 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1194 BOOST_CHECK(device1);
1195
1196 // Register a device
1197 const std::string device2Name = "device2";
1198 const Device* device2 = nullptr;
1199 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1200 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1201 BOOST_CHECK(device2);
1202
1203 // Register a counter set
1204 const std::string counterSet1Name = "counterset1";
1205 const CounterSet* counterSet1 = nullptr;
1206 BOOST_CHECK_NO_THROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1207 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1208 BOOST_CHECK(counterSet1);
1209
1210 // Register a category associated to "device1" and "counterset1"
1211 const std::string category1Name = "category1";
1212 const Category* category1 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001213 BOOST_CHECK_NO_THROW(category1 = counterDirectory.RegisterCategory(category1Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001214 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1215 BOOST_CHECK(category1);
1216
1217 // Register a category not associated to "device2" but no counter set
1218 const std::string category2Name = "category2";
1219 const Category* category2 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001220 BOOST_CHECK_NO_THROW(category2 = counterDirectory.RegisterCategory(category2Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001221 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1222 BOOST_CHECK(category2);
1223
Keith Davis33ed2212020-03-30 10:43:41 +01001224 uint16_t numberOfCores = 4;
Sadik Armagan4c998992020-02-25 12:44:44 +00001225
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001226 // Register a counter associated to "category1"
1227 const Counter* counter1 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001228 BOOST_CHECK_NO_THROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1229 0,
1230 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001231 0,
1232 1,
1233 123.45f,
1234 "counter1",
1235 "counter1description",
Sadik Armagan4c998992020-02-25 12:44:44 +00001236 std::string("counter1units"),
1237 numberOfCores));
Keith Davis33ed2212020-03-30 10:43:41 +01001238 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001239 BOOST_CHECK(counter1);
1240
1241 // Register a counter associated to "category1"
1242 const Counter* counter2 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001243 BOOST_CHECK_NO_THROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1244 4,
1245 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001246 1,
1247 0,
1248 330.1245656765f,
1249 "counter2",
1250 "counter2description",
1251 std::string("counter2units"),
1252 armnn::EmptyOptional(),
1253 device2->m_Uid,
1254 0));
Keith Davis33ed2212020-03-30 10:43:41 +01001255 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001256 BOOST_CHECK(counter2);
1257
1258 // Register a counter associated to "category2"
1259 const Counter* counter3 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001260 BOOST_CHECK_NO_THROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1261 5,
1262 category2Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001263 1,
1264 1,
1265 0.0000045399f,
1266 "counter3",
1267 "counter3description",
1268 armnn::EmptyOptional(),
Keith Davis33ed2212020-03-30 10:43:41 +01001269 numberOfCores,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001270 device2->m_Uid,
1271 counterSet1->m_Uid));
1272 BOOST_CHECK(counterDirectory.GetCounterCount() == 9);
1273 BOOST_CHECK(counter3);
1274
1275 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001276 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001277 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001278 BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
1279
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001280 // Get the readable buffer
1281 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001282
1283 // Check the packet header
Finn Williams985fecf2020-04-30 11:06:43 +01001284 const uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1285 const uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001286 BOOST_TEST(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1287 BOOST_TEST(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
Keith Davis33ed2212020-03-30 10:43:41 +01001288 BOOST_TEST(packetHeaderWord1 == 432); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001289
1290 // Check the body header
Finn Williams985fecf2020-04-30 11:06:43 +01001291 const uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1292 const uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1293 const uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1294 const uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1295 const uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1296 const uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1297 const uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1298 const uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1299 const uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
1300 BOOST_TEST(deviceRecordCount == 2); // device_records_count
1301 BOOST_TEST(bodyHeaderWord1 == bodyHeaderSize * 4); // device_records_pointer_table_offset
1302 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
1303 BOOST_TEST(bodyHeaderWord3 == 8 + bodyHeaderSize * 4); // counter_set_pointer_table_offset
1304 BOOST_TEST(categoryRecordCount == 2); // categories_count
1305 BOOST_TEST(bodyHeaderWord5 == 12 + bodyHeaderSize * 4); // categories_pointer_table_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001306
1307 // Check the device records pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001308 const uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1309 const uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
Finn Williamsd44815f2020-05-01 13:25:55 +01001310 BOOST_TEST(deviceRecordOffset0 == 20); // Device record offset for "device1"
1311 BOOST_TEST(deviceRecordOffset1 == 40); // Device record offset for "device2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001312
1313 // Check the counter set pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001314 const uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
Finn Williamsd44815f2020-05-01 13:25:55 +01001315 BOOST_TEST(counterSetRecordOffset0 == 52); // Counter set record offset for "counterset1"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001316
1317 // Check the category pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001318 const uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1319 const uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
Finn Williamsd44815f2020-05-01 13:25:55 +01001320 BOOST_TEST(categoryRecordOffset0 == 72); // Category record offset for "category1"
1321 BOOST_TEST(categoryRecordOffset1 == 176); // Category record offset for "category2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001322
1323 // Get the device record pool offset
Finn Williams985fecf2020-04-30 11:06:43 +01001324 const uint32_t uint32_t_size = sizeof(uint32_t);
Finn Williamsd44815f2020-05-01 13:25:55 +01001325 const uint32_t packetHeaderSize = 2u * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001326
1327 // Device record structure/collection used for testing
1328 struct DeviceRecord
1329 {
1330 uint16_t uid;
1331 uint16_t cores;
1332 uint32_t name_offset;
1333 uint32_t name_length;
1334 std::string name;
1335 };
1336 std::vector<DeviceRecord> deviceRecords;
Finn Williamsd44815f2020-05-01 13:25:55 +01001337 const uint32_t deviceRecordsPointerTableOffset = packetHeaderSize +
Finn Williams985fecf2020-04-30 11:06:43 +01001338 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001339
1340 const unsigned char* readData = readBuffer->GetReadableData();
1341
Finn Williamsd44815f2020-05-01 13:25:55 +01001342 uint32_t offset = 0;
1343 std::vector<uint32_t> data(800);
1344
1345 for (uint32_t i = 0; i < 800; i+=uint32_t_size)
1346 {
1347 data[i] = ReadUint32(readBuffer, offset);
1348 offset += uint32_t_size;
1349 }
1350
1351 std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount);
1352 offset = deviceRecordsPointerTableOffset;
1353 for (uint32_t i = 0; i < deviceRecordCount; ++i)
1354 {
1355 // deviceRecordOffset is relative to the start of the deviceRecordsPointerTable
1356 deviceRecordOffsets[i] = ReadUint32(readBuffer, offset) + deviceRecordsPointerTableOffset;
1357 offset += uint32_t_size;
1358 }
1359
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001360 for (uint32_t i = 0; i < deviceRecordCount; i++)
1361 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001362 // Collect the data for the device record
Finn Williamsd44815f2020-05-01 13:25:55 +01001363 const uint32_t deviceRecordWord0 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 0 * uint32_t_size);
1364 const uint32_t deviceRecordWord1 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001365 DeviceRecord deviceRecord;
1366 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1367 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1368 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1369
Finn Williamsd44815f2020-05-01 13:25:55 +01001370 uint32_t deviceRecordPoolOffset = deviceRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001371 deviceRecord.name_offset; // Device name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001372 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1373 deviceRecord.name_length = deviceRecordNameLength; // name_length
1374 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1375 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
1376 BOOST_CHECK(deviceRecordNameNullTerminator == '\0');
1377 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1378 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001379 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001380 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1381
1382 deviceRecords.push_back(deviceRecord);
1383 }
1384
1385 // Check that the device records are correct
1386 BOOST_CHECK(deviceRecords.size() == 2);
1387 for (const DeviceRecord& deviceRecord : deviceRecords)
1388 {
1389 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
1390 BOOST_CHECK(device);
1391 BOOST_CHECK(device->m_Uid == deviceRecord.uid);
1392 BOOST_CHECK(device->m_Cores == deviceRecord.cores);
1393 BOOST_CHECK(device->m_Name == deviceRecord.name);
1394 }
1395
Finn Williamsd44815f2020-05-01 13:25:55 +01001396
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001397 // Counter set record structure/collection used for testing
1398 struct CounterSetRecord
1399 {
1400 uint16_t uid;
1401 uint16_t count;
1402 uint32_t name_offset;
1403 uint32_t name_length;
1404 std::string name;
1405 };
1406 std::vector<CounterSetRecord> counterSetRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001407 const uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1408 bodyHeaderWord3; // counter_set_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001409
1410 offset = counterSetRecordsPointerTableOffset;
1411 std::vector<uint32_t> counterSetRecordOffsets(counterSetRecordCount);
1412
1413 for (uint32_t i = 0; i < counterSetRecordCount; ++i)
1414 {
1415 // counterSetRecordOffset is relative to the start of the dcounterSetRecordsPointerTable
1416 counterSetRecordOffsets[i] = ReadUint32(readBuffer, offset) + counterSetRecordsPointerTableOffset;
1417 offset += uint32_t_size;
1418 }
1419
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001420 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1421 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001422 // Collect the data for the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +01001423 const uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 0 * uint32_t_size);
1424 const uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001425 CounterSetRecord counterSetRecord;
1426 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1427 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1428 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1429
Finn Williamsd44815f2020-05-01 13:25:55 +01001430 uint32_t counterSetRecordPoolOffset = counterSetRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001431 counterSetRecord.name_offset; // Counter set name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001432 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1433 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1434 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1435 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
1436 BOOST_CHECK(counterSetRecordNameNullTerminator == '\0');
1437 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1438 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001439 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001440 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1441
1442 counterSetRecords.push_back(counterSetRecord);
1443 }
1444
1445 // Check that the counter set records are correct
1446 BOOST_CHECK(counterSetRecords.size() == 1);
1447 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1448 {
1449 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
1450 BOOST_CHECK(counterSet);
1451 BOOST_CHECK(counterSet->m_Uid == counterSetRecord.uid);
1452 BOOST_CHECK(counterSet->m_Count == counterSetRecord.count);
1453 BOOST_CHECK(counterSet->m_Name == counterSetRecord.name);
1454 }
1455
1456 // Event record structure/collection used for testing
1457 struct EventRecord
1458 {
1459 uint16_t counter_uid;
1460 uint16_t max_counter_uid;
1461 uint16_t device;
1462 uint16_t counter_set;
1463 uint16_t counter_class;
1464 uint16_t interpolation;
1465 double multiplier;
1466 uint32_t name_offset;
1467 uint32_t name_length;
1468 std::string name;
1469 uint32_t description_offset;
1470 uint32_t description_length;
1471 std::string description;
1472 uint32_t units_offset;
1473 uint32_t units_length;
1474 std::string units;
1475 };
1476 // Category record structure/collection used for testing
1477 struct CategoryRecord
1478 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001479 uint16_t event_count;
1480 uint32_t event_pointer_table_offset;
1481 uint32_t name_offset;
1482 uint32_t name_length;
1483 std::string name;
1484 std::vector<uint32_t> event_pointer_table;
1485 std::vector<EventRecord> event_records;
1486 };
1487 std::vector<CategoryRecord> categoryRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001488 const uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1489 bodyHeaderWord5; // categories_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001490
1491 offset = categoryRecordsPointerTableOffset;
1492 std::vector<uint32_t> categoryRecordOffsets(categoryRecordCount);
1493 for (uint32_t i = 0; i < categoryRecordCount; ++i)
1494 {
1495 // categoryRecordOffset is relative to the start of the categoryRecordsPointerTable
1496 categoryRecordOffsets[i] = ReadUint32(readBuffer, offset) + categoryRecordsPointerTableOffset;
1497 offset += uint32_t_size;
1498 }
1499
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001500 for (uint32_t i = 0; i < categoryRecordCount; i++)
1501 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001502 // Collect the data for the category record
Finn Williamsd44815f2020-05-01 13:25:55 +01001503 const uint32_t categoryRecordWord1 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 0 * uint32_t_size);
1504 const uint32_t categoryRecordWord2 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 1 * uint32_t_size);
1505 const uint32_t categoryRecordWord3 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 2 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001506 CategoryRecord categoryRecord;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001507 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1508 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1509 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1510
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001511 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001512 categoryRecordOffsets[i] + categoryRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001513 categoryRecord.name_length = categoryRecordNameLength; // name_length
1514 unsigned char categoryRecordNameNullTerminator =
1515 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001516 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001517 categoryRecord.name_offset +
1518 uint32_t_size +
1519 categoryRecordNameLength - 1); // name null-terminator
1520 BOOST_CHECK(categoryRecordNameNullTerminator == '\0');
1521 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1522 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001523 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001524 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001525 categoryRecord.name_offset +
1526 uint32_t_size,
1527 categoryRecordNameBuffer.size());
1528 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1529
1530 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
Finn Williamsd44815f2020-05-01 13:25:55 +01001531 offset = categoryRecordOffsets[i] + categoryRecord.event_pointer_table_offset;
1532 for (uint32_t eventOffsetIndex = 0; eventOffsetIndex < categoryRecord.event_count; ++eventOffsetIndex)
1533 {
1534 // eventRecordOffset is relative to the start of the event pointer table
1535 categoryRecord.event_pointer_table[eventOffsetIndex] = ReadUint32(readBuffer, offset) +
1536 categoryRecordOffsets[i] +
1537 categoryRecord.event_pointer_table_offset;
1538 offset += uint32_t_size;
1539 }
1540
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001541 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1542 {
Finn Williamsd44815f2020-05-01 13:25:55 +01001543 const uint32_t eventOffset = categoryRecord.event_pointer_table[eventIndex];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001544 // Collect the data for the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001545 const uint32_t eventRecordWord0 = ReadUint32(readBuffer, eventOffset + 0 * uint32_t_size);
1546 const uint32_t eventRecordWord1 = ReadUint32(readBuffer, eventOffset + 1 * uint32_t_size);
1547 const uint32_t eventRecordWord2 = ReadUint32(readBuffer, eventOffset + 2 * uint32_t_size);
1548 const uint64_t eventRecordWord34 = ReadUint64(readBuffer, eventOffset + 3 * uint32_t_size);
1549 const uint32_t eventRecordWord5 = ReadUint32(readBuffer, eventOffset + 5 * uint32_t_size);
1550 const uint32_t eventRecordWord6 = ReadUint32(readBuffer, eventOffset + 6 * uint32_t_size);
1551 const uint32_t eventRecordWord7 = ReadUint32(readBuffer, eventOffset + 7 * uint32_t_size);
1552
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001553 EventRecord eventRecord;
1554 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1555 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1556 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1557 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1558 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1559 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1560 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1561 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1562 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1563 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1564
Finn Williamsd44815f2020-05-01 13:25:55 +01001565 uint32_t eventRecordNameLength = ReadUint32(readBuffer, eventOffset + eventRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001566 eventRecord.name_length = eventRecordNameLength; // name_length
1567 unsigned char eventRecordNameNullTerminator =
1568 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001569 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001570 eventRecord.name_offset +
1571 uint32_t_size +
1572 eventRecordNameLength - 1); // name null-terminator
1573 BOOST_CHECK(eventRecordNameNullTerminator == '\0');
1574 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1575 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001576 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001577 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001578 eventRecord.name_offset +
1579 uint32_t_size,
1580 eventRecordNameBuffer.size());
1581 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1582
1583 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001584 eventOffset + eventRecord.description_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001585 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1586 unsigned char eventRecordDescriptionNullTerminator =
1587 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001588 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001589 eventRecord.description_offset +
1590 uint32_t_size +
1591 eventRecordDescriptionLength - 1); // description null-terminator
1592 BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0');
1593 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1594 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001595 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001596 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001597 eventRecord.description_offset +
1598 uint32_t_size,
1599 eventRecordDescriptionBuffer.size());
1600 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1601 eventRecordDescriptionBuffer.end()); // description
1602
1603 if (eventRecord.units_offset > 0)
1604 {
1605 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001606 eventOffset + eventRecord.units_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001607 eventRecord.units_length = eventRecordUnitsLength; // units_length
1608 unsigned char eventRecordUnitsNullTerminator =
1609 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001610 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001611 eventRecord.units_offset +
1612 uint32_t_size +
1613 eventRecordUnitsLength - 1); // units null-terminator
1614 BOOST_CHECK(eventRecordUnitsNullTerminator == '\0');
1615 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1616 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001617 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001618 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001619 eventRecord.units_offset +
1620 uint32_t_size,
1621 eventRecordUnitsBuffer.size());
1622 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1623 }
1624
1625 categoryRecord.event_records.push_back(eventRecord);
1626 }
1627
1628 categoryRecords.push_back(categoryRecord);
1629 }
1630
1631 // Check that the category records are correct
1632 BOOST_CHECK(categoryRecords.size() == 2);
1633 for (const CategoryRecord& categoryRecord : categoryRecords)
1634 {
1635 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
1636 BOOST_CHECK(category);
1637 BOOST_CHECK(category->m_Name == categoryRecord.name);
Keith Davis33ed2212020-03-30 10:43:41 +01001638 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
1639 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001640
1641 // Check that the event records are correct
1642 for (const EventRecord& eventRecord : categoryRecord.event_records)
1643 {
1644 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
1645 BOOST_CHECK(counter);
1646 BOOST_CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1647 BOOST_CHECK(counter->m_DeviceUid == eventRecord.device);
1648 BOOST_CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1649 BOOST_CHECK(counter->m_Class == eventRecord.counter_class);
1650 BOOST_CHECK(counter->m_Interpolation == eventRecord.interpolation);
1651 BOOST_CHECK(counter->m_Multiplier == eventRecord.multiplier);
1652 BOOST_CHECK(counter->m_Name == eventRecord.name);
1653 BOOST_CHECK(counter->m_Description == eventRecord.description);
1654 BOOST_CHECK(counter->m_Units == eventRecord.units);
1655 }
1656 }
1657}
1658
1659BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest3)
1660{
1661 // Using a mock counter directory that allows to register invalid objects
1662 MockCounterDirectory counterDirectory;
1663
1664 // Register an invalid device
1665 const std::string deviceName = "inv@lid dev!c€";
1666 const Device* device = nullptr;
1667 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1668 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1669 BOOST_CHECK(device);
1670
1671 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001672 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001673 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001674 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1675}
1676
1677BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest4)
1678{
1679 // Using a mock counter directory that allows to register invalid objects
1680 MockCounterDirectory counterDirectory;
1681
1682 // Register an invalid counter set
1683 const std::string counterSetName = "inv@lid count€rs€t";
1684 const CounterSet* counterSet = nullptr;
1685 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1686 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1687 BOOST_CHECK(counterSet);
1688
1689 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001690 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001691 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001692 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1693}
1694
1695BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest5)
1696{
1697 // Using a mock counter directory that allows to register invalid objects
1698 MockCounterDirectory counterDirectory;
1699
1700 // Register an invalid category
1701 const std::string categoryName = "c@t€gory";
1702 const Category* category = nullptr;
1703 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1704 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1705 BOOST_CHECK(category);
1706
1707 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001708 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001709 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001710 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1711}
1712
1713BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest6)
1714{
1715 // Using a mock counter directory that allows to register invalid objects
1716 MockCounterDirectory counterDirectory;
1717
1718 // Register an invalid device
1719 const std::string deviceName = "inv@lid dev!c€";
1720 const Device* device = nullptr;
1721 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1722 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1723 BOOST_CHECK(device);
1724
1725 // Register an invalid counter set
1726 const std::string counterSetName = "inv@lid count€rs€t";
1727 const CounterSet* counterSet = nullptr;
1728 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1729 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1730 BOOST_CHECK(counterSet);
1731
1732 // Register an invalid category associated to an invalid device and an invalid counter set
1733 const std::string categoryName = "c@t€gory";
1734 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001735 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001736 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1737 BOOST_CHECK(category);
1738
1739 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001740 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001741 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001742 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1743}
1744
1745BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest7)
1746{
1747 // Using a mock counter directory that allows to register invalid objects
1748 MockCounterDirectory counterDirectory;
1749
1750 // Register an valid device
1751 const std::string deviceName = "valid device";
1752 const Device* device = nullptr;
1753 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1754 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1755 BOOST_CHECK(device);
1756
1757 // Register an valid counter set
1758 const std::string counterSetName = "valid counterset";
1759 const CounterSet* counterSet = nullptr;
1760 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1761 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1762 BOOST_CHECK(counterSet);
1763
1764 // Register an valid category associated to a valid device and a valid counter set
1765 const std::string categoryName = "category";
1766 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001767 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001768 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1769 BOOST_CHECK(category);
1770
1771 // Register an invalid counter associated to a valid category
1772 const Counter* counter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001773 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1774 0,
1775 categoryName,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001776 0,
1777 1,
1778 123.45f,
1779 "counter",
1780 "counter description",
1781 std::string("invalid counter units"),
1782 5,
1783 device->m_Uid,
1784 counterSet->m_Uid));
1785 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1786 BOOST_CHECK(counter);
1787
1788 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001789 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001790 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001791 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1792}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001793
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001794BOOST_AUTO_TEST_CASE(SendThreadTest0)
1795{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001796 ProfilingStateMachine profilingStateMachine;
1797 SetActiveProfilingState(profilingStateMachine);
1798
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001799 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001800 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001801 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1802 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001803
1804 // Try to start the send thread many times, it must only start once
1805
Sadik Armagan3896b472020-02-10 12:24:15 +00001806 sendThread.Start(mockProfilingConnection);
1807 BOOST_CHECK(sendThread.IsRunning());
1808 sendThread.Start(mockProfilingConnection);
1809 sendThread.Start(mockProfilingConnection);
1810 sendThread.Start(mockProfilingConnection);
1811 sendThread.Start(mockProfilingConnection);
1812 BOOST_CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001813
Sadik Armagan3896b472020-02-10 12:24:15 +00001814 sendThread.Stop();
1815 BOOST_CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001816}
1817
1818BOOST_AUTO_TEST_CASE(SendThreadTest1)
1819{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001820 ProfilingStateMachine profilingStateMachine;
1821 SetActiveProfilingState(profilingStateMachine);
1822
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001823 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001824
1825 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001826 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001827 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1828 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1829 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001830
1831 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1832 // something to become available for reading
1833
Colm Donelan2ba48d22019-11-29 09:10:59 +00001834 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001835
1836 CounterDirectory counterDirectory;
1837 sendCounterPacket.SendStreamMetaDataPacket();
1838
Finn Williamsa0de0562020-04-22 12:27:37 +01001839 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001840
Sadik Armagan3896b472020-02-10 12:24:15 +00001841 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001842
Colm Donelan2ba48d22019-11-29 09:10:59 +00001843 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001844
1845 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1846
1847 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001848 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001849 totalWrittenSize += counterDirectoryPacketSize;
1850
Sadik Armagan3896b472020-02-10 12:24:15 +00001851 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001852
Colm Donelan2ba48d22019-11-29 09:10:59 +00001853 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001854
1855 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1856 {
1857 { 1u, 23u },
1858 { 33u, 1207623u }
1859 });
1860
1861 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001862 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001863 totalWrittenSize += periodicCounterCapturePacketSize;
1864
Sadik Armagan3896b472020-02-10 12:24:15 +00001865 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001866
Colm Donelan2ba48d22019-11-29 09:10:59 +00001867 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001868
1869 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1870 {
1871 { 211u, 923u }
1872 });
1873
1874 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001875 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001876 totalWrittenSize += periodicCounterCapturePacketSize;
1877
1878 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1879 {
1880 { 555u, 23u },
1881 { 556u, 6u },
1882 { 557u, 893454u },
1883 { 558u, 1456623u },
1884 { 559u, 571090u }
1885 });
1886
1887 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001888 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001889 totalWrittenSize += periodicCounterCapturePacketSize;
1890
1891 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1892 {
1893 { 88u, 11u },
1894 { 96u, 22u },
1895 { 97u, 33u },
1896 { 999u, 444u }
1897 });
1898
1899 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001900 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001901 totalWrittenSize += periodicCounterCapturePacketSize;
1902
Sadik Armagan3896b472020-02-10 12:24:15 +00001903 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001904
Colm Donelan2ba48d22019-11-29 09:10:59 +00001905 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001906
1907 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1908
1909 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001910 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001911 totalWrittenSize += periodicCounterCapturePacketSize;
1912
Sadik Armagan3896b472020-02-10 12:24:15 +00001913 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001914
Finn Williams109c05b2019-11-29 13:56:33 +00001915 // 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 +01001916 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001917 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001918
Sadik Armagan3896b472020-02-10 12:24:15 +00001919 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001920
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001921 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001922 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1923 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001924}
1925
1926BOOST_AUTO_TEST_CASE(SendThreadTest2)
1927{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001928 ProfilingStateMachine profilingStateMachine;
1929 SetActiveProfilingState(profilingStateMachine);
1930
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001931 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001932
1933 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001934 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001935 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1936 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1937 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001938
1939 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
1940 // capable of handling unnecessary read requests
1941
Colm Donelan2ba48d22019-11-29 09:10:59 +00001942 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001943
Sadik Armagan3896b472020-02-10 12:24:15 +00001944 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001945
1946 CounterDirectory counterDirectory;
1947 sendCounterPacket.SendStreamMetaDataPacket();
1948
Finn Williamsa0de0562020-04-22 12:27:37 +01001949 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001950
Sadik Armagan3896b472020-02-10 12:24:15 +00001951 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001952
Colm Donelan2ba48d22019-11-29 09:10:59 +00001953 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001954
1955 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1956
1957 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001958 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001959 totalWrittenSize += counterDirectoryPacketSize;
1960
Sadik Armagan3896b472020-02-10 12:24:15 +00001961 sendThread.SetReadyToRead();
1962 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001963
Colm Donelan2ba48d22019-11-29 09:10:59 +00001964 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001965
1966 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1967 {
1968 { 1u, 23u },
1969 { 33u, 1207623u }
1970 });
1971
1972 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001973 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001974 totalWrittenSize += periodicCounterCapturePacketSize;
1975
Sadik Armagan3896b472020-02-10 12:24:15 +00001976 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001977
Colm Donelan2ba48d22019-11-29 09:10:59 +00001978 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001979
Sadik Armagan3896b472020-02-10 12:24:15 +00001980 sendThread.SetReadyToRead();
1981 sendThread.SetReadyToRead();
1982 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001983
Colm Donelan2ba48d22019-11-29 09:10:59 +00001984 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001985
Sadik Armagan3896b472020-02-10 12:24:15 +00001986 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001987 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1988 {
1989 { 211u, 923u }
1990 });
1991
1992 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001993 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001994 totalWrittenSize += periodicCounterCapturePacketSize;
1995
1996 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1997 {
1998 { 555u, 23u },
1999 { 556u, 6u },
2000 { 557u, 893454u },
2001 { 558u, 1456623u },
2002 { 559u, 571090u }
2003 });
2004
2005 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002006 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002007 totalWrittenSize += periodicCounterCapturePacketSize;
2008
Sadik Armagan3896b472020-02-10 12:24:15 +00002009 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002010 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2011 {
2012 { 88u, 11u },
2013 { 96u, 22u },
2014 { 97u, 33u },
2015 { 999u, 444u }
2016 });
2017
2018 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002019 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002020 totalWrittenSize += periodicCounterCapturePacketSize;
2021
Sadik Armagan3896b472020-02-10 12:24:15 +00002022 sendThread.SetReadyToRead();
2023 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002024
Colm Donelan2ba48d22019-11-29 09:10:59 +00002025 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002026
2027 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2028
2029 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002030 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002031 totalWrittenSize += periodicCounterCapturePacketSize;
2032
Sadik Armagan3896b472020-02-10 12:24:15 +00002033 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002034
Finn Williams109c05b2019-11-29 13:56:33 +00002035 // 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 +01002036 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002037 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002038
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002039 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002040 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2041 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002042}
2043
2044BOOST_AUTO_TEST_CASE(SendThreadTest3)
2045{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002046 ProfilingStateMachine profilingStateMachine;
2047 SetActiveProfilingState(profilingStateMachine);
2048
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002049 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002050
2051 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002052 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002053 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
2054 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2055 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002056
2057 // Not using pauses or "grace periods" to stress test the send thread
2058
Sadik Armagan3896b472020-02-10 12:24:15 +00002059 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002060
2061 CounterDirectory counterDirectory;
2062 sendCounterPacket.SendStreamMetaDataPacket();
2063
Finn Williamsa0de0562020-04-22 12:27:37 +01002064 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002065
Sadik Armagan3896b472020-02-10 12:24:15 +00002066 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002067 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2068
2069 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002070 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002071 totalWrittenSize += counterDirectoryPacketSize;
2072
Sadik Armagan3896b472020-02-10 12:24:15 +00002073 sendThread.SetReadyToRead();
2074 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002075 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2076 {
2077 { 1u, 23u },
2078 { 33u, 1207623u }
2079 });
2080
2081 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002082 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002083 totalWrittenSize += periodicCounterCapturePacketSize;
2084
Sadik Armagan3896b472020-02-10 12:24:15 +00002085 sendThread.SetReadyToRead();
2086 sendThread.SetReadyToRead();
2087 sendThread.SetReadyToRead();
2088 sendThread.SetReadyToRead();
2089 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002090 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2091 {
2092 { 211u, 923u }
2093 });
2094
2095 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002096 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002097 totalWrittenSize += periodicCounterCapturePacketSize;
2098
2099 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2100 {
2101 { 555u, 23u },
2102 { 556u, 6u },
2103 { 557u, 893454u },
2104 { 558u, 1456623u },
2105 { 559u, 571090u }
2106 });
2107
2108 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002109 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002110 totalWrittenSize += periodicCounterCapturePacketSize;
2111
Sadik Armagan3896b472020-02-10 12:24:15 +00002112 sendThread.SetReadyToRead();
2113 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002114 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2115 {
2116 { 88u, 11u },
2117 { 96u, 22u },
2118 { 97u, 33u },
2119 { 999u, 444u }
2120 });
2121
2122 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002123 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002124 totalWrittenSize += periodicCounterCapturePacketSize;
2125
Sadik Armagan3896b472020-02-10 12:24:15 +00002126 sendThread.SetReadyToRead();
2127 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002128 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2129
2130 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002131 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002132 totalWrittenSize += periodicCounterCapturePacketSize;
2133
Sadik Armagan3896b472020-02-10 12:24:15 +00002134 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002135
2136 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2137 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002138 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002139
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002140 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2141 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2142 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2143 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2144 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002145}
2146
Sadik Armagan3896b472020-02-10 12:24:15 +00002147BOOST_AUTO_TEST_CASE(SendCounterPacketTestWithSendThread)
2148{
2149 ProfilingStateMachine profilingStateMachine;
2150 SetWaitingForAckProfilingState(profilingStateMachine);
2151
2152 MockProfilingConnection mockProfilingConnection;
2153 BufferManager bufferManager(1, 1024);
2154 SendCounterPacket sendCounterPacket(bufferManager);
2155 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2156 sendThread.Start(mockProfilingConnection);
2157
Finn Williamsa0de0562020-04-22 12:27:37 +01002158 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan3896b472020-02-10 12:24:15 +00002159
2160 sendThread.Stop();
2161
2162 // check for packet in ProfilingConnection
2163 BOOST_CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
2164
2165 SetActiveProfilingState(profilingStateMachine);
2166 sendThread.Start(mockProfilingConnection);
2167
2168 // SendCounterDirectoryPacket
2169 CounterDirectory counterDirectory;
2170 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2171
2172 sendThread.Stop();
2173 unsigned int counterDirectoryPacketSize = 32;
2174 // check for packet in ProfilingConnection
2175 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2176 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2177
2178 sendThread.Start(mockProfilingConnection);
2179
2180 // SendPeriodicCounterCapturePacket
2181 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2182 {
2183 { 1u, 23u },
2184 { 33u, 1207623u }
2185 });
2186
2187 sendThread.Stop();
2188
2189 unsigned int periodicCounterCapturePacketSize = 28;
2190 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2191 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2192}
2193
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002194BOOST_AUTO_TEST_CASE(SendThreadBufferTest)
2195{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002196 ProfilingStateMachine profilingStateMachine;
2197 SetActiveProfilingState(profilingStateMachine);
2198
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002199 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002200 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002201 SendCounterPacket sendCounterPacket(bufferManager);
2202 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2203 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002204
2205 // SendStreamMetaDataPacket
2206 sendCounterPacket.SendStreamMetaDataPacket();
2207
2208 // Read data from the buffer
2209 // Buffer should become readable after commit by SendStreamMetaDataPacket
2210 auto packetBuffer = bufferManager.GetReadableBuffer();
2211 BOOST_TEST(packetBuffer.get());
2212
Finn Williamsa0de0562020-04-22 12:27:37 +01002213 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002214 BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize);
2215
2216 // Recommit to be read by sendCounterPacket
2217 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2218
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002219 // SendCounterDirectoryPacket
2220 CounterDirectory counterDirectory;
2221 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2222
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002223 // SendPeriodicCounterCapturePacket
2224 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2225 {
2226 { 1u, 23u },
2227 { 33u, 1207623u }
2228 });
2229
Sadik Armagan3896b472020-02-10 12:24:15 +00002230 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002231
2232 // The buffer is read by the send thread so it should not be in the readable buffer.
2233 auto readBuffer = bufferManager.GetReadableBuffer();
2234 BOOST_TEST(!readBuffer);
2235
2236 // Successfully reserved the buffer with requested size
2237 unsigned int reservedSize = 0;
2238 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
2239 BOOST_TEST(reservedSize == 512);
2240 BOOST_TEST(reservedBuffer.get());
2241
Finn Williams09ad6f92019-12-19 17:05:18 +00002242 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2243 const auto metaDataPacketCount =
2244 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2245
2246 BOOST_TEST(metaDataPacketCount >= 1);
2247 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2248 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
2249 // Check that we only received the packets we expected
2250 BOOST_TEST(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002251}
2252
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002253BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket1)
2254{
2255 ProfilingStateMachine profilingStateMachine;
2256
2257 MockProfilingConnection mockProfilingConnection;
2258 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002259 SendCounterPacket sendCounterPacket(bufferManager);
2260 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2261 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002262
2263 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002264 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002265}
2266
2267BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket2)
2268{
2269 ProfilingStateMachine profilingStateMachine;
2270 SetNotConnectedProfilingState(profilingStateMachine);
2271
2272 MockProfilingConnection mockProfilingConnection;
2273 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002274 SendCounterPacket sendCounterPacket(bufferManager);
2275 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2276 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002277
2278 // The profiling state is set to "NotConnected", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002279 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002280}
2281
2282BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket3)
2283{
2284 ProfilingStateMachine profilingStateMachine;
2285 SetWaitingForAckProfilingState(profilingStateMachine);
2286
Finn Williamsa0de0562020-04-22 12:27:37 +01002287 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002288
2289 MockProfilingConnection mockProfilingConnection;
2290 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002291 SendCounterPacket sendCounterPacket(bufferManager);
2292 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2293 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002294
2295 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002296 // Wait for sendThread to join
2297 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002298
Finn Williams09ad6f92019-12-19 17:05:18 +00002299 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2300 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2301
Rob Hughesbb46dde2020-05-20 15:27:37 +01002302 BOOST_TEST(writtenDataSize >= 1u);
Finn Williams09ad6f92019-12-19 17:05:18 +00002303 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2304 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002305}
2306
2307BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket4)
2308{
2309 ProfilingStateMachine profilingStateMachine;
2310 SetWaitingForAckProfilingState(profilingStateMachine);
2311
Finn Williamsa0de0562020-04-22 12:27:37 +01002312 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002313
2314 MockProfilingConnection mockProfilingConnection;
2315 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002316 SendCounterPacket sendCounterPacket(bufferManager);
2317 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2318 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002319
2320 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002321 // Wait for sendThread to join
2322 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002323
Sadik Armagan3896b472020-02-10 12:24:15 +00002324 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002325 // Check that the profiling state is still "WaitingForAck"
2326 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2327
Finn Williams3e2969d2019-12-06 17:47:36 +00002328 // Check that the buffer contains at least one Stream Metadata packet
Finn Williams09ad6f92019-12-19 17:05:18 +00002329 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002330
2331 mockProfilingConnection.Clear();
2332
Sadik Armagan3896b472020-02-10 12:24:15 +00002333 sendThread.Stop();
2334 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002335
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002336 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002337 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002338
Sadik Armagan3896b472020-02-10 12:24:15 +00002339 // Wait for sendThread to join
2340 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002341
2342 // Check that the profiling state is still "WaitingForAck"
2343 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2344
Finn Williams09ad6f92019-12-19 17:05:18 +00002345 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2346 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2347
Rob Hughesbb46dde2020-05-20 15:27:37 +01002348 BOOST_TEST(writtenDataSize >= 1u);
Finn Williams09ad6f92019-12-19 17:05:18 +00002349 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2350 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002351}
2352
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01002353BOOST_AUTO_TEST_SUITE_END()