blob: 1380b42d376b2fd26691f3494ee4af98758cc7c2 [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
Sadik Armagan1625efc2021-06-10 18:24:34 +010023#include <armnn/utility/Assert.hpp>
24
25#include <doctest/doctest.h>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010026
Francis Murtagh3a161982019-09-04 15:25:02 +010027#include <chrono>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010028
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000029using namespace arm::pipe;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010030
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010031namespace
32{
33
Colm Donelan2ba48d22019-11-29 09:10:59 +000034// A short delay to wait for the thread to process a packet.
Finn Williams09ad6f92019-12-19 17:05:18 +000035uint16_t constexpr WAIT_UNTIL_READABLE_MS = 20;
Colm Donelan2ba48d22019-11-29 09:10:59 +000036
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010037void SetNotConnectedProfilingState(ProfilingStateMachine& profilingStateMachine)
38{
39 ProfilingState currentState = profilingStateMachine.GetCurrentState();
40 switch (currentState)
41 {
42 case ProfilingState::WaitingForAck:
43 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000044 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010045 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000046 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010047 case ProfilingState::Active:
48 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000049 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010050 case ProfilingState::NotConnected:
51 return;
52 default:
Sadik Armagan1625efc2021-06-10 18:24:34 +010053 CHECK_MESSAGE(false, "Invalid profiling state");
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010054 }
55}
56
57void SetWaitingForAckProfilingState(ProfilingStateMachine& profilingStateMachine)
58{
59 ProfilingState currentState = profilingStateMachine.GetCurrentState();
60 switch (currentState)
61 {
62 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000063 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010064 case ProfilingState::Active:
65 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000066 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010067 case ProfilingState::NotConnected:
68 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000069 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010070 case ProfilingState::WaitingForAck:
71 return;
72 default:
Sadik Armagan1625efc2021-06-10 18:24:34 +010073 CHECK_MESSAGE(false, "Invalid profiling state");
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010074 }
75}
76
77void SetActiveProfilingState(ProfilingStateMachine& profilingStateMachine)
78{
79 ProfilingState currentState = profilingStateMachine.GetCurrentState();
80 switch (currentState)
81 {
82 case ProfilingState::Uninitialised:
83 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000084 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010085 case ProfilingState::NotConnected:
86 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000087 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010088 case ProfilingState::WaitingForAck:
89 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000090 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010091 case ProfilingState::Active:
92 return;
93 default:
Sadik Armagan1625efc2021-06-10 18:24:34 +010094 CHECK_MESSAGE(false, "Invalid profiling state");
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010095 }
96}
97
98} // Anonymous namespace
99
Sadik Armagan1625efc2021-06-10 18:24:34 +0100100TEST_SUITE("SendCounterPacketTests")
101{
Finn Williams09ad6f92019-12-19 17:05:18 +0000102using PacketType = MockProfilingConnection::PacketType;
103
Sadik Armagan1625efc2021-06-10 18:24:34 +0100104TEST_CASE("MockSendCounterPacketTest")
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100105{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100106 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100107 MockSendCounterPacket mockSendCounterPacket(mockBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100108
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100109 mockSendCounterPacket.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100110
111 auto packetBuffer = mockBuffer.GetReadableBuffer();
112 const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100113
Sadik Armagan1625efc2021-06-10 18:24:34 +0100114 CHECK(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100115
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100116 mockBuffer.MarkRead(packetBuffer);
117
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100118 CounterDirectory counterDirectory;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100119 mockSendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100120
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100121 packetBuffer = mockBuffer.GetReadableBuffer();
122 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
123
Sadik Armagan1625efc2021-06-10 18:24:34 +0100124 CHECK(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100125
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100126 mockBuffer.MarkRead(packetBuffer);
127
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100128 uint64_t timestamp = 0;
Finn Williams032bc742020-02-12 11:02:34 +0000129 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100130
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100131 mockSendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100132
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100133 packetBuffer = mockBuffer.GetReadableBuffer();
134 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
135
Sadik Armagan1625efc2021-06-10 18:24:34 +0100136 CHECK(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100137
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100138 mockBuffer.MarkRead(packetBuffer);
139
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100140 uint32_t capturePeriod = 0;
141 std::vector<uint16_t> selectedCounterIds;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100142 mockSendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100143
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100144 packetBuffer = mockBuffer.GetReadableBuffer();
145 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
146
Sadik Armagan1625efc2021-06-10 18:24:34 +0100147 CHECK(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100148
149 mockBuffer.MarkRead(packetBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100150}
151
Sadik Armagan1625efc2021-06-10 18:24:34 +0100152TEST_CASE("SendPeriodicCounterSelectionPacketTest")
Ferran Balaguer73882172019-09-02 16:39:42 +0100153{
154 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100155 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000156 SendCounterPacket sendPacket1(mockBuffer1);
Ferran Balaguer73882172019-09-02 16:39:42 +0100157
158 uint32_t capturePeriod = 1000;
159 std::vector<uint16_t> selectedCounterIds;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100160 CHECK_THROWS_AS(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds),
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100161 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100162
163 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100164 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000165 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer73882172019-09-02 16:39:42 +0100166
167 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100168 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100169
170 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
171 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
172 uint32_t period = ReadUint32(readBuffer2, 8);
173
Sadik Armagan1625efc2021-06-10 18:24:34 +0100174 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
175 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
176 CHECK(headerWord1 == 4); // data lenght
177 CHECK(period == 1000); // capture period
Ferran Balaguer73882172019-09-02 16:39:42 +0100178
179 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100180 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000181 SendCounterPacket sendPacket3(mockBuffer3);
Ferran Balaguer73882172019-09-02 16:39:42 +0100182
183 selectedCounterIds.reserve(5);
184 selectedCounterIds.emplace_back(100);
185 selectedCounterIds.emplace_back(200);
186 selectedCounterIds.emplace_back(300);
187 selectedCounterIds.emplace_back(400);
188 selectedCounterIds.emplace_back(500);
189 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100190 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100191
192 headerWord0 = ReadUint32(readBuffer3, 0);
193 headerWord1 = ReadUint32(readBuffer3, 4);
194 period = ReadUint32(readBuffer3, 8);
195
Sadik Armagan1625efc2021-06-10 18:24:34 +0100196 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
197 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
198 CHECK(headerWord1 == 14); // data lenght
199 CHECK(period == 1000); // capture period
Ferran Balaguer73882172019-09-02 16:39:42 +0100200
201 uint16_t counterId = 0;
202 uint32_t offset = 12;
203
204 // Counter Ids
205 for(const uint16_t& id : selectedCounterIds)
206 {
207 counterId = ReadUint16(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100208 CHECK(counterId == id);
Ferran Balaguer73882172019-09-02 16:39:42 +0100209 offset += 2;
210 }
211}
212
Sadik Armagan1625efc2021-06-10 18:24:34 +0100213TEST_CASE("SendPeriodicCounterCapturePacketTest")
Francis Murtagh3a161982019-09-04 15:25:02 +0100214{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100215 ProfilingStateMachine profilingStateMachine;
216
Francis Murtagh3a161982019-09-04 15:25:02 +0100217 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100218 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000219 SendCounterPacket sendPacket1(mockBuffer1);
Francis Murtagh3a161982019-09-04 15:25:02 +0100220
221 auto captureTimestamp = std::chrono::steady_clock::now();
222 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
Finn Williams032bc742020-02-12 11:02:34 +0000223 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100224
Sadik Armagan1625efc2021-06-10 18:24:34 +0100225 CHECK_THROWS_AS(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
Francis Murtagh3a161982019-09-04 15:25:02 +0100226 BufferExhaustion);
227
228 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100229 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000230 SendCounterPacket sendPacket2(mockBuffer2);
Francis Murtagh3a161982019-09-04 15:25:02 +0100231
232 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100233 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100234
235 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
236 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
237 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
238
Sadik Armagan1625efc2021-06-10 18:24:34 +0100239 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
240 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
241 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
242 CHECK(headerWord1 == 8); // data length
243 CHECK(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100244
245 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100246 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000247 SendCounterPacket sendPacket3(mockBuffer3);
Francis Murtagh3a161982019-09-04 15:25:02 +0100248
249 indexValuePairs.reserve(5);
Finn Williams032bc742020-02-12 11:02:34 +0000250 indexValuePairs.emplace_back(CounterValue{0, 100});
251 indexValuePairs.emplace_back(CounterValue{1, 200});
252 indexValuePairs.emplace_back(CounterValue{2, 300});
253 indexValuePairs.emplace_back(CounterValue{3, 400});
254 indexValuePairs.emplace_back(CounterValue{4, 500});
Francis Murtagh3a161982019-09-04 15:25:02 +0100255 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100256 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100257
258 headerWord0 = ReadUint32(readBuffer3, 0);
259 headerWord1 = ReadUint32(readBuffer3, 4);
260 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
261
Sadik Armagan1625efc2021-06-10 18:24:34 +0100262 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
263 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
264 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
265 CHECK(headerWord1 == 38); // data length
266 CHECK(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100267
268 uint16_t counterIndex = 0;
269 uint32_t counterValue = 100;
270 uint32_t offset = 16;
271
272 // Counter Ids
273 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
274 {
275 // Check Counter Index
276 uint16_t readIndex = ReadUint16(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100277 CHECK(counterIndex == readIndex);
Francis Murtagh3a161982019-09-04 15:25:02 +0100278 counterIndex++;
279 offset += 2;
280
281 // Check Counter Value
282 uint32_t readValue = ReadUint32(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100283 CHECK(counterValue == readValue);
Francis Murtagh3a161982019-09-04 15:25:02 +0100284 counterValue += 100;
285 offset += 4;
286 }
287
288}
289
Sadik Armagan1625efc2021-06-10 18:24:34 +0100290TEST_CASE("SendStreamMetaDataPacketTest")
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100291{
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100292 uint32_t sizeUint32 = armnn::numeric_cast<uint32_t>(sizeof(uint32_t));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100293
294 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100295 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000296 SendCounterPacket sendPacket1(mockBuffer1);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000297 CHECK_THROWS_AS(sendPacket1.SendStreamMetaDataPacket(), BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100298
299 // Full metadata packet
300
301 std::string processName = GetProcessName().substr(0, 60);
302
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100303 uint32_t infoSize = armnn::numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1;
304 uint32_t hardwareVersionSize = armnn::numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1;
305 uint32_t softwareVersionSize = armnn::numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1;
306 uint32_t processNameSize = armnn::numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100307
Jim Flynn83d08a92020-07-09 13:48:16 +0100308 // Supported Packets
309 // Packet Encoding version 1.0.0
310 // Control packet family
311 // Stream metadata packet (packet family=0; packet id=0)
312 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
313 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
314 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
315 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
316 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
317 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
318 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
319 // Counter Packet Family
320 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
321 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
322 // Timeline Packet Family
323 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
324 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
325 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100326 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
327 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
328 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
329 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
330 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
331 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
332 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
333 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
334 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
335 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
336 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
337 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
338 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Jim Flynn83d08a92020-07-09 13:48:16 +0100339
340 uint32_t packetEntries = static_cast<uint32_t>(packetVersions.size());
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100341
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100342 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000343 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100344 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100345 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100346
347 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
348 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
349
Sadik Armagan1625efc2021-06-10 18:24:34 +0100350 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
351 CHECK(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100352
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100353 uint32_t totalLength = armnn::numeric_cast<uint32_t>(2 * sizeUint32 +
354 10 * sizeUint32 + infoSize +
355 hardwareVersionSize + softwareVersionSize +
356 processNameSize + sizeUint32 +
357 2 * packetEntries * sizeUint32);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100358
Sadik Armagan1625efc2021-06-10 18:24:34 +0100359 CHECK(headerWord1 == totalLength - (2 * sizeUint32)); // data length
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100360
361 uint32_t offset = sizeUint32 * 2;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100362 CHECK(ReadUint32(readBuffer2, offset) == arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100363 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100364 CHECK(ReadUint32(readBuffer2, offset) == arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100365 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100366 CHECK(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100367 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000368 int pid = armnnUtils::Processes::GetCurrentId();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100369 CHECK(ReadUint32(readBuffer2, offset) == armnn::numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100370 offset += sizeUint32;
371 uint32_t poolOffset = 10 * sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100372 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100373 offset += sizeUint32;
374 poolOffset += infoSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100375 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100376 offset += sizeUint32;
377 poolOffset += hardwareVersionSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100378 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100379 offset += sizeUint32;
380 poolOffset += softwareVersionSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100381 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100382 offset += sizeUint32;
383 poolOffset += processNameSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100384 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100385 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100386 CHECK(ReadUint32(readBuffer2, offset) == 0); // reserved
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100387
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100388 const unsigned char* readData2 = readBuffer2->GetReadableData();
389
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100390 offset += sizeUint32;
391 if (infoSize)
392 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100393 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100394 offset += infoSize;
395 }
396
397 if (hardwareVersionSize)
398 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100399 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100400 offset += hardwareVersionSize;
401 }
402
403 if (softwareVersionSize)
404 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100405 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100406 offset += softwareVersionSize;
407 }
408
409 if (processNameSize)
410 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100411 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100412 offset += processNameSize;
413 }
414
415 if (packetEntries)
416 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100417 uint32_t numberOfEntries = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100418 CHECK((numberOfEntries >> 16) == packetEntries);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100419 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100420 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100421 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100422 uint32_t readPacketId = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100423 CHECK(packetVersion.first == readPacketId);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100424 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100425 uint32_t readVersion = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100426 CHECK(packetVersion.second == readVersion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100427 offset += sizeUint32;
428 }
429 }
430
Sadik Armagan1625efc2021-06-10 18:24:34 +0100431 CHECK(offset == totalLength);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100432}
433
Sadik Armagan1625efc2021-06-10 18:24:34 +0100434TEST_CASE("CreateDeviceRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100435{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100436 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000437 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100438
439 // Create a device for testing
440 uint16_t deviceUid = 27;
441 const std::string deviceName = "some_device";
442 uint16_t deviceCores = 3;
443 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
444
445 // Create a device record
446 SendCounterPacket::DeviceRecord deviceRecord;
447 std::string errorMessage;
448 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
449
Sadik Armagan1625efc2021-06-10 18:24:34 +0100450 CHECK(result);
451 CHECK(errorMessage.empty());
452 CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100453
454 uint16_t deviceRecordWord0[]
455 {
456 static_cast<uint16_t>(deviceRecord[0] >> 16),
457 static_cast<uint16_t>(deviceRecord[0])
458 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100459 CHECK(deviceRecordWord0[0] == deviceUid); // uid
460 CHECK(deviceRecordWord0[1] == deviceCores); // cores
461 CHECK(deviceRecord[1] == 8); // name_offset
462 CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
463 CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100464}
465
Sadik Armagan1625efc2021-06-10 18:24:34 +0100466TEST_CASE("CreateInvalidDeviceRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100467{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100468 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000469 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100470
471 // Create a device for testing
472 uint16_t deviceUid = 27;
473 const std::string deviceName = "some€£invalid‡device";
474 uint16_t deviceCores = 3;
475 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
476
477 // Create a device record
478 SendCounterPacket::DeviceRecord deviceRecord;
479 std::string errorMessage;
480 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
481
Sadik Armagan1625efc2021-06-10 18:24:34 +0100482 CHECK(!result);
483 CHECK(!errorMessage.empty());
484 CHECK(deviceRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100485}
486
Sadik Armagan1625efc2021-06-10 18:24:34 +0100487TEST_CASE("CreateCounterSetRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100488{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100489 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000490 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100491
492 // Create a counter set for testing
493 uint16_t counterSetUid = 27;
494 const std::string counterSetName = "some_counter_set";
495 uint16_t counterSetCount = 3421;
496 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
497
498 // Create a counter set record
499 SendCounterPacket::CounterSetRecord counterSetRecord;
500 std::string errorMessage;
501 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
502
Sadik Armagan1625efc2021-06-10 18:24:34 +0100503 CHECK(result);
504 CHECK(errorMessage.empty());
505 CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100506
507 uint16_t counterSetRecordWord0[]
508 {
509 static_cast<uint16_t>(counterSetRecord[0] >> 16),
510 static_cast<uint16_t>(counterSetRecord[0])
511 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100512 CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
513 CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
514 CHECK(counterSetRecord[1] == 8); // name_offset
515 CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
516 CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100517}
518
Sadik Armagan1625efc2021-06-10 18:24:34 +0100519TEST_CASE("CreateInvalidCounterSetRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100520{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100521 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000522 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100523
524 // Create a counter set for testing
525 uint16_t counterSetUid = 27;
526 const std::string counterSetName = "some invalid_counter€£set";
527 uint16_t counterSetCount = 3421;
528 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
529
530 // Create a counter set record
531 SendCounterPacket::CounterSetRecord counterSetRecord;
532 std::string errorMessage;
533 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
534
Sadik Armagan1625efc2021-06-10 18:24:34 +0100535 CHECK(!result);
536 CHECK(!errorMessage.empty());
537 CHECK(counterSetRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100538}
539
Sadik Armagan1625efc2021-06-10 18:24:34 +0100540TEST_CASE("CreateEventRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100541{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100542 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000543 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100544
545 // Create a counter for testing
546 uint16_t counterUid = 7256;
547 uint16_t maxCounterUid = 132;
548 uint16_t deviceUid = 132;
549 uint16_t counterSetUid = 4497;
550 uint16_t counterClass = 1;
551 uint16_t counterInterpolation = 1;
552 double counterMultiplier = 1234.567f;
553 const std::string counterName = "some_valid_counter";
554 const std::string counterDescription = "a_counter_for_testing";
555 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000556 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
557 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100558 maxCounterUid,
559 counterClass,
560 counterInterpolation,
561 counterMultiplier,
562 counterName,
563 counterDescription,
564 counterUnits,
565 deviceUid,
566 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100567 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100568
569 // Create an event record
570 SendCounterPacket::EventRecord eventRecord;
571 std::string errorMessage;
572 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
573
Sadik Armagan1625efc2021-06-10 18:24:34 +0100574 CHECK(result);
575 CHECK(errorMessage.empty());
576 CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100577
578 uint16_t eventRecordWord0[]
579 {
580 static_cast<uint16_t>(eventRecord[0] >> 16),
581 static_cast<uint16_t>(eventRecord[0])
582 };
583 uint16_t eventRecordWord1[]
584 {
585 static_cast<uint16_t>(eventRecord[1] >> 16),
586 static_cast<uint16_t>(eventRecord[1])
587 };
588 uint16_t eventRecordWord2[]
589 {
590 static_cast<uint16_t>(eventRecord[2] >> 16),
591 static_cast<uint16_t>(eventRecord[2])
592 };
593 uint32_t eventRecordWord34[]
594 {
595 eventRecord[3],
596 eventRecord[4]
597 };
Finn Williamsd44815f2020-05-01 13:25:55 +0100598
Sadik Armagan1625efc2021-06-10 18:24:34 +0100599 CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
600 CHECK(eventRecordWord0[1] == counterUid); // counter_uid
601 CHECK(eventRecordWord1[0] == deviceUid); // device
Finn Williamsd44815f2020-05-01 13:25:55 +0100602
Sadik Armagan1625efc2021-06-10 18:24:34 +0100603 CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
604 CHECK(eventRecordWord2[0] == counterClass); // class
605 CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
606 CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100607
608 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100609 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
610 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100611 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
612 4u + // Counter name length (uint32_t)
613 counterName.size() + // 18u
614 1u + // Null-terminator
615 1u; // Rounding to the next word
Finn Williamsd44815f2020-05-01 13:25:55 +0100616
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100617 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
618 4u + // Counter description length (uint32_t)
619 counterDescription.size() + // 21u
620 1u + // Null-terminator
Finn Williamsd44815f2020-05-01 13:25:55 +0100621 2u; // Rounding to the next word
622
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100623 ARMNN_NO_CONVERSION_WARN_END
624
Sadik Armagan1625efc2021-06-10 18:24:34 +0100625 CHECK(eventRecord[5] == counterNameOffset); // name_offset
626 CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
627 CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100628
Finn Williamsd44815f2020-05-01 13:25:55 +0100629 // Offsets are relative to the start of the eventRecord
630 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100631 size_t uint32_t_size = sizeof(uint32_t);
632
633 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100634 CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100635 // The counter name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100636 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100637 counterNameOffset + // Offset
638 uint32_t_size /* The length of the name */,
639 counterName.data(),
640 counterName.size()) == 0); // name
641 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100642 CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100643
644 // The length of the SWTrace string (description)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100645 CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100646 // The counter description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100647 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100648 counterDescriptionOffset + // Offset
649 uint32_t_size /* The length of the description */,
650 counterDescription.data(),
651 counterDescription.size()) == 0); // description
652 // The null-terminator at the end of the description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100653 CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100654
655 // The length of the SWTrace namestring (units)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100656 CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100657 // The counter units
Sadik Armagan1625efc2021-06-10 18:24:34 +0100658 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100659 counterUnitsOffset + // Offset
660 uint32_t_size /* The length of the units */,
661 counterUnits.data(),
662 counterUnits.size()) == 0); // units
663 // The null-terminator at the end of the units
Sadik Armagan1625efc2021-06-10 18:24:34 +0100664 CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100665}
666
Sadik Armagan1625efc2021-06-10 18:24:34 +0100667TEST_CASE("CreateEventRecordNoUnitsTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100668{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100669 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000670 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100671
672 // Create a counter for testing
673 uint16_t counterUid = 44312;
674 uint16_t maxCounterUid = 345;
675 uint16_t deviceUid = 101;
676 uint16_t counterSetUid = 34035;
677 uint16_t counterClass = 0;
678 uint16_t counterInterpolation = 1;
679 double counterMultiplier = 4435.0023f;
680 const std::string counterName = "some_valid_counter";
681 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000682 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
683 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100684 maxCounterUid,
685 counterClass,
686 counterInterpolation,
687 counterMultiplier,
688 counterName,
689 counterDescription,
690 "",
691 deviceUid,
692 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100693 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100694
695 // Create an event record
696 SendCounterPacket::EventRecord eventRecord;
697 std::string errorMessage;
698 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
699
Sadik Armagan1625efc2021-06-10 18:24:34 +0100700 CHECK(result);
701 CHECK(errorMessage.empty());
702 CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100703
704 uint16_t eventRecordWord0[]
705 {
706 static_cast<uint16_t>(eventRecord[0] >> 16),
707 static_cast<uint16_t>(eventRecord[0])
708 };
709 uint16_t eventRecordWord1[]
710 {
711 static_cast<uint16_t>(eventRecord[1] >> 16),
712 static_cast<uint16_t>(eventRecord[1])
713 };
714 uint16_t eventRecordWord2[]
715 {
716 static_cast<uint16_t>(eventRecord[2] >> 16),
717 static_cast<uint16_t>(eventRecord[2])
718 };
719 uint32_t eventRecordWord34[]
720 {
721 eventRecord[3],
722 eventRecord[4]
723 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100724 CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
725 CHECK(eventRecordWord0[1] == counterUid); // counter_uid
726 CHECK(eventRecordWord1[0] == deviceUid); // device
727 CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
728 CHECK(eventRecordWord2[0] == counterClass); // class
729 CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
730 CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100731
732 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100733 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
734 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100735 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
736 4u + // Counter name length (uint32_t)
737 counterName.size() + // 18u
738 1u + // Null-terminator
739 1u; // Rounding to the next word
740 ARMNN_NO_CONVERSION_WARN_END
741
Sadik Armagan1625efc2021-06-10 18:24:34 +0100742 CHECK(eventRecord[5] == counterNameOffset); // name_offset
743 CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
744 CHECK(eventRecord[7] == 0); // units_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100745
Finn Williamsd44815f2020-05-01 13:25:55 +0100746 // Offsets are relative to the start of the eventRecord
747 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100748 size_t uint32_t_size = sizeof(uint32_t);
749
750 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100751 CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100752 // The counter name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100753 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100754 counterNameOffset + // Offset
755 uint32_t_size, // The length of the name
756 counterName.data(),
757 counterName.size()) == 0); // name
758 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100759 CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100760
761 // The length of the SWTrace string (description)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100762 CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100763 // The counter description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100764 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100765 counterDescriptionOffset + // Offset
766 uint32_t_size, // The length of the description
767 counterDescription.data(),
768 counterDescription.size()) == 0); // description
769 // The null-terminator at the end of the description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100770 CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100771}
772
Sadik Armagan1625efc2021-06-10 18:24:34 +0100773TEST_CASE("CreateInvalidEventRecordTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100774{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100775 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000776 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100777
778 // Create a counter for testing
779 uint16_t counterUid = 7256;
780 uint16_t maxCounterUid = 132;
781 uint16_t deviceUid = 132;
782 uint16_t counterSetUid = 4497;
783 uint16_t counterClass = 1;
784 uint16_t counterInterpolation = 1;
785 double counterMultiplier = 1234.567f;
786 const std::string counterName = "some_invalid_counter £££"; // Invalid name
787 const std::string counterDescription = "a_counter_for_testing";
788 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000789 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
790 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791 maxCounterUid,
792 counterClass,
793 counterInterpolation,
794 counterMultiplier,
795 counterName,
796 counterDescription,
797 counterUnits,
798 deviceUid,
799 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100800 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100801
802 // Create an event record
803 SendCounterPacket::EventRecord eventRecord;
804 std::string errorMessage;
805 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
806
Sadik Armagan1625efc2021-06-10 18:24:34 +0100807 CHECK(!result);
808 CHECK(!errorMessage.empty());
809 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100810}
811
Sadik Armagan1625efc2021-06-10 18:24:34 +0100812TEST_CASE("CreateInvalidEventRecordTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100813{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100814 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000815 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100816
817 // Create a counter for testing
818 uint16_t counterUid = 7256;
819 uint16_t maxCounterUid = 132;
820 uint16_t deviceUid = 132;
821 uint16_t counterSetUid = 4497;
822 uint16_t counterClass = 1;
823 uint16_t counterInterpolation = 1;
824 double counterMultiplier = 1234.567f;
825 const std::string counterName = "some_invalid_counter";
826 const std::string counterDescription = "an invalid d€scription"; // Invalid description
827 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000828 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
829 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100830 maxCounterUid,
831 counterClass,
832 counterInterpolation,
833 counterMultiplier,
834 counterName,
835 counterDescription,
836 counterUnits,
837 deviceUid,
838 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100839 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100840
841 // Create an event record
842 SendCounterPacket::EventRecord eventRecord;
843 std::string errorMessage;
844 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
845
Sadik Armagan1625efc2021-06-10 18:24:34 +0100846 CHECK(!result);
847 CHECK(!errorMessage.empty());
848 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100849}
850
Sadik Armagan1625efc2021-06-10 18:24:34 +0100851TEST_CASE("CreateInvalidEventRecordTest3")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100852{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100853 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000854 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100855
856 // Create a counter for testing
857 uint16_t counterUid = 7256;
858 uint16_t maxCounterUid = 132;
859 uint16_t deviceUid = 132;
860 uint16_t counterSetUid = 4497;
861 uint16_t counterClass = 1;
862 uint16_t counterInterpolation = 1;
863 double counterMultiplier = 1234.567f;
864 const std::string counterName = "some_invalid_counter";
865 const std::string counterDescription = "a valid description";
866 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000867 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
868 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100869 maxCounterUid,
870 counterClass,
871 counterInterpolation,
872 counterMultiplier,
873 counterName,
874 counterDescription,
875 counterUnits,
876 deviceUid,
877 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100878 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100879
880 // Create an event record
881 SendCounterPacket::EventRecord eventRecord;
882 std::string errorMessage;
883 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
884
Sadik Armagan1625efc2021-06-10 18:24:34 +0100885 CHECK(!result);
886 CHECK(!errorMessage.empty());
887 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100888}
889
Sadik Armagan1625efc2021-06-10 18:24:34 +0100890TEST_CASE("CreateCategoryRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100891{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100892 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000893 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100894
895 // Create a category for testing
896 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +0000897 const CategoryPtr category = std::make_unique<Category>(categoryName);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100898 ARMNN_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100899 category->m_Counters = { 11u, 23u, 5670u };
900
901 // Create a collection of counters
902 Counters counters;
903 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000904 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100905 0,
Keith Davise394bd92019-12-02 15:12:19 +0000906 11,
907 0,
908 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100909 534.0003f,
910 "counter1",
911 "the first counter",
912 "millipi2",
913 0,
914 0))));
915 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000916 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100917 1,
Keith Davise394bd92019-12-02 15:12:19 +0000918 23,
919 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100920 1,
921 534.0003f,
922 "this is counter 2",
923 "the second counter",
924 "",
925 0,
926 0))));
927 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000928 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
929 2,
930 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100931 0,
932 0,
933 534.0003f,
934 "and this is number 3",
935 "the third counter",
936 "blah_per_second",
937 0,
938 0))));
939 Counter* counter1 = counters.find(11)->second.get();
940 Counter* counter2 = counters.find(23)->second.get();
941 Counter* counter3 = counters.find(5670)->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100942 ARMNN_ASSERT(counter1);
943 ARMNN_ASSERT(counter2);
944 ARMNN_ASSERT(counter3);
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100945 uint16_t categoryEventCount = armnn::numeric_cast<uint16_t>(counters.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100946
947 // Create a category record
948 SendCounterPacket::CategoryRecord categoryRecord;
949 std::string errorMessage;
950 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
951
Sadik Armagan1625efc2021-06-10 18:24:34 +0100952 CHECK(result);
953 CHECK(errorMessage.empty());
954 CHECK(categoryRecord.size() == 79); // Size in words: header [3] + event pointer table [3] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100955 // category name [5] + event records [68 = 22 + 20 + 26]
956
Sadik Armagan4c998992020-02-25 12:44:44 +0000957 uint16_t categoryRecordWord1[]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100958 {
959 static_cast<uint16_t>(categoryRecord[0] >> 16),
960 static_cast<uint16_t>(categoryRecord[0])
961 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100962 CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
963 CHECK(categoryRecordWord1[1] == 0); // reserved
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100964
965 size_t uint32_t_size = sizeof(uint32_t);
966
967 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100968 uint32_t categoryRecordBlockSize = 3u * uint32_t_size;
969 uint32_t eventPointerTableOffset = categoryRecordBlockSize; // The event pointer table is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100970 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
971 categoryEventCount * uint32_t_size; // The size of the event pointer table
972 ARMNN_NO_CONVERSION_WARN_END
973
Sadik Armagan1625efc2021-06-10 18:24:34 +0100974 CHECK(categoryRecord[1] == eventPointerTableOffset); // event_pointer_table_offset
975 CHECK(categoryRecord[2] == categoryNameOffset); // name_offset
Finn Williamsd44815f2020-05-01 13:25:55 +0100976 // Offsets are relative to the start of the category record
977 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100978
979 // The event pointer table
980 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
981 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
982 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
Sadik Armagan1625efc2021-06-10 18:24:34 +0100983 CHECK(eventRecord0Offset == 32);
984 CHECK(eventRecord1Offset == 120);
985 CHECK(eventRecord2Offset == 200);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100986
987 // The length of the SWTrace namestring (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100988 CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100989 // The category name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100990 CHECK(std::memcmp(categoryRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100991 categoryNameOffset + // Offset
992 uint32_t_size, // The length of the name
993 categoryName.data(),
994 categoryName.size()) == 0); // name
995 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100996 CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100997
998 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
999 // as the event records already have a number of unit tests dedicated to them
1000
1001 // Counter1 UID and max counter UID
1002 uint16_t eventRecord0Word0[2] = { 0u, 0u };
Finn Williamsd44815f2020-05-01 13:25:55 +01001003 std::memcpy(eventRecord0Word0, categoryRecordPool + categoryRecordBlockSize + eventRecord0Offset,
1004 sizeof(eventRecord0Word0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001005 CHECK(eventRecord0Word0[0] == counter1->m_Uid);
1006 CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001007
1008 // Counter1 name
1009 uint32_t counter1NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001010 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001011 CHECK(counter1NameOffset == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001012 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +01001013 CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001014 categoryRecordBlockSize + // Offset to the end of the category record block
1015 8u * uint32_t_size + // Offset to the event record pool
1016 counter1NameOffset // Offset to the name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001017 ] == counter1->m_Name.size() + 1); // The length of the name including the
1018 // null-terminator
1019 // The counter1 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001020 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001021 categoryRecordBlockSize + // Offset to the end of the category record block
1022 eventRecord0Offset + // Offset to the event record
1023 8u * uint32_t_size + // Offset to the event record pool
1024 counter1NameOffset + // Offset to the name of the counter
1025 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001026 counter1->m_Name.data(),
1027 counter1->m_Name.size()) == 0); // name
1028 // The null-terminator at the end of the counter1 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001029 CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001030 categoryRecordBlockSize + // Offset to the end of the category record block
1031 8u * uint32_t_size + // Offset to the event record pool
1032 counter1NameOffset + // Offset to the name of the counter
1033 uint32_t_size + // The length of the name
1034 counter1->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001035 ] == '\0');
1036
1037 // Counter2 name
1038 uint32_t counter2NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001039 std::memcpy(&counter2NameOffset, categoryRecordPool +
1040 categoryRecordBlockSize +
1041 eventRecord1Offset +
1042 5u * uint32_t_size,
1043 uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001044 CHECK(counter2NameOffset == 8u * uint32_t_size );
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001045 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001046
Sadik Armagan1625efc2021-06-10 18:24:34 +01001047 CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001048 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001049 counter2NameOffset // Offset to the name of the counter
1050 ] == counter2->m_Name.size() + 1); // The length of the name including the
1051 // null-terminator
1052 // The counter2 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001053 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001054 categoryRecordBlockSize + // Offset to the end of the category record block
1055 eventRecord1Offset + // Offset to the event record
1056 counter2NameOffset + // Offset to the name of the counter
1057 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001058 counter2->m_Name.data(),
1059 counter2->m_Name.size()) == 0); // name
Finn Williamsd44815f2020-05-01 13:25:55 +01001060
1061
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001062 // The null-terminator at the end of the counter2 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001063 CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001064 categoryRecordBlockSize + // Offset to the end of the category record block
1065 counter2NameOffset + // Offset to the name of the counter
1066 uint32_t_size + // The length of the name
1067 counter2->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001068 ] == '\0');
1069
1070 // Counter3 name
1071 uint32_t counter3NameOffset = 0;
1072 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001073 CHECK(counter3NameOffset == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001074 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +01001075 CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001076 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001077 8u * uint32_t_size + // Offset to the event record pool
1078 counter3NameOffset // Offset to the name of the counter
1079 ] == counter3->m_Name.size() + 1); // The length of the name including the
1080 // null-terminator
1081 // The counter3 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001082 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001083 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001084 eventRecord2Offset + // Offset to the event record
1085 8u * uint32_t_size + // Offset to the event record pool
1086 counter3NameOffset + // Offset to the name of the counter
1087 uint32_t_size, // The length of the name
1088 counter3->m_Name.data(),
1089 counter3->m_Name.size()) == 0); // name
1090 // The null-terminator at the end of the counter3 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001091 CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001092 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001093 8u * uint32_t_size + // Offset to the event record pool
1094 counter3NameOffset + // Offset to the name of the counter
1095 uint32_t_size + // The length of the name
1096 counter3->m_Name.size() // The name of the counter
1097 ] == '\0');
1098}
1099
Sadik Armagan1625efc2021-06-10 18:24:34 +01001100TEST_CASE("CreateInvalidCategoryRecordTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001101{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001102 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001103 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001104
1105 // Create a category for testing
1106 const std::string categoryName = "some invalid category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001107 const CategoryPtr category = std::make_unique<Category>(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001108 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001109
1110 // Create a category record
1111 Counters counters;
1112 SendCounterPacket::CategoryRecord categoryRecord;
1113 std::string errorMessage;
1114 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1115
Sadik Armagan1625efc2021-06-10 18:24:34 +01001116 CHECK(!result);
1117 CHECK(!errorMessage.empty());
1118 CHECK(categoryRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001119}
1120
Sadik Armagan1625efc2021-06-10 18:24:34 +01001121TEST_CASE("CreateInvalidCategoryRecordTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001122{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001123 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001124 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001125
1126 // Create a category for testing
1127 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001128 const CategoryPtr category = std::make_unique<Category>(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001129 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001130 category->m_Counters = { 11u, 23u, 5670u };
1131
1132 // Create a collection of counters
1133 Counters counters;
1134 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001135 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1136 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001137 1234,
1138 0,
1139 1,
1140 534.0003f,
1141 "count€r1", // Invalid name
1142 "the first counter",
1143 "millipi2",
1144 0,
1145 0))));
1146
1147 Counter* counter1 = counters.find(11)->second.get();
Sadik Armagan1625efc2021-06-10 18:24:34 +01001148 CHECK(counter1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001149
1150 // Create a category record
1151 SendCounterPacket::CategoryRecord categoryRecord;
1152 std::string errorMessage;
1153 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1154
Sadik Armagan1625efc2021-06-10 18:24:34 +01001155 CHECK(!result);
1156 CHECK(!errorMessage.empty());
1157 CHECK(categoryRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001158}
1159
Sadik Armagan1625efc2021-06-10 18:24:34 +01001160TEST_CASE("SendCounterDirectoryPacketTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001161{
1162 // The counter directory used for testing
1163 CounterDirectory counterDirectory;
1164
1165 // Register a device
1166 const std::string device1Name = "device1";
1167 const Device* device1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001168 CHECK_NOTHROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1169 CHECK(counterDirectory.GetDeviceCount() == 1);
1170 CHECK(device1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001171
1172 // Register a device
1173 const std::string device2Name = "device2";
1174 const Device* device2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001175 CHECK_NOTHROW(device2 = counterDirectory.RegisterDevice(device2Name));
1176 CHECK(counterDirectory.GetDeviceCount() == 2);
1177 CHECK(device2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001178
1179 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001180 MockBufferManager mockBuffer(10);
Sadik Armagan3896b472020-02-10 12:24:15 +00001181 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001182 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001183 BufferExhaustion);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001184}
1185
Sadik Armagan1625efc2021-06-10 18:24:34 +01001186TEST_CASE("SendCounterDirectoryPacketTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001187{
1188 // The counter directory used for testing
1189 CounterDirectory counterDirectory;
1190
1191 // Register a device
1192 const std::string device1Name = "device1";
1193 const Device* device1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001194 CHECK_NOTHROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1195 CHECK(counterDirectory.GetDeviceCount() == 1);
1196 CHECK(device1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001197
1198 // Register a device
1199 const std::string device2Name = "device2";
1200 const Device* device2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001201 CHECK_NOTHROW(device2 = counterDirectory.RegisterDevice(device2Name));
1202 CHECK(counterDirectory.GetDeviceCount() == 2);
1203 CHECK(device2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001204
1205 // Register a counter set
1206 const std::string counterSet1Name = "counterset1";
1207 const CounterSet* counterSet1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001208 CHECK_NOTHROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1209 CHECK(counterDirectory.GetCounterSetCount() == 1);
1210 CHECK(counterSet1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001211
1212 // Register a category associated to "device1" and "counterset1"
1213 const std::string category1Name = "category1";
1214 const Category* category1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001215 CHECK_NOTHROW(category1 = counterDirectory.RegisterCategory(category1Name));
1216 CHECK(counterDirectory.GetCategoryCount() == 1);
1217 CHECK(category1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001218
1219 // Register a category not associated to "device2" but no counter set
1220 const std::string category2Name = "category2";
1221 const Category* category2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001222 CHECK_NOTHROW(category2 = counterDirectory.RegisterCategory(category2Name));
1223 CHECK(counterDirectory.GetCategoryCount() == 2);
1224 CHECK(category2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001225
Keith Davis33ed2212020-03-30 10:43:41 +01001226 uint16_t numberOfCores = 4;
Sadik Armagan4c998992020-02-25 12:44:44 +00001227
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001228 // Register a counter associated to "category1"
1229 const Counter* counter1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001230 CHECK_NOTHROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001231 0,
1232 category1Name,
1233 0,
1234 1,
1235 123.45f,
1236 "counter1",
1237 "counter1description",
1238 std::string("counter1units"),
1239 numberOfCores));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001240 CHECK(counterDirectory.GetCounterCount() == 4);
1241 CHECK(counter1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001242
1243 // Register a counter associated to "category1"
1244 const Counter* counter2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001245 CHECK_NOTHROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001246 4,
1247 category1Name,
1248 1,
1249 0,
1250 330.1245656765f,
1251 "counter2",
1252 "counter2description",
1253 std::string("counter2units"),
1254 armnn::EmptyOptional(),
1255 device2->m_Uid,
1256 0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001257 CHECK(counterDirectory.GetCounterCount() == 5);
1258 CHECK(counter2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001259
1260 // Register a counter associated to "category2"
1261 const Counter* counter3 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001262 CHECK_NOTHROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001263 5,
1264 category2Name,
1265 1,
1266 1,
1267 0.0000045399f,
1268 "counter3",
1269 "counter3description",
1270 armnn::EmptyOptional(),
1271 numberOfCores,
1272 device2->m_Uid,
1273 counterSet1->m_Uid));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001274 CHECK(counterDirectory.GetCounterCount() == 9);
1275 CHECK(counter3);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001276
1277 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001278 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001279 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001280 CHECK_NOTHROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001281
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001282 // Get the readable buffer
1283 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001284
1285 // Check the packet header
Finn Williams985fecf2020-04-30 11:06:43 +01001286 const uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1287 const uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001288 CHECK(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1289 CHECK(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
1290 CHECK(packetHeaderWord1 == 432); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001291
1292 // Check the body header
Finn Williams985fecf2020-04-30 11:06:43 +01001293 const uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1294 const uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1295 const uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1296 const uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1297 const uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1298 const uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1299 const uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1300 const uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1301 const uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001302 CHECK(deviceRecordCount == 2); // device_records_count
1303 CHECK(bodyHeaderWord1 == bodyHeaderSize * 4); // device_records_pointer_table_offset
1304 CHECK(counterSetRecordCount == 1); // counter_set_count
1305 CHECK(bodyHeaderWord3 == 8 + bodyHeaderSize * 4); // counter_set_pointer_table_offset
1306 CHECK(categoryRecordCount == 2); // categories_count
1307 CHECK(bodyHeaderWord5 == 12 + bodyHeaderSize * 4); // categories_pointer_table_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001308
1309 // Check the device records pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001310 const uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1311 const uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001312 CHECK(deviceRecordOffset0 == 20); // Device record offset for "device1"
1313 CHECK(deviceRecordOffset1 == 40); // Device record offset for "device2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001314
1315 // Check the counter set pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001316 const uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001317 CHECK(counterSetRecordOffset0 == 52); // Counter set record offset for "counterset1"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001318
1319 // Check the category pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001320 const uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1321 const uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001322 CHECK(categoryRecordOffset0 == 72); // Category record offset for "category1"
1323 CHECK(categoryRecordOffset1 == 176); // Category record offset for "category2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001324
1325 // Get the device record pool offset
Finn Williams985fecf2020-04-30 11:06:43 +01001326 const uint32_t uint32_t_size = sizeof(uint32_t);
Finn Williamsd44815f2020-05-01 13:25:55 +01001327 const uint32_t packetHeaderSize = 2u * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001328
1329 // Device record structure/collection used for testing
1330 struct DeviceRecord
1331 {
1332 uint16_t uid;
1333 uint16_t cores;
1334 uint32_t name_offset;
1335 uint32_t name_length;
1336 std::string name;
1337 };
1338 std::vector<DeviceRecord> deviceRecords;
Finn Williamsd44815f2020-05-01 13:25:55 +01001339 const uint32_t deviceRecordsPointerTableOffset = packetHeaderSize +
Finn Williams985fecf2020-04-30 11:06:43 +01001340 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001341
1342 const unsigned char* readData = readBuffer->GetReadableData();
1343
Finn Williamsd44815f2020-05-01 13:25:55 +01001344 uint32_t offset = 0;
1345 std::vector<uint32_t> data(800);
1346
1347 for (uint32_t i = 0; i < 800; i+=uint32_t_size)
1348 {
1349 data[i] = ReadUint32(readBuffer, offset);
1350 offset += uint32_t_size;
1351 }
1352
1353 std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount);
1354 offset = deviceRecordsPointerTableOffset;
1355 for (uint32_t i = 0; i < deviceRecordCount; ++i)
1356 {
1357 // deviceRecordOffset is relative to the start of the deviceRecordsPointerTable
1358 deviceRecordOffsets[i] = ReadUint32(readBuffer, offset) + deviceRecordsPointerTableOffset;
1359 offset += uint32_t_size;
1360 }
1361
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001362 for (uint32_t i = 0; i < deviceRecordCount; i++)
1363 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001364 // Collect the data for the device record
Finn Williamsd44815f2020-05-01 13:25:55 +01001365 const uint32_t deviceRecordWord0 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 0 * uint32_t_size);
1366 const uint32_t deviceRecordWord1 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001367 DeviceRecord deviceRecord;
1368 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1369 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1370 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1371
Finn Williamsd44815f2020-05-01 13:25:55 +01001372 uint32_t deviceRecordPoolOffset = deviceRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001373 deviceRecord.name_offset; // Device name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001374 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1375 deviceRecord.name_length = deviceRecordNameLength; // name_length
1376 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1377 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001378 CHECK(deviceRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001379 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1380 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001381 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001382 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1383
1384 deviceRecords.push_back(deviceRecord);
1385 }
1386
1387 // Check that the device records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001388 CHECK(deviceRecords.size() == 2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001389 for (const DeviceRecord& deviceRecord : deviceRecords)
1390 {
1391 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001392 CHECK(device);
1393 CHECK(device->m_Uid == deviceRecord.uid);
1394 CHECK(device->m_Cores == deviceRecord.cores);
1395 CHECK(device->m_Name == deviceRecord.name);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001396 }
1397
Finn Williamsd44815f2020-05-01 13:25:55 +01001398
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001399 // Counter set record structure/collection used for testing
1400 struct CounterSetRecord
1401 {
1402 uint16_t uid;
1403 uint16_t count;
1404 uint32_t name_offset;
1405 uint32_t name_length;
1406 std::string name;
1407 };
1408 std::vector<CounterSetRecord> counterSetRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001409 const uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1410 bodyHeaderWord3; // counter_set_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001411
1412 offset = counterSetRecordsPointerTableOffset;
1413 std::vector<uint32_t> counterSetRecordOffsets(counterSetRecordCount);
1414
1415 for (uint32_t i = 0; i < counterSetRecordCount; ++i)
1416 {
1417 // counterSetRecordOffset is relative to the start of the dcounterSetRecordsPointerTable
1418 counterSetRecordOffsets[i] = ReadUint32(readBuffer, offset) + counterSetRecordsPointerTableOffset;
1419 offset += uint32_t_size;
1420 }
1421
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001422 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1423 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001424 // Collect the data for the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +01001425 const uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 0 * uint32_t_size);
1426 const uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001427 CounterSetRecord counterSetRecord;
1428 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1429 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1430 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1431
Finn Williamsd44815f2020-05-01 13:25:55 +01001432 uint32_t counterSetRecordPoolOffset = counterSetRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001433 counterSetRecord.name_offset; // Counter set name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001434 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1435 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1436 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1437 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001438 CHECK(counterSetRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001439 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1440 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001441 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001442 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1443
1444 counterSetRecords.push_back(counterSetRecord);
1445 }
1446
1447 // Check that the counter set records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001448 CHECK(counterSetRecords.size() == 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001449 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1450 {
1451 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001452 CHECK(counterSet);
1453 CHECK(counterSet->m_Uid == counterSetRecord.uid);
1454 CHECK(counterSet->m_Count == counterSetRecord.count);
1455 CHECK(counterSet->m_Name == counterSetRecord.name);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001456 }
1457
1458 // Event record structure/collection used for testing
1459 struct EventRecord
1460 {
1461 uint16_t counter_uid;
1462 uint16_t max_counter_uid;
1463 uint16_t device;
1464 uint16_t counter_set;
1465 uint16_t counter_class;
1466 uint16_t interpolation;
1467 double multiplier;
1468 uint32_t name_offset;
1469 uint32_t name_length;
1470 std::string name;
1471 uint32_t description_offset;
1472 uint32_t description_length;
1473 std::string description;
1474 uint32_t units_offset;
1475 uint32_t units_length;
1476 std::string units;
1477 };
1478 // Category record structure/collection used for testing
1479 struct CategoryRecord
1480 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001481 uint16_t event_count;
1482 uint32_t event_pointer_table_offset;
1483 uint32_t name_offset;
1484 uint32_t name_length;
1485 std::string name;
1486 std::vector<uint32_t> event_pointer_table;
1487 std::vector<EventRecord> event_records;
1488 };
1489 std::vector<CategoryRecord> categoryRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001490 const uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1491 bodyHeaderWord5; // categories_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001492
1493 offset = categoryRecordsPointerTableOffset;
1494 std::vector<uint32_t> categoryRecordOffsets(categoryRecordCount);
1495 for (uint32_t i = 0; i < categoryRecordCount; ++i)
1496 {
1497 // categoryRecordOffset is relative to the start of the categoryRecordsPointerTable
1498 categoryRecordOffsets[i] = ReadUint32(readBuffer, offset) + categoryRecordsPointerTableOffset;
1499 offset += uint32_t_size;
1500 }
1501
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001502 for (uint32_t i = 0; i < categoryRecordCount; i++)
1503 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001504 // Collect the data for the category record
Finn Williamsd44815f2020-05-01 13:25:55 +01001505 const uint32_t categoryRecordWord1 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 0 * uint32_t_size);
1506 const uint32_t categoryRecordWord2 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 1 * uint32_t_size);
1507 const uint32_t categoryRecordWord3 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 2 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001508 CategoryRecord categoryRecord;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001509 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1510 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1511 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1512
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001513 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001514 categoryRecordOffsets[i] + categoryRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001515 categoryRecord.name_length = categoryRecordNameLength; // name_length
1516 unsigned char categoryRecordNameNullTerminator =
1517 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001518 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001519 categoryRecord.name_offset +
1520 uint32_t_size +
1521 categoryRecordNameLength - 1); // name null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001522 CHECK(categoryRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001523 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1524 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001525 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001526 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001527 categoryRecord.name_offset +
1528 uint32_t_size,
1529 categoryRecordNameBuffer.size());
1530 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1531
1532 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
Finn Williamsd44815f2020-05-01 13:25:55 +01001533 offset = categoryRecordOffsets[i] + categoryRecord.event_pointer_table_offset;
1534 for (uint32_t eventOffsetIndex = 0; eventOffsetIndex < categoryRecord.event_count; ++eventOffsetIndex)
1535 {
1536 // eventRecordOffset is relative to the start of the event pointer table
1537 categoryRecord.event_pointer_table[eventOffsetIndex] = ReadUint32(readBuffer, offset) +
1538 categoryRecordOffsets[i] +
1539 categoryRecord.event_pointer_table_offset;
1540 offset += uint32_t_size;
1541 }
1542
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001543 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1544 {
Finn Williamsd44815f2020-05-01 13:25:55 +01001545 const uint32_t eventOffset = categoryRecord.event_pointer_table[eventIndex];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001546 // Collect the data for the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001547 const uint32_t eventRecordWord0 = ReadUint32(readBuffer, eventOffset + 0 * uint32_t_size);
1548 const uint32_t eventRecordWord1 = ReadUint32(readBuffer, eventOffset + 1 * uint32_t_size);
1549 const uint32_t eventRecordWord2 = ReadUint32(readBuffer, eventOffset + 2 * uint32_t_size);
1550 const uint64_t eventRecordWord34 = ReadUint64(readBuffer, eventOffset + 3 * uint32_t_size);
1551 const uint32_t eventRecordWord5 = ReadUint32(readBuffer, eventOffset + 5 * uint32_t_size);
1552 const uint32_t eventRecordWord6 = ReadUint32(readBuffer, eventOffset + 6 * uint32_t_size);
1553 const uint32_t eventRecordWord7 = ReadUint32(readBuffer, eventOffset + 7 * uint32_t_size);
1554
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001555 EventRecord eventRecord;
1556 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1557 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1558 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1559 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1560 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1561 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1562 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1563 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1564 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1565 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1566
Finn Williamsd44815f2020-05-01 13:25:55 +01001567 uint32_t eventRecordNameLength = ReadUint32(readBuffer, eventOffset + eventRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001568 eventRecord.name_length = eventRecordNameLength; // name_length
1569 unsigned char eventRecordNameNullTerminator =
1570 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001571 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001572 eventRecord.name_offset +
1573 uint32_t_size +
1574 eventRecordNameLength - 1); // name null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001575 CHECK(eventRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001576 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1577 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001578 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001579 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001580 eventRecord.name_offset +
1581 uint32_t_size,
1582 eventRecordNameBuffer.size());
1583 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1584
1585 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001586 eventOffset + eventRecord.description_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001587 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1588 unsigned char eventRecordDescriptionNullTerminator =
1589 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001590 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001591 eventRecord.description_offset +
1592 uint32_t_size +
1593 eventRecordDescriptionLength - 1); // description null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001594 CHECK(eventRecordDescriptionNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001595 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1596 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001597 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001598 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001599 eventRecord.description_offset +
1600 uint32_t_size,
1601 eventRecordDescriptionBuffer.size());
1602 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1603 eventRecordDescriptionBuffer.end()); // description
1604
1605 if (eventRecord.units_offset > 0)
1606 {
1607 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001608 eventOffset + eventRecord.units_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001609 eventRecord.units_length = eventRecordUnitsLength; // units_length
1610 unsigned char eventRecordUnitsNullTerminator =
1611 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001612 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001613 eventRecord.units_offset +
1614 uint32_t_size +
1615 eventRecordUnitsLength - 1); // units null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001616 CHECK(eventRecordUnitsNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001617 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1618 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001619 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001620 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001621 eventRecord.units_offset +
1622 uint32_t_size,
1623 eventRecordUnitsBuffer.size());
1624 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1625 }
1626
1627 categoryRecord.event_records.push_back(eventRecord);
1628 }
1629
1630 categoryRecords.push_back(categoryRecord);
1631 }
1632
1633 // Check that the category records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001634 CHECK(categoryRecords.size() == 2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001635 for (const CategoryRecord& categoryRecord : categoryRecords)
1636 {
1637 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001638 CHECK(category);
1639 CHECK(category->m_Name == categoryRecord.name);
1640 CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
1641 CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001642
1643 // Check that the event records are correct
1644 for (const EventRecord& eventRecord : categoryRecord.event_records)
1645 {
1646 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001647 CHECK(counter);
1648 CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1649 CHECK(counter->m_DeviceUid == eventRecord.device);
1650 CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1651 CHECK(counter->m_Class == eventRecord.counter_class);
1652 CHECK(counter->m_Interpolation == eventRecord.interpolation);
1653 CHECK(counter->m_Multiplier == eventRecord.multiplier);
1654 CHECK(counter->m_Name == eventRecord.name);
1655 CHECK(counter->m_Description == eventRecord.description);
1656 CHECK(counter->m_Units == eventRecord.units);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001657 }
1658 }
1659}
1660
Sadik Armagan1625efc2021-06-10 18:24:34 +01001661TEST_CASE("SendCounterDirectoryPacketTest3")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001662{
1663 // Using a mock counter directory that allows to register invalid objects
1664 MockCounterDirectory counterDirectory;
1665
1666 // Register an invalid device
1667 const std::string deviceName = "inv@lid dev!c€";
1668 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001669 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1670 CHECK(counterDirectory.GetDeviceCount() == 1);
1671 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001672
1673 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001674 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001675 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001676 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001677}
1678
Sadik Armagan1625efc2021-06-10 18:24:34 +01001679TEST_CASE("SendCounterDirectoryPacketTest4")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001680{
1681 // Using a mock counter directory that allows to register invalid objects
1682 MockCounterDirectory counterDirectory;
1683
1684 // Register an invalid counter set
1685 const std::string counterSetName = "inv@lid count€rs€t";
1686 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001687 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1688 CHECK(counterDirectory.GetCounterSetCount() == 1);
1689 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001690
1691 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001692 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001693 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001694 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001695}
1696
Sadik Armagan1625efc2021-06-10 18:24:34 +01001697TEST_CASE("SendCounterDirectoryPacketTest5")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001698{
1699 // Using a mock counter directory that allows to register invalid objects
1700 MockCounterDirectory counterDirectory;
1701
1702 // Register an invalid category
1703 const std::string categoryName = "c@t€gory";
1704 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001705 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1706 CHECK(counterDirectory.GetCategoryCount() == 1);
1707 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001708
1709 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001710 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001711 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001712 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001713}
1714
Sadik Armagan1625efc2021-06-10 18:24:34 +01001715TEST_CASE("SendCounterDirectoryPacketTest6")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001716{
1717 // Using a mock counter directory that allows to register invalid objects
1718 MockCounterDirectory counterDirectory;
1719
1720 // Register an invalid device
1721 const std::string deviceName = "inv@lid dev!c€";
1722 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001723 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1724 CHECK(counterDirectory.GetDeviceCount() == 1);
1725 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001726
1727 // Register an invalid counter set
1728 const std::string counterSetName = "inv@lid count€rs€t";
1729 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001730 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1731 CHECK(counterDirectory.GetCounterSetCount() == 1);
1732 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001733
1734 // Register an invalid category associated to an invalid device and an invalid counter set
1735 const std::string categoryName = "c@t€gory";
1736 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001737 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1738 CHECK(counterDirectory.GetCategoryCount() == 1);
1739 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001740
1741 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001742 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001743 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001744 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001745}
1746
Sadik Armagan1625efc2021-06-10 18:24:34 +01001747TEST_CASE("SendCounterDirectoryPacketTest7")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001748{
1749 // Using a mock counter directory that allows to register invalid objects
1750 MockCounterDirectory counterDirectory;
1751
1752 // Register an valid device
1753 const std::string deviceName = "valid device";
1754 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001755 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1756 CHECK(counterDirectory.GetDeviceCount() == 1);
1757 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001758
1759 // Register an valid counter set
1760 const std::string counterSetName = "valid counterset";
1761 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001762 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1763 CHECK(counterDirectory.GetCounterSetCount() == 1);
1764 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001765
1766 // Register an valid category associated to a valid device and a valid counter set
1767 const std::string categoryName = "category";
1768 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001769 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1770 CHECK(counterDirectory.GetCategoryCount() == 1);
1771 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001772
1773 // Register an invalid counter associated to a valid category
1774 const Counter* counter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001775 CHECK_NOTHROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001776 0,
1777 categoryName,
1778 0,
1779 1,
1780 123.45f,
1781 "counter",
1782 "counter description",
1783 std::string("invalid counter units"),
1784 5,
1785 device->m_Uid,
1786 counterSet->m_Uid));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001787 CHECK(counterDirectory.GetCounterCount() == 5);
1788 CHECK(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001789
1790 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001791 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001792 SendCounterPacket sendCounterPacket(mockBuffer);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001793 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001794}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001795
Sadik Armagan1625efc2021-06-10 18:24:34 +01001796TEST_CASE("SendThreadTest0")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001797{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001798 ProfilingStateMachine profilingStateMachine;
1799 SetActiveProfilingState(profilingStateMachine);
1800
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001801 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001802 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001803 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1804 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001805
1806 // Try to start the send thread many times, it must only start once
1807
Sadik Armagan3896b472020-02-10 12:24:15 +00001808 sendThread.Start(mockProfilingConnection);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001809 CHECK(sendThread.IsRunning());
Sadik Armagan3896b472020-02-10 12:24:15 +00001810 sendThread.Start(mockProfilingConnection);
1811 sendThread.Start(mockProfilingConnection);
1812 sendThread.Start(mockProfilingConnection);
1813 sendThread.Start(mockProfilingConnection);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001814 CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001815
Sadik Armagan3896b472020-02-10 12:24:15 +00001816 sendThread.Stop();
Sadik Armagan1625efc2021-06-10 18:24:34 +01001817 CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001818}
1819
Sadik Armagan1625efc2021-06-10 18:24:34 +01001820TEST_CASE("SendThreadTest1")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001821{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001822 ProfilingStateMachine profilingStateMachine;
1823 SetActiveProfilingState(profilingStateMachine);
1824
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001825 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001826
1827 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001828 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001829 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1830 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1831 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001832
1833 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1834 // something to become available for reading
1835
Colm Donelan2ba48d22019-11-29 09:10:59 +00001836 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001837
1838 CounterDirectory counterDirectory;
1839 sendCounterPacket.SendStreamMetaDataPacket();
1840
Finn Williamsa0de0562020-04-22 12:27:37 +01001841 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001842
Sadik Armagan3896b472020-02-10 12:24:15 +00001843 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001844
Colm Donelan2ba48d22019-11-29 09:10:59 +00001845 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001846
1847 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1848
1849 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001850 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001851 totalWrittenSize += counterDirectoryPacketSize;
1852
Sadik Armagan3896b472020-02-10 12:24:15 +00001853 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001854
Colm Donelan2ba48d22019-11-29 09:10:59 +00001855 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001856
1857 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1858 {
1859 { 1u, 23u },
1860 { 33u, 1207623u }
1861 });
1862
1863 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001864 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001865 totalWrittenSize += periodicCounterCapturePacketSize;
1866
Sadik Armagan3896b472020-02-10 12:24:15 +00001867 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001868
Colm Donelan2ba48d22019-11-29 09:10:59 +00001869 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001870
1871 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1872 {
1873 { 211u, 923u }
1874 });
1875
1876 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001877 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001878 totalWrittenSize += periodicCounterCapturePacketSize;
1879
1880 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1881 {
1882 { 555u, 23u },
1883 { 556u, 6u },
1884 { 557u, 893454u },
1885 { 558u, 1456623u },
1886 { 559u, 571090u }
1887 });
1888
1889 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001890 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001891 totalWrittenSize += periodicCounterCapturePacketSize;
1892
1893 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1894 {
1895 { 88u, 11u },
1896 { 96u, 22u },
1897 { 97u, 33u },
1898 { 999u, 444u }
1899 });
1900
1901 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001902 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001903 totalWrittenSize += periodicCounterCapturePacketSize;
1904
Sadik Armagan3896b472020-02-10 12:24:15 +00001905 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001906
Colm Donelan2ba48d22019-11-29 09:10:59 +00001907 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001908
1909 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1910
1911 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001912 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001913 totalWrittenSize += periodicCounterCapturePacketSize;
1914
Sadik Armagan3896b472020-02-10 12:24:15 +00001915 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001916
Finn Williams109c05b2019-11-29 13:56:33 +00001917 // To test an exact value of the "read size" in the mock buffer, wait to allow the send thread to
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001918 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001919 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001920
Sadik Armagan3896b472020-02-10 12:24:15 +00001921 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001922
Sadik Armagan1625efc2021-06-10 18:24:34 +01001923 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
1924 CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1925 CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001926}
1927
Sadik Armagan1625efc2021-06-10 18:24:34 +01001928TEST_CASE("SendThreadTest2")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001929{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001930 ProfilingStateMachine profilingStateMachine;
1931 SetActiveProfilingState(profilingStateMachine);
1932
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001933 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001934
1935 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001936 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001937 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1938 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1939 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001940
1941 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
1942 // capable of handling unnecessary read requests
1943
Colm Donelan2ba48d22019-11-29 09:10:59 +00001944 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001945
Sadik Armagan3896b472020-02-10 12:24:15 +00001946 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001947
1948 CounterDirectory counterDirectory;
1949 sendCounterPacket.SendStreamMetaDataPacket();
1950
Finn Williamsa0de0562020-04-22 12:27:37 +01001951 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001952
Sadik Armagan3896b472020-02-10 12:24:15 +00001953 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001954
Colm Donelan2ba48d22019-11-29 09:10:59 +00001955 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001956
1957 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1958
1959 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001960 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001961 totalWrittenSize += counterDirectoryPacketSize;
1962
Sadik Armagan3896b472020-02-10 12:24:15 +00001963 sendThread.SetReadyToRead();
1964 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001965
Colm Donelan2ba48d22019-11-29 09:10:59 +00001966 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001967
1968 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1969 {
1970 { 1u, 23u },
1971 { 33u, 1207623u }
1972 });
1973
1974 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001975 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001976 totalWrittenSize += periodicCounterCapturePacketSize;
1977
Sadik Armagan3896b472020-02-10 12:24:15 +00001978 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001979
Colm Donelan2ba48d22019-11-29 09:10:59 +00001980 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001981
Sadik Armagan3896b472020-02-10 12:24:15 +00001982 sendThread.SetReadyToRead();
1983 sendThread.SetReadyToRead();
1984 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001985
Colm Donelan2ba48d22019-11-29 09:10:59 +00001986 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001987
Sadik Armagan3896b472020-02-10 12:24:15 +00001988 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001989 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1990 {
1991 { 211u, 923u }
1992 });
1993
1994 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001995 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001996 totalWrittenSize += periodicCounterCapturePacketSize;
1997
1998 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1999 {
2000 { 555u, 23u },
2001 { 556u, 6u },
2002 { 557u, 893454u },
2003 { 558u, 1456623u },
2004 { 559u, 571090u }
2005 });
2006
2007 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002008 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002009 totalWrittenSize += periodicCounterCapturePacketSize;
2010
Sadik Armagan3896b472020-02-10 12:24:15 +00002011 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002012 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2013 {
2014 { 88u, 11u },
2015 { 96u, 22u },
2016 { 97u, 33u },
2017 { 999u, 444u }
2018 });
2019
2020 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002021 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002022 totalWrittenSize += periodicCounterCapturePacketSize;
2023
Sadik Armagan3896b472020-02-10 12:24:15 +00002024 sendThread.SetReadyToRead();
2025 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002026
Colm Donelan2ba48d22019-11-29 09:10:59 +00002027 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002028
2029 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2030
2031 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002032 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002033 totalWrittenSize += periodicCounterCapturePacketSize;
2034
Sadik Armagan3896b472020-02-10 12:24:15 +00002035 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002036
Finn Williams109c05b2019-11-29 13:56:33 +00002037 // To test an exact value of the "read size" in the mock buffer, wait to allow the send thread to
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002038 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002039 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002040
Sadik Armagan1625efc2021-06-10 18:24:34 +01002041 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2042 CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2043 CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002044}
2045
Sadik Armagan1625efc2021-06-10 18:24:34 +01002046TEST_CASE("SendThreadTest3")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002047{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002048 ProfilingStateMachine profilingStateMachine;
2049 SetActiveProfilingState(profilingStateMachine);
2050
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002051 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002052
2053 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002054 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002055 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
2056 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2057 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002058
2059 // Not using pauses or "grace periods" to stress test the send thread
2060
Sadik Armagan3896b472020-02-10 12:24:15 +00002061 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002062
2063 CounterDirectory counterDirectory;
2064 sendCounterPacket.SendStreamMetaDataPacket();
2065
Finn Williamsa0de0562020-04-22 12:27:37 +01002066 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002067
Sadik Armagan3896b472020-02-10 12:24:15 +00002068 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002069 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2070
2071 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002072 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002073 totalWrittenSize += counterDirectoryPacketSize;
2074
Sadik Armagan3896b472020-02-10 12:24:15 +00002075 sendThread.SetReadyToRead();
2076 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002077 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2078 {
2079 { 1u, 23u },
2080 { 33u, 1207623u }
2081 });
2082
2083 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002084 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002085 totalWrittenSize += periodicCounterCapturePacketSize;
2086
Sadik Armagan3896b472020-02-10 12:24:15 +00002087 sendThread.SetReadyToRead();
2088 sendThread.SetReadyToRead();
2089 sendThread.SetReadyToRead();
2090 sendThread.SetReadyToRead();
2091 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002092 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2093 {
2094 { 211u, 923u }
2095 });
2096
2097 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002098 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002099 totalWrittenSize += periodicCounterCapturePacketSize;
2100
2101 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2102 {
2103 { 555u, 23u },
2104 { 556u, 6u },
2105 { 557u, 893454u },
2106 { 558u, 1456623u },
2107 { 559u, 571090u }
2108 });
2109
2110 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002111 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002112 totalWrittenSize += periodicCounterCapturePacketSize;
2113
Sadik Armagan3896b472020-02-10 12:24:15 +00002114 sendThread.SetReadyToRead();
2115 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002116 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2117 {
2118 { 88u, 11u },
2119 { 96u, 22u },
2120 { 97u, 33u },
2121 { 999u, 444u }
2122 });
2123
2124 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002125 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002126 totalWrittenSize += periodicCounterCapturePacketSize;
2127
Sadik Armagan3896b472020-02-10 12:24:15 +00002128 sendThread.SetReadyToRead();
2129 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002130 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2131
2132 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002133 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002134 totalWrittenSize += periodicCounterCapturePacketSize;
2135
Sadik Armagan3896b472020-02-10 12:24:15 +00002136 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002137
2138 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2139 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002140 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002141
Sadik Armagan1625efc2021-06-10 18:24:34 +01002142 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2143 CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2144 CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2145 CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2146 CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002147}
2148
Sadik Armagan1625efc2021-06-10 18:24:34 +01002149TEST_CASE("SendCounterPacketTestWithSendThread")
Sadik Armagan3896b472020-02-10 12:24:15 +00002150{
2151 ProfilingStateMachine profilingStateMachine;
2152 SetWaitingForAckProfilingState(profilingStateMachine);
2153
2154 MockProfilingConnection mockProfilingConnection;
2155 BufferManager bufferManager(1, 1024);
2156 SendCounterPacket sendCounterPacket(bufferManager);
2157 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2158 sendThread.Start(mockProfilingConnection);
2159
Finn Williamsa0de0562020-04-22 12:27:37 +01002160 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan3896b472020-02-10 12:24:15 +00002161
2162 sendThread.Stop();
2163
2164 // check for packet in ProfilingConnection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002165 CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
Sadik Armagan3896b472020-02-10 12:24:15 +00002166
2167 SetActiveProfilingState(profilingStateMachine);
2168 sendThread.Start(mockProfilingConnection);
2169
2170 // SendCounterDirectoryPacket
2171 CounterDirectory counterDirectory;
2172 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2173
2174 sendThread.Stop();
2175 unsigned int counterDirectoryPacketSize = 32;
2176 // check for packet in ProfilingConnection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002177 CHECK(mockProfilingConnection.CheckForPacket(
Sadik Armagan3896b472020-02-10 12:24:15 +00002178 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2179
2180 sendThread.Start(mockProfilingConnection);
2181
2182 // SendPeriodicCounterCapturePacket
2183 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2184 {
2185 { 1u, 23u },
2186 { 33u, 1207623u }
2187 });
2188
2189 sendThread.Stop();
2190
2191 unsigned int periodicCounterCapturePacketSize = 28;
Sadik Armagan1625efc2021-06-10 18:24:34 +01002192 CHECK(mockProfilingConnection.CheckForPacket(
Sadik Armagan3896b472020-02-10 12:24:15 +00002193 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2194}
2195
Sadik Armagan1625efc2021-06-10 18:24:34 +01002196TEST_CASE("SendThreadBufferTest")
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002197{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002198 ProfilingStateMachine profilingStateMachine;
2199 SetActiveProfilingState(profilingStateMachine);
2200
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002201 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002202 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002203 SendCounterPacket sendCounterPacket(bufferManager);
2204 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2205 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002206
2207 // SendStreamMetaDataPacket
2208 sendCounterPacket.SendStreamMetaDataPacket();
2209
2210 // Read data from the buffer
2211 // Buffer should become readable after commit by SendStreamMetaDataPacket
2212 auto packetBuffer = bufferManager.GetReadableBuffer();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002213 CHECK(packetBuffer.get());
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002214
Finn Williamsa0de0562020-04-22 12:27:37 +01002215 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002216 CHECK(packetBuffer->GetSize() == streamMetadataPacketsize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002217
2218 // Recommit to be read by sendCounterPacket
2219 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2220
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002221 // SendCounterDirectoryPacket
2222 CounterDirectory counterDirectory;
2223 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2224
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002225 // SendPeriodicCounterCapturePacket
2226 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2227 {
2228 { 1u, 23u },
2229 { 33u, 1207623u }
2230 });
2231
Sadik Armagan3896b472020-02-10 12:24:15 +00002232 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002233
2234 // The buffer is read by the send thread so it should not be in the readable buffer.
2235 auto readBuffer = bufferManager.GetReadableBuffer();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002236 CHECK(!readBuffer);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002237
2238 // Successfully reserved the buffer with requested size
2239 unsigned int reservedSize = 0;
2240 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002241 CHECK(reservedSize == 512);
2242 CHECK(reservedBuffer.get());
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002243
Finn Williams09ad6f92019-12-19 17:05:18 +00002244 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2245 const auto metaDataPacketCount =
2246 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2247
Sadik Armagan1625efc2021-06-10 18:24:34 +01002248 CHECK(metaDataPacketCount >= 1);
2249 CHECK(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2250 CHECK(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
Finn Williams09ad6f92019-12-19 17:05:18 +00002251 // Check that we only received the packets we expected
Sadik Armagan1625efc2021-06-10 18:24:34 +01002252 CHECK(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002253}
2254
Sadik Armagan1625efc2021-06-10 18:24:34 +01002255TEST_CASE("SendThreadSendStreamMetadataPacket1")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002256{
2257 ProfilingStateMachine profilingStateMachine;
2258
2259 MockProfilingConnection mockProfilingConnection;
2260 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002261 SendCounterPacket sendCounterPacket(bufferManager);
2262 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2263 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002264
2265 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Sadik Armagan1625efc2021-06-10 18:24:34 +01002266 CHECK_THROWS_AS(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002267}
2268
Sadik Armagan1625efc2021-06-10 18:24:34 +01002269TEST_CASE("SendThreadSendStreamMetadataPacket2")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002270{
2271 ProfilingStateMachine profilingStateMachine;
2272 SetNotConnectedProfilingState(profilingStateMachine);
2273
2274 MockProfilingConnection mockProfilingConnection;
2275 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002276 SendCounterPacket sendCounterPacket(bufferManager);
2277 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2278 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002279
2280 // The profiling state is set to "NotConnected", so the send thread should throw an exception
Sadik Armagan1625efc2021-06-10 18:24:34 +01002281 CHECK_THROWS_AS(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002282}
2283
Sadik Armagan1625efc2021-06-10 18:24:34 +01002284TEST_CASE("SendThreadSendStreamMetadataPacket3")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002285{
2286 ProfilingStateMachine profilingStateMachine;
2287 SetWaitingForAckProfilingState(profilingStateMachine);
2288
Finn Williamsa0de0562020-04-22 12:27:37 +01002289 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002290
2291 MockProfilingConnection mockProfilingConnection;
2292 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002293 SendCounterPacket sendCounterPacket(bufferManager);
2294 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2295 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002296
2297 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002298 // Wait for sendThread to join
Sadik Armagan1625efc2021-06-10 18:24:34 +01002299 CHECK_NOTHROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002300
Finn Williams09ad6f92019-12-19 17:05:18 +00002301 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2302 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2303
Sadik Armagan1625efc2021-06-10 18:24:34 +01002304 CHECK(writtenDataSize >= 1u);
2305 CHECK(mockProfilingConnection.CheckForPacket(
Finn Williams09ad6f92019-12-19 17:05:18 +00002306 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002307}
2308
Sadik Armagan1625efc2021-06-10 18:24:34 +01002309TEST_CASE("SendThreadSendStreamMetadataPacket4")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002310{
2311 ProfilingStateMachine profilingStateMachine;
2312 SetWaitingForAckProfilingState(profilingStateMachine);
2313
Finn Williamsa0de0562020-04-22 12:27:37 +01002314 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002315
2316 MockProfilingConnection mockProfilingConnection;
2317 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002318 SendCounterPacket sendCounterPacket(bufferManager);
2319 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2320 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002321
2322 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002323 // Wait for sendThread to join
2324 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002325
Sadik Armagan3896b472020-02-10 12:24:15 +00002326 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002327 // Check that the profiling state is still "WaitingForAck"
Sadik Armagan1625efc2021-06-10 18:24:34 +01002328 CHECK((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002329
Finn Williams3e2969d2019-12-06 17:47:36 +00002330 // Check that the buffer contains at least one Stream Metadata packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002331 CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002332
2333 mockProfilingConnection.Clear();
2334
Sadik Armagan3896b472020-02-10 12:24:15 +00002335 sendThread.Stop();
2336 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002337
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002338 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002339 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002340
Sadik Armagan3896b472020-02-10 12:24:15 +00002341 // Wait for sendThread to join
Sadik Armagan1625efc2021-06-10 18:24:34 +01002342 CHECK_NOTHROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002343
2344 // Check that the profiling state is still "WaitingForAck"
Sadik Armagan1625efc2021-06-10 18:24:34 +01002345 CHECK((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002346
Finn Williams09ad6f92019-12-19 17:05:18 +00002347 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2348 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2349
Sadik Armagan1625efc2021-06-10 18:24:34 +01002350 CHECK(writtenDataSize >= 1u);
2351 CHECK(mockProfilingConnection.CheckForPacket(
Finn Williams09ad6f92019-12-19 17:05:18 +00002352 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002353}
2354
Sadik Armagan1625efc2021-06-10 18:24:34 +01002355}