blob: 4ba5eed6ac12bbe02a41ed3aab0c4fea64b3ea0e [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>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010011#include <ProfilingUtils.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <SendCounterPacket.hpp>
13
Rob Hughes122f3252020-01-09 12:46:21 +000014#include <armnn/Utils.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010015
Jim Flynn6730fe92022-03-10 22:57:47 +000016#include <common/include/Assert.hpp>
Jim Flynn0a013a72022-03-13 16:57:04 +000017#include <common/include/Conversion.hpp>
Finn Williamse09fc822020-04-29 13:17:30 +010018#include <common/include/Constants.hpp>
Jim Flynnc454ac92022-03-16 18:43:18 +000019#include <common/include/CounterDirectory.hpp>
Jim Flynn6730fe92022-03-10 22:57:47 +000020#include <common/include/EncodeVersion.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000021#include <common/include/NumericCast.hpp>
Jim Flynn77b284e2022-03-13 20:53:35 +000022#include <common/include/Processes.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000023#include <common/include/ProfilingException.hpp>
Finn Williamse09fc822020-04-29 13:17:30 +010024
Sadik Armagan1625efc2021-06-10 18:24:34 +010025#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);
Jim Flynn9c85b412022-03-16 00:27:43 +0000156 SendCounterPacket sendPacket1(mockBuffer1,
157 arm::pipe::ARMNN_SOFTWARE_INFO,
158 arm::pipe::ARMNN_SOFTWARE_VERSION,
159 arm::pipe::ARMNN_HARDWARE_VERSION);
Ferran Balaguer73882172019-09-02 16:39:42 +0100160
161 uint32_t capturePeriod = 1000;
162 std::vector<uint16_t> selectedCounterIds;
Jim Flynn9c85b412022-03-16 00:27:43 +0000163 CHECK_THROWS_AS(sendPacket1.SendPeriodicCounterSelectionPacket(
164 capturePeriod, selectedCounterIds),
165 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100166
167 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100168 MockBufferManager mockBuffer2(512);
Jim Flynn9c85b412022-03-16 00:27:43 +0000169 SendCounterPacket sendPacket2(mockBuffer2,
170 arm::pipe::ARMNN_SOFTWARE_INFO,
171 arm::pipe::ARMNN_SOFTWARE_VERSION,
172 arm::pipe::ARMNN_HARDWARE_VERSION);
Ferran Balaguer73882172019-09-02 16:39:42 +0100173
174 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100175 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100176
177 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
178 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
179 uint32_t period = ReadUint32(readBuffer2, 8);
180
Sadik Armagan1625efc2021-06-10 18:24:34 +0100181 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
182 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
183 CHECK(headerWord1 == 4); // data lenght
184 CHECK(period == 1000); // capture period
Ferran Balaguer73882172019-09-02 16:39:42 +0100185
186 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100187 MockBufferManager mockBuffer3(512);
Jim Flynn9c85b412022-03-16 00:27:43 +0000188 SendCounterPacket sendPacket3(mockBuffer3,
189 arm::pipe::ARMNN_SOFTWARE_INFO,
190 arm::pipe::ARMNN_SOFTWARE_VERSION,
191 arm::pipe::ARMNN_HARDWARE_VERSION);
Ferran Balaguer73882172019-09-02 16:39:42 +0100192
193 selectedCounterIds.reserve(5);
194 selectedCounterIds.emplace_back(100);
195 selectedCounterIds.emplace_back(200);
196 selectedCounterIds.emplace_back(300);
197 selectedCounterIds.emplace_back(400);
198 selectedCounterIds.emplace_back(500);
199 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100200 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100201
202 headerWord0 = ReadUint32(readBuffer3, 0);
203 headerWord1 = ReadUint32(readBuffer3, 4);
204 period = ReadUint32(readBuffer3, 8);
205
Sadik Armagan1625efc2021-06-10 18:24:34 +0100206 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
207 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
208 CHECK(headerWord1 == 14); // data lenght
209 CHECK(period == 1000); // capture period
Ferran Balaguer73882172019-09-02 16:39:42 +0100210
211 uint16_t counterId = 0;
212 uint32_t offset = 12;
213
214 // Counter Ids
215 for(const uint16_t& id : selectedCounterIds)
216 {
217 counterId = ReadUint16(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100218 CHECK(counterId == id);
Ferran Balaguer73882172019-09-02 16:39:42 +0100219 offset += 2;
220 }
221}
222
Sadik Armagan1625efc2021-06-10 18:24:34 +0100223TEST_CASE("SendPeriodicCounterCapturePacketTest")
Francis Murtagh3a161982019-09-04 15:25:02 +0100224{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100225 ProfilingStateMachine profilingStateMachine;
226
Francis Murtagh3a161982019-09-04 15:25:02 +0100227 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100228 MockBufferManager mockBuffer1(10);
Jim Flynn9c85b412022-03-16 00:27:43 +0000229 SendCounterPacket sendPacket1(mockBuffer1,
230 arm::pipe::ARMNN_SOFTWARE_INFO,
231 arm::pipe::ARMNN_SOFTWARE_VERSION,
232 arm::pipe::ARMNN_HARDWARE_VERSION);
Francis Murtagh3a161982019-09-04 15:25:02 +0100233
234 auto captureTimestamp = std::chrono::steady_clock::now();
235 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
Finn Williams032bc742020-02-12 11:02:34 +0000236 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100237
Sadik Armagan1625efc2021-06-10 18:24:34 +0100238 CHECK_THROWS_AS(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
Francis Murtagh3a161982019-09-04 15:25:02 +0100239 BufferExhaustion);
240
241 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100242 MockBufferManager mockBuffer2(512);
Jim Flynn9c85b412022-03-16 00:27:43 +0000243 SendCounterPacket sendPacket2(mockBuffer2,
244 arm::pipe::ARMNN_SOFTWARE_INFO,
245 arm::pipe::ARMNN_SOFTWARE_VERSION,
246 arm::pipe::ARMNN_HARDWARE_VERSION);
Francis Murtagh3a161982019-09-04 15:25:02 +0100247
248 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100249 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100250
251 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
252 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
253 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
254
Sadik Armagan1625efc2021-06-10 18:24:34 +0100255 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
256 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
257 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
258 CHECK(headerWord1 == 8); // data length
259 CHECK(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100260
261 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100262 MockBufferManager mockBuffer3(512);
Jim Flynn9c85b412022-03-16 00:27:43 +0000263 SendCounterPacket sendPacket3(mockBuffer3,
264 arm::pipe::ARMNN_SOFTWARE_INFO,
265 arm::pipe::ARMNN_SOFTWARE_VERSION,
266 arm::pipe::ARMNN_HARDWARE_VERSION);
Francis Murtagh3a161982019-09-04 15:25:02 +0100267
268 indexValuePairs.reserve(5);
Finn Williams032bc742020-02-12 11:02:34 +0000269 indexValuePairs.emplace_back(CounterValue{0, 100});
270 indexValuePairs.emplace_back(CounterValue{1, 200});
271 indexValuePairs.emplace_back(CounterValue{2, 300});
272 indexValuePairs.emplace_back(CounterValue{3, 400});
273 indexValuePairs.emplace_back(CounterValue{4, 500});
Francis Murtagh3a161982019-09-04 15:25:02 +0100274 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100275 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100276
277 headerWord0 = ReadUint32(readBuffer3, 0);
278 headerWord1 = ReadUint32(readBuffer3, 4);
279 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
280
Sadik Armagan1625efc2021-06-10 18:24:34 +0100281 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
282 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
283 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
284 CHECK(headerWord1 == 38); // data length
285 CHECK(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100286
287 uint16_t counterIndex = 0;
288 uint32_t counterValue = 100;
289 uint32_t offset = 16;
290
291 // Counter Ids
292 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
293 {
294 // Check Counter Index
295 uint16_t readIndex = ReadUint16(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100296 CHECK(counterIndex == readIndex);
Francis Murtagh3a161982019-09-04 15:25:02 +0100297 counterIndex++;
298 offset += 2;
299
300 // Check Counter Value
301 uint32_t readValue = ReadUint32(readBuffer3, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100302 CHECK(counterValue == readValue);
Francis Murtagh3a161982019-09-04 15:25:02 +0100303 counterValue += 100;
304 offset += 4;
305 }
306
307}
308
Sadik Armagan1625efc2021-06-10 18:24:34 +0100309TEST_CASE("SendStreamMetaDataPacketTest")
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100310{
Jim Flynn75c14f42022-03-10 22:05:42 +0000311 uint32_t sizeUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100312
313 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100314 MockBufferManager mockBuffer1(10);
Jim Flynn9c85b412022-03-16 00:27:43 +0000315 SendCounterPacket sendPacket1(mockBuffer1,
316 arm::pipe::ARMNN_SOFTWARE_INFO,
317 arm::pipe::ARMNN_SOFTWARE_VERSION,
318 arm::pipe::ARMNN_HARDWARE_VERSION);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000319 CHECK_THROWS_AS(sendPacket1.SendStreamMetaDataPacket(), BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100320
321 // Full metadata packet
322
323 std::string processName = GetProcessName().substr(0, 60);
324
Jim Flynn9c85b412022-03-16 00:27:43 +0000325 uint32_t infoSize = arm::pipe::numeric_cast<uint32_t>(arm::pipe::ARMNN_SOFTWARE_INFO.size()) + 1;
326 uint32_t hardwareVersionSize = arm::pipe::numeric_cast<uint32_t>(arm::pipe::ARMNN_HARDWARE_VERSION.size()) + 1;
327 uint32_t softwareVersionSize = arm::pipe::numeric_cast<uint32_t>(arm::pipe::ARMNN_SOFTWARE_VERSION.size()) + 1;
Jim Flynn75c14f42022-03-10 22:05:42 +0000328 uint32_t processNameSize = arm::pipe::numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100329
Jim Flynn83d08a92020-07-09 13:48:16 +0100330 // Supported Packets
331 // Packet Encoding version 1.0.0
332 // Control packet family
333 // Stream metadata packet (packet family=0; packet id=0)
334 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
335 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
336 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
337 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
338 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
339 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
340 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
341 // Counter Packet Family
342 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
343 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
344 // Timeline Packet Family
345 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
346 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
347 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100348 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
349 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
350 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
351 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
352 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
353 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
354 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
355 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
356 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
357 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
358 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
359 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
360 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Jim Flynn83d08a92020-07-09 13:48:16 +0100361
362 uint32_t packetEntries = static_cast<uint32_t>(packetVersions.size());
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100363
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100364 MockBufferManager mockBuffer2(512);
Jim Flynn9c85b412022-03-16 00:27:43 +0000365 SendCounterPacket sendPacket2(mockBuffer2,
366 arm::pipe::ARMNN_SOFTWARE_INFO,
367 arm::pipe::ARMNN_SOFTWARE_VERSION,
368 arm::pipe::ARMNN_HARDWARE_VERSION);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100369 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100370 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100371
372 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
373 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
374
Sadik Armagan1625efc2021-06-10 18:24:34 +0100375 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
376 CHECK(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100377
Jim Flynn75c14f42022-03-10 22:05:42 +0000378 uint32_t totalLength = arm::pipe::numeric_cast<uint32_t>(2 * sizeUint32 +
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100379 10 * sizeUint32 + infoSize +
380 hardwareVersionSize + softwareVersionSize +
381 processNameSize + sizeUint32 +
382 2 * packetEntries * sizeUint32);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100383
Sadik Armagan1625efc2021-06-10 18:24:34 +0100384 CHECK(headerWord1 == totalLength - (2 * sizeUint32)); // data length
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100385
386 uint32_t offset = sizeUint32 * 2;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100387 CHECK(ReadUint32(readBuffer2, offset) == arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100388 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100389 CHECK(ReadUint32(readBuffer2, offset) == arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100390 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100391 CHECK(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100392 offset += sizeUint32;
Jim Flynn9c85b412022-03-16 00:27:43 +0000393 int pid = arm::pipe::GetCurrentProcessId();
Jim Flynn75c14f42022-03-10 22:05:42 +0000394 CHECK(ReadUint32(readBuffer2, offset) == arm::pipe::numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100395 offset += sizeUint32;
396 uint32_t poolOffset = 10 * sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100397 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100398 offset += sizeUint32;
399 poolOffset += infoSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100400 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100401 offset += sizeUint32;
402 poolOffset += hardwareVersionSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100403 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100404 offset += sizeUint32;
405 poolOffset += softwareVersionSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100406 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100407 offset += sizeUint32;
408 poolOffset += processNameSize;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100409 CHECK(ReadUint32(readBuffer2, offset) == poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100410 offset += sizeUint32;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100411 CHECK(ReadUint32(readBuffer2, offset) == 0); // reserved
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100412
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100413 const unsigned char* readData2 = readBuffer2->GetReadableData();
414
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100415 offset += sizeUint32;
416 if (infoSize)
417 {
Jim Flynn9c85b412022-03-16 00:27:43 +0000418 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]),
419 arm::pipe::ARMNN_SOFTWARE_INFO.c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100420 offset += infoSize;
421 }
422
423 if (hardwareVersionSize)
424 {
Jim Flynn9c85b412022-03-16 00:27:43 +0000425 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]),
426 arm::pipe::ARMNN_HARDWARE_VERSION.c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100427 offset += hardwareVersionSize;
428 }
429
430 if (softwareVersionSize)
431 {
Jim Flynn9c85b412022-03-16 00:27:43 +0000432 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]),
433 arm::pipe::ARMNN_SOFTWARE_VERSION.c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100434 offset += softwareVersionSize;
435 }
436
437 if (processNameSize)
438 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100439 CHECK(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100440 offset += processNameSize;
441 }
442
443 if (packetEntries)
444 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100445 uint32_t numberOfEntries = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100446 CHECK((numberOfEntries >> 16) == packetEntries);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100447 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100448 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100449 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100450 uint32_t readPacketId = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100451 CHECK(packetVersion.first == readPacketId);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100452 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100453 uint32_t readVersion = ReadUint32(readBuffer2, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100454 CHECK(packetVersion.second == readVersion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100455 offset += sizeUint32;
456 }
457 }
458
Sadik Armagan1625efc2021-06-10 18:24:34 +0100459 CHECK(offset == totalLength);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100460}
461
Sadik Armagan1625efc2021-06-10 18:24:34 +0100462TEST_CASE("CreateDeviceRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100463{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100464 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000465 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100466
467 // Create a device for testing
468 uint16_t deviceUid = 27;
469 const std::string deviceName = "some_device";
470 uint16_t deviceCores = 3;
471 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
472
473 // Create a device record
474 SendCounterPacket::DeviceRecord deviceRecord;
475 std::string errorMessage;
476 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
477
Sadik Armagan1625efc2021-06-10 18:24:34 +0100478 CHECK(result);
479 CHECK(errorMessage.empty());
480 CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100481
482 uint16_t deviceRecordWord0[]
483 {
484 static_cast<uint16_t>(deviceRecord[0] >> 16),
485 static_cast<uint16_t>(deviceRecord[0])
486 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100487 CHECK(deviceRecordWord0[0] == deviceUid); // uid
488 CHECK(deviceRecordWord0[1] == deviceCores); // cores
489 CHECK(deviceRecord[1] == 8); // name_offset
490 CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
491 CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100492}
493
Sadik Armagan1625efc2021-06-10 18:24:34 +0100494TEST_CASE("CreateInvalidDeviceRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100495{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100496 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000497 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100498
499 // Create a device for testing
500 uint16_t deviceUid = 27;
501 const std::string deviceName = "some€£invalid‡device";
502 uint16_t deviceCores = 3;
503 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
504
505 // Create a device record
506 SendCounterPacket::DeviceRecord deviceRecord;
507 std::string errorMessage;
508 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
509
Sadik Armagan1625efc2021-06-10 18:24:34 +0100510 CHECK(!result);
511 CHECK(!errorMessage.empty());
512 CHECK(deviceRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100513}
514
Sadik Armagan1625efc2021-06-10 18:24:34 +0100515TEST_CASE("CreateCounterSetRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100516{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100517 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000518 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100519
520 // Create a counter set for testing
521 uint16_t counterSetUid = 27;
522 const std::string counterSetName = "some_counter_set";
523 uint16_t counterSetCount = 3421;
524 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
525
526 // Create a counter set record
527 SendCounterPacket::CounterSetRecord counterSetRecord;
528 std::string errorMessage;
529 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
530
Sadik Armagan1625efc2021-06-10 18:24:34 +0100531 CHECK(result);
532 CHECK(errorMessage.empty());
533 CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100534
535 uint16_t counterSetRecordWord0[]
536 {
537 static_cast<uint16_t>(counterSetRecord[0] >> 16),
538 static_cast<uint16_t>(counterSetRecord[0])
539 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100540 CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
541 CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
542 CHECK(counterSetRecord[1] == 8); // name_offset
543 CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
544 CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100545}
546
Sadik Armagan1625efc2021-06-10 18:24:34 +0100547TEST_CASE("CreateInvalidCounterSetRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100548{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100549 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000550 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100551
552 // Create a counter set for testing
553 uint16_t counterSetUid = 27;
554 const std::string counterSetName = "some invalid_counter€£set";
555 uint16_t counterSetCount = 3421;
556 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
557
558 // Create a counter set record
559 SendCounterPacket::CounterSetRecord counterSetRecord;
560 std::string errorMessage;
561 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
562
Sadik Armagan1625efc2021-06-10 18:24:34 +0100563 CHECK(!result);
564 CHECK(!errorMessage.empty());
565 CHECK(counterSetRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100566}
567
Sadik Armagan1625efc2021-06-10 18:24:34 +0100568TEST_CASE("CreateEventRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100569{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100570 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000571 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100572
573 // Create a counter for testing
574 uint16_t counterUid = 7256;
575 uint16_t maxCounterUid = 132;
576 uint16_t deviceUid = 132;
577 uint16_t counterSetUid = 4497;
578 uint16_t counterClass = 1;
579 uint16_t counterInterpolation = 1;
580 double counterMultiplier = 1234.567f;
581 const std::string counterName = "some_valid_counter";
582 const std::string counterDescription = "a_counter_for_testing";
583 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000584 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
585 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100586 maxCounterUid,
587 counterClass,
588 counterInterpolation,
589 counterMultiplier,
590 counterName,
591 counterDescription,
592 counterUnits,
593 deviceUid,
594 counterSetUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000595 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100596
597 // Create an event record
598 SendCounterPacket::EventRecord eventRecord;
599 std::string errorMessage;
600 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
601
Sadik Armagan1625efc2021-06-10 18:24:34 +0100602 CHECK(result);
603 CHECK(errorMessage.empty());
604 CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100605
606 uint16_t eventRecordWord0[]
607 {
608 static_cast<uint16_t>(eventRecord[0] >> 16),
609 static_cast<uint16_t>(eventRecord[0])
610 };
611 uint16_t eventRecordWord1[]
612 {
613 static_cast<uint16_t>(eventRecord[1] >> 16),
614 static_cast<uint16_t>(eventRecord[1])
615 };
616 uint16_t eventRecordWord2[]
617 {
618 static_cast<uint16_t>(eventRecord[2] >> 16),
619 static_cast<uint16_t>(eventRecord[2])
620 };
621 uint32_t eventRecordWord34[]
622 {
623 eventRecord[3],
624 eventRecord[4]
625 };
Finn Williamsd44815f2020-05-01 13:25:55 +0100626
Sadik Armagan1625efc2021-06-10 18:24:34 +0100627 CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
628 CHECK(eventRecordWord0[1] == counterUid); // counter_uid
629 CHECK(eventRecordWord1[0] == deviceUid); // device
Finn Williamsd44815f2020-05-01 13:25:55 +0100630
Sadik Armagan1625efc2021-06-10 18:24:34 +0100631 CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
632 CHECK(eventRecordWord2[0] == counterClass); // class
633 CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
634 CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100635
Jim Flynn0a013a72022-03-13 16:57:04 +0000636 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100637 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
638 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100639 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
640 4u + // Counter name length (uint32_t)
641 counterName.size() + // 18u
642 1u + // Null-terminator
643 1u; // Rounding to the next word
Finn Williamsd44815f2020-05-01 13:25:55 +0100644
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100645 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
646 4u + // Counter description length (uint32_t)
647 counterDescription.size() + // 21u
648 1u + // Null-terminator
Finn Williamsd44815f2020-05-01 13:25:55 +0100649 2u; // Rounding to the next word
650
Jim Flynn0a013a72022-03-13 16:57:04 +0000651 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100652
Sadik Armagan1625efc2021-06-10 18:24:34 +0100653 CHECK(eventRecord[5] == counterNameOffset); // name_offset
654 CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
655 CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100656
Finn Williamsd44815f2020-05-01 13:25:55 +0100657 // Offsets are relative to the start of the eventRecord
658 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100659 size_t uint32_t_size = sizeof(uint32_t);
660
661 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100662 CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100663 // The counter name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100664 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100665 counterNameOffset + // Offset
666 uint32_t_size /* The length of the name */,
667 counterName.data(),
668 counterName.size()) == 0); // name
669 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100670 CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100671
672 // The length of the SWTrace string (description)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100673 CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100674 // The counter description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100675 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100676 counterDescriptionOffset + // Offset
677 uint32_t_size /* The length of the description */,
678 counterDescription.data(),
679 counterDescription.size()) == 0); // description
680 // The null-terminator at the end of the description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100681 CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100682
683 // The length of the SWTrace namestring (units)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100684 CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100685 // The counter units
Sadik Armagan1625efc2021-06-10 18:24:34 +0100686 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100687 counterUnitsOffset + // Offset
688 uint32_t_size /* The length of the units */,
689 counterUnits.data(),
690 counterUnits.size()) == 0); // units
691 // The null-terminator at the end of the units
Sadik Armagan1625efc2021-06-10 18:24:34 +0100692 CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100693}
694
Sadik Armagan1625efc2021-06-10 18:24:34 +0100695TEST_CASE("CreateEventRecordNoUnitsTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100696{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100697 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000698 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100699
700 // Create a counter for testing
701 uint16_t counterUid = 44312;
702 uint16_t maxCounterUid = 345;
703 uint16_t deviceUid = 101;
704 uint16_t counterSetUid = 34035;
705 uint16_t counterClass = 0;
706 uint16_t counterInterpolation = 1;
707 double counterMultiplier = 4435.0023f;
708 const std::string counterName = "some_valid_counter";
709 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000710 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
711 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100712 maxCounterUid,
713 counterClass,
714 counterInterpolation,
715 counterMultiplier,
716 counterName,
717 counterDescription,
718 "",
719 deviceUid,
720 counterSetUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000721 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100722
723 // Create an event record
724 SendCounterPacket::EventRecord eventRecord;
725 std::string errorMessage;
726 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
727
Sadik Armagan1625efc2021-06-10 18:24:34 +0100728 CHECK(result);
729 CHECK(errorMessage.empty());
730 CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100731
732 uint16_t eventRecordWord0[]
733 {
734 static_cast<uint16_t>(eventRecord[0] >> 16),
735 static_cast<uint16_t>(eventRecord[0])
736 };
737 uint16_t eventRecordWord1[]
738 {
739 static_cast<uint16_t>(eventRecord[1] >> 16),
740 static_cast<uint16_t>(eventRecord[1])
741 };
742 uint16_t eventRecordWord2[]
743 {
744 static_cast<uint16_t>(eventRecord[2] >> 16),
745 static_cast<uint16_t>(eventRecord[2])
746 };
747 uint32_t eventRecordWord34[]
748 {
749 eventRecord[3],
750 eventRecord[4]
751 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100752 CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
753 CHECK(eventRecordWord0[1] == counterUid); // counter_uid
754 CHECK(eventRecordWord1[0] == deviceUid); // device
755 CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
756 CHECK(eventRecordWord2[0] == counterClass); // class
757 CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
758 CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100759
Jim Flynn0a013a72022-03-13 16:57:04 +0000760 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100761 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
762 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100763 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
764 4u + // Counter name length (uint32_t)
765 counterName.size() + // 18u
766 1u + // Null-terminator
767 1u; // Rounding to the next word
Jim Flynn0a013a72022-03-13 16:57:04 +0000768 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100769
Sadik Armagan1625efc2021-06-10 18:24:34 +0100770 CHECK(eventRecord[5] == counterNameOffset); // name_offset
771 CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
772 CHECK(eventRecord[7] == 0); // units_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100773
Finn Williamsd44815f2020-05-01 13:25:55 +0100774 // Offsets are relative to the start of the eventRecord
775 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100776 size_t uint32_t_size = sizeof(uint32_t);
777
778 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100779 CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100780 // The counter name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100781 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100782 counterNameOffset + // Offset
783 uint32_t_size, // The length of the name
784 counterName.data(),
785 counterName.size()) == 0); // name
786 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100787 CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100788
789 // The length of the SWTrace string (description)
Sadik Armagan1625efc2021-06-10 18:24:34 +0100790 CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791 // The counter description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100792 CHECK(std::memcmp(eventRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100793 counterDescriptionOffset + // Offset
794 uint32_t_size, // The length of the description
795 counterDescription.data(),
796 counterDescription.size()) == 0); // description
797 // The null-terminator at the end of the description
Sadik Armagan1625efc2021-06-10 18:24:34 +0100798 CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100799}
800
Sadik Armagan1625efc2021-06-10 18:24:34 +0100801TEST_CASE("CreateInvalidEventRecordTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100802{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100803 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000804 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100805
806 // Create a counter for testing
807 uint16_t counterUid = 7256;
808 uint16_t maxCounterUid = 132;
809 uint16_t deviceUid = 132;
810 uint16_t counterSetUid = 4497;
811 uint16_t counterClass = 1;
812 uint16_t counterInterpolation = 1;
813 double counterMultiplier = 1234.567f;
814 const std::string counterName = "some_invalid_counter £££"; // Invalid name
815 const std::string counterDescription = "a_counter_for_testing";
816 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000817 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
818 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100819 maxCounterUid,
820 counterClass,
821 counterInterpolation,
822 counterMultiplier,
823 counterName,
824 counterDescription,
825 counterUnits,
826 deviceUid,
827 counterSetUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000828 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100829
830 // Create an event record
831 SendCounterPacket::EventRecord eventRecord;
832 std::string errorMessage;
833 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
834
Sadik Armagan1625efc2021-06-10 18:24:34 +0100835 CHECK(!result);
836 CHECK(!errorMessage.empty());
837 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100838}
839
Sadik Armagan1625efc2021-06-10 18:24:34 +0100840TEST_CASE("CreateInvalidEventRecordTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100841{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100842 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000843 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100844
845 // Create a counter for testing
846 uint16_t counterUid = 7256;
847 uint16_t maxCounterUid = 132;
848 uint16_t deviceUid = 132;
849 uint16_t counterSetUid = 4497;
850 uint16_t counterClass = 1;
851 uint16_t counterInterpolation = 1;
852 double counterMultiplier = 1234.567f;
853 const std::string counterName = "some_invalid_counter";
854 const std::string counterDescription = "an invalid d€scription"; // Invalid description
855 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000856 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
857 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100858 maxCounterUid,
859 counterClass,
860 counterInterpolation,
861 counterMultiplier,
862 counterName,
863 counterDescription,
864 counterUnits,
865 deviceUid,
866 counterSetUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000867 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100868
869 // Create an event record
870 SendCounterPacket::EventRecord eventRecord;
871 std::string errorMessage;
872 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
873
Sadik Armagan1625efc2021-06-10 18:24:34 +0100874 CHECK(!result);
875 CHECK(!errorMessage.empty());
876 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100877}
878
Sadik Armagan1625efc2021-06-10 18:24:34 +0100879TEST_CASE("CreateInvalidEventRecordTest3")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100880{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100881 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000882 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100883
884 // Create a counter for testing
885 uint16_t counterUid = 7256;
886 uint16_t maxCounterUid = 132;
887 uint16_t deviceUid = 132;
888 uint16_t counterSetUid = 4497;
889 uint16_t counterClass = 1;
890 uint16_t counterInterpolation = 1;
891 double counterMultiplier = 1234.567f;
892 const std::string counterName = "some_invalid_counter";
893 const std::string counterDescription = "a valid description";
894 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000895 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
896 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100897 maxCounterUid,
898 counterClass,
899 counterInterpolation,
900 counterMultiplier,
901 counterName,
902 counterDescription,
903 counterUnits,
904 deviceUid,
905 counterSetUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000906 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100907
908 // Create an event record
909 SendCounterPacket::EventRecord eventRecord;
910 std::string errorMessage;
911 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
912
Sadik Armagan1625efc2021-06-10 18:24:34 +0100913 CHECK(!result);
914 CHECK(!errorMessage.empty());
915 CHECK(eventRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100916}
917
Sadik Armagan1625efc2021-06-10 18:24:34 +0100918TEST_CASE("CreateCategoryRecordTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100919{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100920 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000921 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100922
923 // Create a category for testing
924 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +0000925 const CategoryPtr category = std::make_unique<Category>(categoryName);
Jim Flynn6730fe92022-03-10 22:57:47 +0000926 ARM_PIPE_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100927 category->m_Counters = { 11u, 23u, 5670u };
928
929 // Create a collection of counters
930 Counters counters;
931 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000932 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100933 0,
Keith Davise394bd92019-12-02 15:12:19 +0000934 11,
935 0,
936 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100937 534.0003f,
938 "counter1",
939 "the first counter",
940 "millipi2",
941 0,
942 0))));
943 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000944 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100945 1,
Keith Davise394bd92019-12-02 15:12:19 +0000946 23,
947 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100948 1,
949 534.0003f,
950 "this is counter 2",
951 "the second counter",
952 "",
953 0,
954 0))));
955 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000956 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
957 2,
958 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100959 0,
960 0,
961 534.0003f,
962 "and this is number 3",
963 "the third counter",
964 "blah_per_second",
965 0,
966 0))));
967 Counter* counter1 = counters.find(11)->second.get();
968 Counter* counter2 = counters.find(23)->second.get();
969 Counter* counter3 = counters.find(5670)->second.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000970 ARM_PIPE_ASSERT(counter1);
971 ARM_PIPE_ASSERT(counter2);
972 ARM_PIPE_ASSERT(counter3);
973 uint16_t categoryEventCount = armnn::numeric_cast<uint16_t>(counters.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100974
975 // Create a category record
976 SendCounterPacket::CategoryRecord categoryRecord;
977 std::string errorMessage;
978 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
979
Sadik Armagan1625efc2021-06-10 18:24:34 +0100980 CHECK(result);
981 CHECK(errorMessage.empty());
982 CHECK(categoryRecord.size() == 79); // Size in words: header [3] + event pointer table [3] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100983 // category name [5] + event records [68 = 22 + 20 + 26]
984
Sadik Armagan4c998992020-02-25 12:44:44 +0000985 uint16_t categoryRecordWord1[]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100986 {
987 static_cast<uint16_t>(categoryRecord[0] >> 16),
988 static_cast<uint16_t>(categoryRecord[0])
989 };
Sadik Armagan1625efc2021-06-10 18:24:34 +0100990 CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
991 CHECK(categoryRecordWord1[1] == 0); // reserved
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100992
993 size_t uint32_t_size = sizeof(uint32_t);
994
Jim Flynn0a013a72022-03-13 16:57:04 +0000995 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100996 uint32_t categoryRecordBlockSize = 3u * uint32_t_size;
997 uint32_t eventPointerTableOffset = categoryRecordBlockSize; // The event pointer table is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100998 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
999 categoryEventCount * uint32_t_size; // The size of the event pointer table
Jim Flynn0a013a72022-03-13 16:57:04 +00001000 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001001
Sadik Armagan1625efc2021-06-10 18:24:34 +01001002 CHECK(categoryRecord[1] == eventPointerTableOffset); // event_pointer_table_offset
1003 CHECK(categoryRecord[2] == categoryNameOffset); // name_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001004 // Offsets are relative to the start of the category record
1005 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001006
1007 // The event pointer table
1008 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
1009 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
1010 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
Sadik Armagan1625efc2021-06-10 18:24:34 +01001011 CHECK(eventRecord0Offset == 32);
1012 CHECK(eventRecord1Offset == 120);
1013 CHECK(eventRecord2Offset == 200);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001014
1015 // The length of the SWTrace namestring (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +01001016 CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001017 // The category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001018 CHECK(std::memcmp(categoryRecordPool +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001019 categoryNameOffset + // Offset
1020 uint32_t_size, // The length of the name
1021 categoryName.data(),
1022 categoryName.size()) == 0); // name
1023 // The null-terminator at the end of the name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001024 CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001025
1026 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
1027 // as the event records already have a number of unit tests dedicated to them
1028
1029 // Counter1 UID and max counter UID
1030 uint16_t eventRecord0Word0[2] = { 0u, 0u };
Finn Williamsd44815f2020-05-01 13:25:55 +01001031 std::memcpy(eventRecord0Word0, categoryRecordPool + categoryRecordBlockSize + eventRecord0Offset,
1032 sizeof(eventRecord0Word0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001033 CHECK(eventRecord0Word0[0] == counter1->m_Uid);
1034 CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001035
1036 // Counter1 name
1037 uint32_t counter1NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001038 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001039 CHECK(counter1NameOffset == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001040 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +01001041 CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001042 categoryRecordBlockSize + // Offset to the end of the category record block
1043 8u * uint32_t_size + // Offset to the event record pool
1044 counter1NameOffset // Offset to the name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001045 ] == counter1->m_Name.size() + 1); // The length of the name including the
1046 // null-terminator
1047 // The counter1 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001048 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001049 categoryRecordBlockSize + // Offset to the end of the category record block
1050 eventRecord0Offset + // Offset to the event record
1051 8u * uint32_t_size + // Offset to the event record pool
1052 counter1NameOffset + // Offset to the name of the counter
1053 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001054 counter1->m_Name.data(),
1055 counter1->m_Name.size()) == 0); // name
1056 // The null-terminator at the end of the counter1 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001057 CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001058 categoryRecordBlockSize + // Offset to the end of the category record block
1059 8u * uint32_t_size + // Offset to the event record pool
1060 counter1NameOffset + // Offset to the name of the counter
1061 uint32_t_size + // The length of the name
1062 counter1->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001063 ] == '\0');
1064
1065 // Counter2 name
1066 uint32_t counter2NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001067 std::memcpy(&counter2NameOffset, categoryRecordPool +
1068 categoryRecordBlockSize +
1069 eventRecord1Offset +
1070 5u * uint32_t_size,
1071 uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001072 CHECK(counter2NameOffset == 8u * uint32_t_size );
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001073 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001074
Sadik Armagan1625efc2021-06-10 18:24:34 +01001075 CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001076 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001077 counter2NameOffset // Offset to the name of the counter
1078 ] == counter2->m_Name.size() + 1); // The length of the name including the
1079 // null-terminator
1080 // The counter2 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001081 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001082 categoryRecordBlockSize + // Offset to the end of the category record block
1083 eventRecord1Offset + // Offset to the event record
1084 counter2NameOffset + // Offset to the name of the counter
1085 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001086 counter2->m_Name.data(),
1087 counter2->m_Name.size()) == 0); // name
Finn Williamsd44815f2020-05-01 13:25:55 +01001088
1089
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001090 // The null-terminator at the end of the counter2 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001091 CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001092 categoryRecordBlockSize + // Offset to the end of the category record block
1093 counter2NameOffset + // Offset to the name of the counter
1094 uint32_t_size + // The length of the name
1095 counter2->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001096 ] == '\0');
1097
1098 // Counter3 name
1099 uint32_t counter3NameOffset = 0;
1100 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001101 CHECK(counter3NameOffset == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001102 // The length of the SWTrace string (name)
Sadik Armagan1625efc2021-06-10 18:24:34 +01001103 CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001104 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001105 8u * uint32_t_size + // Offset to the event record pool
1106 counter3NameOffset // Offset to the name of the counter
1107 ] == counter3->m_Name.size() + 1); // The length of the name including the
1108 // null-terminator
1109 // The counter3 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001110 CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001111 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001112 eventRecord2Offset + // Offset to the event record
1113 8u * uint32_t_size + // Offset to the event record pool
1114 counter3NameOffset + // Offset to the name of the counter
1115 uint32_t_size, // The length of the name
1116 counter3->m_Name.data(),
1117 counter3->m_Name.size()) == 0); // name
1118 // The null-terminator at the end of the counter3 name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001119 CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001120 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001121 8u * uint32_t_size + // Offset to the event record pool
1122 counter3NameOffset + // Offset to the name of the counter
1123 uint32_t_size + // The length of the name
1124 counter3->m_Name.size() // The name of the counter
1125 ] == '\0');
1126}
1127
Sadik Armagan1625efc2021-06-10 18:24:34 +01001128TEST_CASE("CreateInvalidCategoryRecordTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001129{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001130 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001131 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001132
1133 // Create a category for testing
1134 const std::string categoryName = "some invalid category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001135 const CategoryPtr category = std::make_unique<Category>(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001136 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001137
1138 // Create a category record
1139 Counters counters;
1140 SendCounterPacket::CategoryRecord categoryRecord;
1141 std::string errorMessage;
1142 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1143
Sadik Armagan1625efc2021-06-10 18:24:34 +01001144 CHECK(!result);
1145 CHECK(!errorMessage.empty());
1146 CHECK(categoryRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001147}
1148
Sadik Armagan1625efc2021-06-10 18:24:34 +01001149TEST_CASE("CreateInvalidCategoryRecordTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001150{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001151 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001152 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001153
1154 // Create a category for testing
1155 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001156 const CategoryPtr category = std::make_unique<Category>(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001157 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001158 category->m_Counters = { 11u, 23u, 5670u };
1159
1160 // Create a collection of counters
1161 Counters counters;
1162 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001163 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1164 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001165 1234,
1166 0,
1167 1,
1168 534.0003f,
1169 "count€r1", // Invalid name
1170 "the first counter",
1171 "millipi2",
1172 0,
1173 0))));
1174
1175 Counter* counter1 = counters.find(11)->second.get();
Sadik Armagan1625efc2021-06-10 18:24:34 +01001176 CHECK(counter1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001177
1178 // Create a category record
1179 SendCounterPacket::CategoryRecord categoryRecord;
1180 std::string errorMessage;
1181 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1182
Sadik Armagan1625efc2021-06-10 18:24:34 +01001183 CHECK(!result);
1184 CHECK(!errorMessage.empty());
1185 CHECK(categoryRecord.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001186}
1187
Sadik Armagan1625efc2021-06-10 18:24:34 +01001188TEST_CASE("SendCounterDirectoryPacketTest1")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001189{
1190 // The counter directory used for testing
1191 CounterDirectory counterDirectory;
1192
1193 // Register a device
1194 const std::string device1Name = "device1";
1195 const Device* device1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001196 CHECK_NOTHROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1197 CHECK(counterDirectory.GetDeviceCount() == 1);
1198 CHECK(device1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001199
1200 // Register a device
1201 const std::string device2Name = "device2";
1202 const Device* device2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001203 CHECK_NOTHROW(device2 = counterDirectory.RegisterDevice(device2Name));
1204 CHECK(counterDirectory.GetDeviceCount() == 2);
1205 CHECK(device2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001206
1207 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001208 MockBufferManager mockBuffer(10);
Jim Flynn9c85b412022-03-16 00:27:43 +00001209 SendCounterPacket sendCounterPacket(mockBuffer,
1210 arm::pipe::ARMNN_SOFTWARE_INFO,
1211 arm::pipe::ARMNN_SOFTWARE_VERSION,
1212 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001213 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001214 BufferExhaustion);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001215}
1216
Sadik Armagan1625efc2021-06-10 18:24:34 +01001217TEST_CASE("SendCounterDirectoryPacketTest2")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001218{
1219 // The counter directory used for testing
1220 CounterDirectory counterDirectory;
1221
1222 // Register a device
1223 const std::string device1Name = "device1";
1224 const Device* device1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001225 CHECK_NOTHROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1226 CHECK(counterDirectory.GetDeviceCount() == 1);
1227 CHECK(device1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001228
1229 // Register a device
1230 const std::string device2Name = "device2";
1231 const Device* device2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001232 CHECK_NOTHROW(device2 = counterDirectory.RegisterDevice(device2Name));
1233 CHECK(counterDirectory.GetDeviceCount() == 2);
1234 CHECK(device2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001235
1236 // Register a counter set
1237 const std::string counterSet1Name = "counterset1";
1238 const CounterSet* counterSet1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001239 CHECK_NOTHROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1240 CHECK(counterDirectory.GetCounterSetCount() == 1);
1241 CHECK(counterSet1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001242
1243 // Register a category associated to "device1" and "counterset1"
1244 const std::string category1Name = "category1";
1245 const Category* category1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001246 CHECK_NOTHROW(category1 = counterDirectory.RegisterCategory(category1Name));
1247 CHECK(counterDirectory.GetCategoryCount() == 1);
1248 CHECK(category1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001249
1250 // Register a category not associated to "device2" but no counter set
1251 const std::string category2Name = "category2";
1252 const Category* category2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001253 CHECK_NOTHROW(category2 = counterDirectory.RegisterCategory(category2Name));
1254 CHECK(counterDirectory.GetCategoryCount() == 2);
1255 CHECK(category2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001256
Keith Davis33ed2212020-03-30 10:43:41 +01001257 uint16_t numberOfCores = 4;
Sadik Armagan4c998992020-02-25 12:44:44 +00001258
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001259 // Register a counter associated to "category1"
1260 const Counter* counter1 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001261 CHECK_NOTHROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001262 0,
1263 category1Name,
1264 0,
1265 1,
1266 123.45f,
1267 "counter1",
1268 "counter1description",
1269 std::string("counter1units"),
1270 numberOfCores));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001271 CHECK(counterDirectory.GetCounterCount() == 4);
1272 CHECK(counter1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001273
1274 // Register a counter associated to "category1"
1275 const Counter* counter2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001276 CHECK_NOTHROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001277 4,
1278 category1Name,
1279 1,
1280 0,
1281 330.1245656765f,
1282 "counter2",
1283 "counter2description",
1284 std::string("counter2units"),
Jim Flynndecd08b2022-03-13 22:35:46 +00001285 arm::pipe::EmptyOptional(),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001286 device2->m_Uid,
1287 0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001288 CHECK(counterDirectory.GetCounterCount() == 5);
1289 CHECK(counter2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001290
1291 // Register a counter associated to "category2"
1292 const Counter* counter3 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001293 CHECK_NOTHROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001294 5,
1295 category2Name,
1296 1,
1297 1,
1298 0.0000045399f,
1299 "counter3",
1300 "counter3description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001301 arm::pipe::EmptyOptional(),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001302 numberOfCores,
1303 device2->m_Uid,
1304 counterSet1->m_Uid));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001305 CHECK(counterDirectory.GetCounterCount() == 9);
1306 CHECK(counter3);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001307
1308 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001309 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001310 SendCounterPacket sendCounterPacket(mockBuffer,
1311 arm::pipe::ARMNN_SOFTWARE_INFO,
1312 arm::pipe::ARMNN_SOFTWARE_VERSION,
1313 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001314 CHECK_NOTHROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001315
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001316 // Get the readable buffer
1317 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001318
1319 // Check the packet header
Finn Williams985fecf2020-04-30 11:06:43 +01001320 const uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1321 const uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001322 CHECK(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1323 CHECK(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
1324 CHECK(packetHeaderWord1 == 432); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001325
1326 // Check the body header
Finn Williams985fecf2020-04-30 11:06:43 +01001327 const uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1328 const uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1329 const uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1330 const uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1331 const uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1332 const uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1333 const uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1334 const uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1335 const uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001336 CHECK(deviceRecordCount == 2); // device_records_count
1337 CHECK(bodyHeaderWord1 == bodyHeaderSize * 4); // device_records_pointer_table_offset
1338 CHECK(counterSetRecordCount == 1); // counter_set_count
1339 CHECK(bodyHeaderWord3 == 8 + bodyHeaderSize * 4); // counter_set_pointer_table_offset
1340 CHECK(categoryRecordCount == 2); // categories_count
1341 CHECK(bodyHeaderWord5 == 12 + bodyHeaderSize * 4); // categories_pointer_table_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001342
1343 // Check the device records pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001344 const uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1345 const uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001346 CHECK(deviceRecordOffset0 == 20); // Device record offset for "device1"
1347 CHECK(deviceRecordOffset1 == 40); // Device record offset for "device2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001348
1349 // Check the counter set pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001350 const uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001351 CHECK(counterSetRecordOffset0 == 52); // Counter set record offset for "counterset1"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001352
1353 // Check the category pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001354 const uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1355 const uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001356 CHECK(categoryRecordOffset0 == 72); // Category record offset for "category1"
1357 CHECK(categoryRecordOffset1 == 176); // Category record offset for "category2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001358
1359 // Get the device record pool offset
Finn Williams985fecf2020-04-30 11:06:43 +01001360 const uint32_t uint32_t_size = sizeof(uint32_t);
Finn Williamsd44815f2020-05-01 13:25:55 +01001361 const uint32_t packetHeaderSize = 2u * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001362
1363 // Device record structure/collection used for testing
1364 struct DeviceRecord
1365 {
1366 uint16_t uid;
1367 uint16_t cores;
1368 uint32_t name_offset;
1369 uint32_t name_length;
1370 std::string name;
1371 };
1372 std::vector<DeviceRecord> deviceRecords;
Finn Williamsd44815f2020-05-01 13:25:55 +01001373 const uint32_t deviceRecordsPointerTableOffset = packetHeaderSize +
Finn Williams985fecf2020-04-30 11:06:43 +01001374 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001375
1376 const unsigned char* readData = readBuffer->GetReadableData();
1377
Finn Williamsd44815f2020-05-01 13:25:55 +01001378 uint32_t offset = 0;
1379 std::vector<uint32_t> data(800);
1380
1381 for (uint32_t i = 0; i < 800; i+=uint32_t_size)
1382 {
1383 data[i] = ReadUint32(readBuffer, offset);
1384 offset += uint32_t_size;
1385 }
1386
1387 std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount);
1388 offset = deviceRecordsPointerTableOffset;
1389 for (uint32_t i = 0; i < deviceRecordCount; ++i)
1390 {
1391 // deviceRecordOffset is relative to the start of the deviceRecordsPointerTable
1392 deviceRecordOffsets[i] = ReadUint32(readBuffer, offset) + deviceRecordsPointerTableOffset;
1393 offset += uint32_t_size;
1394 }
1395
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001396 for (uint32_t i = 0; i < deviceRecordCount; i++)
1397 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001398 // Collect the data for the device record
Finn Williamsd44815f2020-05-01 13:25:55 +01001399 const uint32_t deviceRecordWord0 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 0 * uint32_t_size);
1400 const uint32_t deviceRecordWord1 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001401 DeviceRecord deviceRecord;
1402 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1403 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1404 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1405
Finn Williamsd44815f2020-05-01 13:25:55 +01001406 uint32_t deviceRecordPoolOffset = deviceRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001407 deviceRecord.name_offset; // Device name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001408 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1409 deviceRecord.name_length = deviceRecordNameLength; // name_length
1410 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1411 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001412 CHECK(deviceRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001413 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1414 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001415 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001416 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1417
1418 deviceRecords.push_back(deviceRecord);
1419 }
1420
1421 // Check that the device records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001422 CHECK(deviceRecords.size() == 2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001423 for (const DeviceRecord& deviceRecord : deviceRecords)
1424 {
1425 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001426 CHECK(device);
1427 CHECK(device->m_Uid == deviceRecord.uid);
1428 CHECK(device->m_Cores == deviceRecord.cores);
1429 CHECK(device->m_Name == deviceRecord.name);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001430 }
1431
Finn Williamsd44815f2020-05-01 13:25:55 +01001432
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001433 // Counter set record structure/collection used for testing
1434 struct CounterSetRecord
1435 {
1436 uint16_t uid;
1437 uint16_t count;
1438 uint32_t name_offset;
1439 uint32_t name_length;
1440 std::string name;
1441 };
1442 std::vector<CounterSetRecord> counterSetRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001443 const uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1444 bodyHeaderWord3; // counter_set_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001445
1446 offset = counterSetRecordsPointerTableOffset;
1447 std::vector<uint32_t> counterSetRecordOffsets(counterSetRecordCount);
1448
1449 for (uint32_t i = 0; i < counterSetRecordCount; ++i)
1450 {
1451 // counterSetRecordOffset is relative to the start of the dcounterSetRecordsPointerTable
1452 counterSetRecordOffsets[i] = ReadUint32(readBuffer, offset) + counterSetRecordsPointerTableOffset;
1453 offset += uint32_t_size;
1454 }
1455
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001456 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1457 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001458 // Collect the data for the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +01001459 const uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 0 * uint32_t_size);
1460 const uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001461 CounterSetRecord counterSetRecord;
1462 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1463 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1464 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1465
Finn Williamsd44815f2020-05-01 13:25:55 +01001466 uint32_t counterSetRecordPoolOffset = counterSetRecordOffsets[i] + // Packet body offset
Finn Williams6be1e9b2020-05-15 11:21:54 +01001467 counterSetRecord.name_offset; // Counter set name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001468 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1469 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1470 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1471 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001472 CHECK(counterSetRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001473 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1474 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001475 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001476 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1477
1478 counterSetRecords.push_back(counterSetRecord);
1479 }
1480
1481 // Check that the counter set records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001482 CHECK(counterSetRecords.size() == 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001483 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1484 {
1485 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001486 CHECK(counterSet);
1487 CHECK(counterSet->m_Uid == counterSetRecord.uid);
1488 CHECK(counterSet->m_Count == counterSetRecord.count);
1489 CHECK(counterSet->m_Name == counterSetRecord.name);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001490 }
1491
1492 // Event record structure/collection used for testing
1493 struct EventRecord
1494 {
1495 uint16_t counter_uid;
1496 uint16_t max_counter_uid;
1497 uint16_t device;
1498 uint16_t counter_set;
1499 uint16_t counter_class;
1500 uint16_t interpolation;
1501 double multiplier;
1502 uint32_t name_offset;
1503 uint32_t name_length;
1504 std::string name;
1505 uint32_t description_offset;
1506 uint32_t description_length;
1507 std::string description;
1508 uint32_t units_offset;
1509 uint32_t units_length;
1510 std::string units;
1511 };
1512 // Category record structure/collection used for testing
1513 struct CategoryRecord
1514 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001515 uint16_t event_count;
1516 uint32_t event_pointer_table_offset;
1517 uint32_t name_offset;
1518 uint32_t name_length;
1519 std::string name;
1520 std::vector<uint32_t> event_pointer_table;
1521 std::vector<EventRecord> event_records;
1522 };
1523 std::vector<CategoryRecord> categoryRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001524 const uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1525 bodyHeaderWord5; // categories_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001526
1527 offset = categoryRecordsPointerTableOffset;
1528 std::vector<uint32_t> categoryRecordOffsets(categoryRecordCount);
1529 for (uint32_t i = 0; i < categoryRecordCount; ++i)
1530 {
1531 // categoryRecordOffset is relative to the start of the categoryRecordsPointerTable
1532 categoryRecordOffsets[i] = ReadUint32(readBuffer, offset) + categoryRecordsPointerTableOffset;
1533 offset += uint32_t_size;
1534 }
1535
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001536 for (uint32_t i = 0; i < categoryRecordCount; i++)
1537 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001538 // Collect the data for the category record
Finn Williamsd44815f2020-05-01 13:25:55 +01001539 const uint32_t categoryRecordWord1 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 0 * uint32_t_size);
1540 const uint32_t categoryRecordWord2 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 1 * uint32_t_size);
1541 const uint32_t categoryRecordWord3 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 2 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001542 CategoryRecord categoryRecord;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001543 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1544 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1545 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1546
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001547 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001548 categoryRecordOffsets[i] + categoryRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001549 categoryRecord.name_length = categoryRecordNameLength; // name_length
1550 unsigned char categoryRecordNameNullTerminator =
1551 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001552 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001553 categoryRecord.name_offset +
1554 uint32_t_size +
1555 categoryRecordNameLength - 1); // name null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001556 CHECK(categoryRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001557 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1558 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001559 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001560 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001561 categoryRecord.name_offset +
1562 uint32_t_size,
1563 categoryRecordNameBuffer.size());
1564 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1565
1566 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
Finn Williamsd44815f2020-05-01 13:25:55 +01001567 offset = categoryRecordOffsets[i] + categoryRecord.event_pointer_table_offset;
1568 for (uint32_t eventOffsetIndex = 0; eventOffsetIndex < categoryRecord.event_count; ++eventOffsetIndex)
1569 {
1570 // eventRecordOffset is relative to the start of the event pointer table
1571 categoryRecord.event_pointer_table[eventOffsetIndex] = ReadUint32(readBuffer, offset) +
1572 categoryRecordOffsets[i] +
1573 categoryRecord.event_pointer_table_offset;
1574 offset += uint32_t_size;
1575 }
1576
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001577 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1578 {
Finn Williamsd44815f2020-05-01 13:25:55 +01001579 const uint32_t eventOffset = categoryRecord.event_pointer_table[eventIndex];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001580 // Collect the data for the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001581 const uint32_t eventRecordWord0 = ReadUint32(readBuffer, eventOffset + 0 * uint32_t_size);
1582 const uint32_t eventRecordWord1 = ReadUint32(readBuffer, eventOffset + 1 * uint32_t_size);
1583 const uint32_t eventRecordWord2 = ReadUint32(readBuffer, eventOffset + 2 * uint32_t_size);
1584 const uint64_t eventRecordWord34 = ReadUint64(readBuffer, eventOffset + 3 * uint32_t_size);
1585 const uint32_t eventRecordWord5 = ReadUint32(readBuffer, eventOffset + 5 * uint32_t_size);
1586 const uint32_t eventRecordWord6 = ReadUint32(readBuffer, eventOffset + 6 * uint32_t_size);
1587 const uint32_t eventRecordWord7 = ReadUint32(readBuffer, eventOffset + 7 * uint32_t_size);
1588
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001589 EventRecord eventRecord;
1590 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1591 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1592 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1593 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1594 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1595 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1596 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1597 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1598 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1599 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1600
Finn Williamsd44815f2020-05-01 13:25:55 +01001601 uint32_t eventRecordNameLength = ReadUint32(readBuffer, eventOffset + eventRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001602 eventRecord.name_length = eventRecordNameLength; // name_length
1603 unsigned char eventRecordNameNullTerminator =
1604 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001605 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001606 eventRecord.name_offset +
1607 uint32_t_size +
1608 eventRecordNameLength - 1); // name null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001609 CHECK(eventRecordNameNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001610 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1611 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001612 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001613 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001614 eventRecord.name_offset +
1615 uint32_t_size,
1616 eventRecordNameBuffer.size());
1617 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1618
1619 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001620 eventOffset + eventRecord.description_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001621 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1622 unsigned char eventRecordDescriptionNullTerminator =
1623 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001624 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001625 eventRecord.description_offset +
1626 uint32_t_size +
1627 eventRecordDescriptionLength - 1); // description null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001628 CHECK(eventRecordDescriptionNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001629 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1630 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001631 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001632 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001633 eventRecord.description_offset +
1634 uint32_t_size,
1635 eventRecordDescriptionBuffer.size());
1636 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1637 eventRecordDescriptionBuffer.end()); // description
1638
1639 if (eventRecord.units_offset > 0)
1640 {
1641 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001642 eventOffset + eventRecord.units_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001643 eventRecord.units_length = eventRecordUnitsLength; // units_length
1644 unsigned char eventRecordUnitsNullTerminator =
1645 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001646 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001647 eventRecord.units_offset +
1648 uint32_t_size +
1649 eventRecordUnitsLength - 1); // units null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01001650 CHECK(eventRecordUnitsNullTerminator == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001651 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1652 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001653 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001654 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001655 eventRecord.units_offset +
1656 uint32_t_size,
1657 eventRecordUnitsBuffer.size());
1658 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1659 }
1660
1661 categoryRecord.event_records.push_back(eventRecord);
1662 }
1663
1664 categoryRecords.push_back(categoryRecord);
1665 }
1666
1667 // Check that the category records are correct
Sadik Armagan1625efc2021-06-10 18:24:34 +01001668 CHECK(categoryRecords.size() == 2);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001669 for (const CategoryRecord& categoryRecord : categoryRecords)
1670 {
1671 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001672 CHECK(category);
1673 CHECK(category->m_Name == categoryRecord.name);
1674 CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
1675 CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001676
1677 // Check that the event records are correct
1678 for (const EventRecord& eventRecord : categoryRecord.event_records)
1679 {
1680 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001681 CHECK(counter);
1682 CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1683 CHECK(counter->m_DeviceUid == eventRecord.device);
1684 CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1685 CHECK(counter->m_Class == eventRecord.counter_class);
1686 CHECK(counter->m_Interpolation == eventRecord.interpolation);
1687 CHECK(counter->m_Multiplier == eventRecord.multiplier);
1688 CHECK(counter->m_Name == eventRecord.name);
1689 CHECK(counter->m_Description == eventRecord.description);
1690 CHECK(counter->m_Units == eventRecord.units);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001691 }
1692 }
1693}
1694
Sadik Armagan1625efc2021-06-10 18:24:34 +01001695TEST_CASE("SendCounterDirectoryPacketTest3")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001696{
1697 // Using a mock counter directory that allows to register invalid objects
1698 MockCounterDirectory counterDirectory;
1699
1700 // Register an invalid device
1701 const std::string deviceName = "inv@lid dev!c€";
1702 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001703 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1704 CHECK(counterDirectory.GetDeviceCount() == 1);
1705 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001706
1707 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001708 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001709 SendCounterPacket sendCounterPacket(mockBuffer,
1710 arm::pipe::ARMNN_SOFTWARE_INFO,
1711 arm::pipe::ARMNN_SOFTWARE_VERSION,
1712 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001713 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), arm::pipe::ProfilingException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001714}
1715
Sadik Armagan1625efc2021-06-10 18:24:34 +01001716TEST_CASE("SendCounterDirectoryPacketTest4")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001717{
1718 // Using a mock counter directory that allows to register invalid objects
1719 MockCounterDirectory counterDirectory;
1720
1721 // Register an invalid counter set
1722 const std::string counterSetName = "inv@lid count€rs€t";
1723 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001724 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1725 CHECK(counterDirectory.GetCounterSetCount() == 1);
1726 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001727
1728 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001729 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001730 SendCounterPacket sendCounterPacket(mockBuffer,
1731 arm::pipe::ARMNN_SOFTWARE_INFO,
1732 arm::pipe::ARMNN_SOFTWARE_VERSION,
1733 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001734 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), arm::pipe::ProfilingException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001735}
1736
Sadik Armagan1625efc2021-06-10 18:24:34 +01001737TEST_CASE("SendCounterDirectoryPacketTest5")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001738{
1739 // Using a mock counter directory that allows to register invalid objects
1740 MockCounterDirectory counterDirectory;
1741
1742 // Register an invalid category
1743 const std::string categoryName = "c@t€gory";
1744 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001745 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1746 CHECK(counterDirectory.GetCategoryCount() == 1);
1747 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001748
1749 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001750 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001751 SendCounterPacket sendCounterPacket(mockBuffer,
1752 arm::pipe::ARMNN_SOFTWARE_INFO,
1753 arm::pipe::ARMNN_SOFTWARE_VERSION,
1754 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001755 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), arm::pipe::ProfilingException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001756}
1757
Sadik Armagan1625efc2021-06-10 18:24:34 +01001758TEST_CASE("SendCounterDirectoryPacketTest6")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001759{
1760 // Using a mock counter directory that allows to register invalid objects
1761 MockCounterDirectory counterDirectory;
1762
1763 // Register an invalid device
1764 const std::string deviceName = "inv@lid dev!c€";
1765 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001766 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1767 CHECK(counterDirectory.GetDeviceCount() == 1);
1768 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001769
1770 // Register an invalid counter set
1771 const std::string counterSetName = "inv@lid count€rs€t";
1772 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001773 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1774 CHECK(counterDirectory.GetCounterSetCount() == 1);
1775 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001776
1777 // Register an invalid category associated to an invalid device and an invalid counter set
1778 const std::string categoryName = "c@t€gory";
1779 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001780 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1781 CHECK(counterDirectory.GetCategoryCount() == 1);
1782 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001783
1784 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001785 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001786 SendCounterPacket sendCounterPacket(mockBuffer,
1787 arm::pipe::ARMNN_SOFTWARE_INFO,
1788 arm::pipe::ARMNN_SOFTWARE_VERSION,
1789 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001790 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), arm::pipe::ProfilingException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001791}
1792
Sadik Armagan1625efc2021-06-10 18:24:34 +01001793TEST_CASE("SendCounterDirectoryPacketTest7")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001794{
1795 // Using a mock counter directory that allows to register invalid objects
1796 MockCounterDirectory counterDirectory;
1797
1798 // Register an valid device
1799 const std::string deviceName = "valid device";
1800 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001801 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1802 CHECK(counterDirectory.GetDeviceCount() == 1);
1803 CHECK(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001804
1805 // Register an valid counter set
1806 const std::string counterSetName = "valid counterset";
1807 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001808 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1809 CHECK(counterDirectory.GetCounterSetCount() == 1);
1810 CHECK(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001811
1812 // Register an valid category associated to a valid device and a valid counter set
1813 const std::string categoryName = "category";
1814 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001815 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1816 CHECK(counterDirectory.GetCategoryCount() == 1);
1817 CHECK(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001818
1819 // Register an invalid counter associated to a valid category
1820 const Counter* counter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001821 CHECK_NOTHROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001822 0,
1823 categoryName,
1824 0,
1825 1,
1826 123.45f,
1827 "counter",
1828 "counter description",
1829 std::string("invalid counter units"),
1830 5,
1831 device->m_Uid,
1832 counterSet->m_Uid));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001833 CHECK(counterDirectory.GetCounterCount() == 5);
1834 CHECK(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001835
1836 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001837 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001838 SendCounterPacket sendCounterPacket(mockBuffer,
1839 arm::pipe::ARMNN_SOFTWARE_INFO,
1840 arm::pipe::ARMNN_SOFTWARE_VERSION,
1841 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001842 CHECK_THROWS_AS(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), arm::pipe::ProfilingException);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001843}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001844
Sadik Armagan1625efc2021-06-10 18:24:34 +01001845TEST_CASE("SendThreadTest0")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001846{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001847 ProfilingStateMachine profilingStateMachine;
1848 SetActiveProfilingState(profilingStateMachine);
1849
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001850 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001851 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Jim Flynn9c85b412022-03-16 00:27:43 +00001852 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer,
1853 arm::pipe::ARMNN_SOFTWARE_INFO,
1854 arm::pipe::ARMNN_SOFTWARE_VERSION,
1855 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00001856 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001857
1858 // Try to start the send thread many times, it must only start once
1859
Sadik Armagan3896b472020-02-10 12:24:15 +00001860 sendThread.Start(mockProfilingConnection);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001861 CHECK(sendThread.IsRunning());
Sadik Armagan3896b472020-02-10 12:24:15 +00001862 sendThread.Start(mockProfilingConnection);
1863 sendThread.Start(mockProfilingConnection);
1864 sendThread.Start(mockProfilingConnection);
1865 sendThread.Start(mockProfilingConnection);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001866 CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001867
Sadik Armagan3896b472020-02-10 12:24:15 +00001868 sendThread.Stop();
Sadik Armagan1625efc2021-06-10 18:24:34 +01001869 CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001870}
1871
Sadik Armagan1625efc2021-06-10 18:24:34 +01001872TEST_CASE("SendThreadTest1")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001873{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001874 ProfilingStateMachine profilingStateMachine;
1875 SetActiveProfilingState(profilingStateMachine);
1876
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001877 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001878
1879 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001880 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001881 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer,
1882 arm::pipe::ARMNN_SOFTWARE_INFO,
1883 arm::pipe::ARMNN_SOFTWARE_VERSION,
1884 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00001885 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1886 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001887
1888 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1889 // something to become available for reading
1890
Colm Donelan2ba48d22019-11-29 09:10:59 +00001891 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001892
1893 CounterDirectory counterDirectory;
1894 sendCounterPacket.SendStreamMetaDataPacket();
1895
Finn Williamsa0de0562020-04-22 12:27:37 +01001896 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001897
Sadik Armagan3896b472020-02-10 12:24:15 +00001898 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001899
Colm Donelan2ba48d22019-11-29 09:10:59 +00001900 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001901
1902 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1903
1904 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001905 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001906 totalWrittenSize += counterDirectoryPacketSize;
1907
Sadik Armagan3896b472020-02-10 12:24:15 +00001908 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001909
Colm Donelan2ba48d22019-11-29 09:10:59 +00001910 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001911
1912 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1913 {
1914 { 1u, 23u },
1915 { 33u, 1207623u }
1916 });
1917
1918 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001919 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001920 totalWrittenSize += periodicCounterCapturePacketSize;
1921
Sadik Armagan3896b472020-02-10 12:24:15 +00001922 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001923
Colm Donelan2ba48d22019-11-29 09:10:59 +00001924 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001925
1926 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1927 {
1928 { 211u, 923u }
1929 });
1930
1931 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001932 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001933 totalWrittenSize += periodicCounterCapturePacketSize;
1934
1935 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1936 {
1937 { 555u, 23u },
1938 { 556u, 6u },
1939 { 557u, 893454u },
1940 { 558u, 1456623u },
1941 { 559u, 571090u }
1942 });
1943
1944 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001945 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001946 totalWrittenSize += periodicCounterCapturePacketSize;
1947
1948 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1949 {
1950 { 88u, 11u },
1951 { 96u, 22u },
1952 { 97u, 33u },
1953 { 999u, 444u }
1954 });
1955
1956 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001957 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001958 totalWrittenSize += periodicCounterCapturePacketSize;
1959
Sadik Armagan3896b472020-02-10 12:24:15 +00001960 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001961
Colm Donelan2ba48d22019-11-29 09:10:59 +00001962 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001963
1964 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1965
1966 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001967 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001968 totalWrittenSize += periodicCounterCapturePacketSize;
1969
Sadik Armagan3896b472020-02-10 12:24:15 +00001970 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001971
Finn Williams109c05b2019-11-29 13:56:33 +00001972 // 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 +01001973 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001974 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001975
Sadik Armagan3896b472020-02-10 12:24:15 +00001976 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001977
Sadik Armagan1625efc2021-06-10 18:24:34 +01001978 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
1979 CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1980 CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001981}
1982
Sadik Armagan1625efc2021-06-10 18:24:34 +01001983TEST_CASE("SendThreadTest2")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001984{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001985 ProfilingStateMachine profilingStateMachine;
1986 SetActiveProfilingState(profilingStateMachine);
1987
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001988 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001989
1990 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001991 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00001992 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer,
1993 arm::pipe::ARMNN_SOFTWARE_INFO,
1994 arm::pipe::ARMNN_SOFTWARE_VERSION,
1995 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00001996 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1997 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001998
1999 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
2000 // capable of handling unnecessary read requests
2001
Colm Donelan2ba48d22019-11-29 09:10:59 +00002002 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002003
Sadik Armagan3896b472020-02-10 12:24:15 +00002004 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002005
2006 CounterDirectory counterDirectory;
2007 sendCounterPacket.SendStreamMetaDataPacket();
2008
Finn Williamsa0de0562020-04-22 12:27:37 +01002009 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002010
Sadik Armagan3896b472020-02-10 12:24:15 +00002011 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002012
Colm Donelan2ba48d22019-11-29 09:10:59 +00002013 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002014
2015 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2016
2017 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002018 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002019 totalWrittenSize += counterDirectoryPacketSize;
2020
Sadik Armagan3896b472020-02-10 12:24:15 +00002021 sendThread.SetReadyToRead();
2022 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002023
Colm Donelan2ba48d22019-11-29 09:10:59 +00002024 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002025
2026 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2027 {
2028 { 1u, 23u },
2029 { 33u, 1207623u }
2030 });
2031
2032 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002033 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002034 totalWrittenSize += periodicCounterCapturePacketSize;
2035
Sadik Armagan3896b472020-02-10 12:24:15 +00002036 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002037
Colm Donelan2ba48d22019-11-29 09:10:59 +00002038 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002039
Sadik Armagan3896b472020-02-10 12:24:15 +00002040 sendThread.SetReadyToRead();
2041 sendThread.SetReadyToRead();
2042 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002043
Colm Donelan2ba48d22019-11-29 09:10:59 +00002044 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002045
Sadik Armagan3896b472020-02-10 12:24:15 +00002046 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002047 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2048 {
2049 { 211u, 923u }
2050 });
2051
2052 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002053 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002054 totalWrittenSize += periodicCounterCapturePacketSize;
2055
2056 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2057 {
2058 { 555u, 23u },
2059 { 556u, 6u },
2060 { 557u, 893454u },
2061 { 558u, 1456623u },
2062 { 559u, 571090u }
2063 });
2064
2065 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002066 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002067 totalWrittenSize += periodicCounterCapturePacketSize;
2068
Sadik Armagan3896b472020-02-10 12:24:15 +00002069 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002070 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2071 {
2072 { 88u, 11u },
2073 { 96u, 22u },
2074 { 97u, 33u },
2075 { 999u, 444u }
2076 });
2077
2078 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002079 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002080 totalWrittenSize += periodicCounterCapturePacketSize;
2081
Sadik Armagan3896b472020-02-10 12:24:15 +00002082 sendThread.SetReadyToRead();
2083 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002084
Colm Donelan2ba48d22019-11-29 09:10:59 +00002085 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002086
2087 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2088
2089 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002090 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002091 totalWrittenSize += periodicCounterCapturePacketSize;
2092
Sadik Armagan3896b472020-02-10 12:24:15 +00002093 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002094
Finn Williams109c05b2019-11-29 13:56:33 +00002095 // 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 +01002096 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002097 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002098
Sadik Armagan1625efc2021-06-10 18:24:34 +01002099 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2100 CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2101 CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002102}
2103
Sadik Armagan1625efc2021-06-10 18:24:34 +01002104TEST_CASE("SendThreadTest3")
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002105{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002106 ProfilingStateMachine profilingStateMachine;
2107 SetActiveProfilingState(profilingStateMachine);
2108
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002109 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002110
2111 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002112 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002113 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer,
2114 arm::pipe::ARMNN_SOFTWARE_INFO,
2115 arm::pipe::ARMNN_SOFTWARE_VERSION,
2116 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002117 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2118 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002119
2120 // Not using pauses or "grace periods" to stress test the send thread
2121
Sadik Armagan3896b472020-02-10 12:24:15 +00002122 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002123
2124 CounterDirectory counterDirectory;
2125 sendCounterPacket.SendStreamMetaDataPacket();
2126
Finn Williamsa0de0562020-04-22 12:27:37 +01002127 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002128
Sadik Armagan3896b472020-02-10 12:24:15 +00002129 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002130 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2131
2132 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002133 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002134 totalWrittenSize += counterDirectoryPacketSize;
2135
Sadik Armagan3896b472020-02-10 12:24:15 +00002136 sendThread.SetReadyToRead();
2137 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002138 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2139 {
2140 { 1u, 23u },
2141 { 33u, 1207623u }
2142 });
2143
2144 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002145 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002146 totalWrittenSize += periodicCounterCapturePacketSize;
2147
Sadik Armagan3896b472020-02-10 12:24:15 +00002148 sendThread.SetReadyToRead();
2149 sendThread.SetReadyToRead();
2150 sendThread.SetReadyToRead();
2151 sendThread.SetReadyToRead();
2152 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002153 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2154 {
2155 { 211u, 923u }
2156 });
2157
2158 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002159 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002160 totalWrittenSize += periodicCounterCapturePacketSize;
2161
2162 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2163 {
2164 { 555u, 23u },
2165 { 556u, 6u },
2166 { 557u, 893454u },
2167 { 558u, 1456623u },
2168 { 559u, 571090u }
2169 });
2170
2171 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002172 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002173 totalWrittenSize += periodicCounterCapturePacketSize;
2174
Sadik Armagan3896b472020-02-10 12:24:15 +00002175 sendThread.SetReadyToRead();
2176 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002177 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2178 {
2179 { 88u, 11u },
2180 { 96u, 22u },
2181 { 97u, 33u },
2182 { 999u, 444u }
2183 });
2184
2185 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002186 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002187 totalWrittenSize += periodicCounterCapturePacketSize;
2188
Sadik Armagan3896b472020-02-10 12:24:15 +00002189 sendThread.SetReadyToRead();
2190 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002191 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2192
2193 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002194 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002195 totalWrittenSize += periodicCounterCapturePacketSize;
2196
Sadik Armagan3896b472020-02-10 12:24:15 +00002197 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002198
2199 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2200 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002201 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002202
Sadik Armagan1625efc2021-06-10 18:24:34 +01002203 CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2204 CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2205 CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2206 CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2207 CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002208}
2209
Sadik Armagan1625efc2021-06-10 18:24:34 +01002210TEST_CASE("SendCounterPacketTestWithSendThread")
Sadik Armagan3896b472020-02-10 12:24:15 +00002211{
2212 ProfilingStateMachine profilingStateMachine;
2213 SetWaitingForAckProfilingState(profilingStateMachine);
2214
2215 MockProfilingConnection mockProfilingConnection;
2216 BufferManager bufferManager(1, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002217 SendCounterPacket sendCounterPacket(bufferManager,
2218 arm::pipe::ARMNN_SOFTWARE_INFO,
2219 arm::pipe::ARMNN_SOFTWARE_VERSION,
2220 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002221 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2222 sendThread.Start(mockProfilingConnection);
2223
Finn Williamsa0de0562020-04-22 12:27:37 +01002224 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan3896b472020-02-10 12:24:15 +00002225
2226 sendThread.Stop();
2227
2228 // check for packet in ProfilingConnection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002229 CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
Sadik Armagan3896b472020-02-10 12:24:15 +00002230
2231 SetActiveProfilingState(profilingStateMachine);
2232 sendThread.Start(mockProfilingConnection);
2233
2234 // SendCounterDirectoryPacket
2235 CounterDirectory counterDirectory;
2236 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2237
2238 sendThread.Stop();
2239 unsigned int counterDirectoryPacketSize = 32;
2240 // check for packet in ProfilingConnection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002241 CHECK(mockProfilingConnection.CheckForPacket(
Sadik Armagan3896b472020-02-10 12:24:15 +00002242 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2243
2244 sendThread.Start(mockProfilingConnection);
2245
2246 // SendPeriodicCounterCapturePacket
2247 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2248 {
2249 { 1u, 23u },
2250 { 33u, 1207623u }
2251 });
2252
2253 sendThread.Stop();
2254
2255 unsigned int periodicCounterCapturePacketSize = 28;
Sadik Armagan1625efc2021-06-10 18:24:34 +01002256 CHECK(mockProfilingConnection.CheckForPacket(
Sadik Armagan3896b472020-02-10 12:24:15 +00002257 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2258}
2259
Sadik Armagan1625efc2021-06-10 18:24:34 +01002260TEST_CASE("SendThreadBufferTest")
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002261{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002262 ProfilingStateMachine profilingStateMachine;
2263 SetActiveProfilingState(profilingStateMachine);
2264
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002265 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002266 BufferManager bufferManager(3, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002267 SendCounterPacket sendCounterPacket(bufferManager,
2268 arm::pipe::ARMNN_SOFTWARE_INFO,
2269 arm::pipe::ARMNN_SOFTWARE_VERSION,
2270 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002271 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2272 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002273
2274 // SendStreamMetaDataPacket
2275 sendCounterPacket.SendStreamMetaDataPacket();
2276
2277 // Read data from the buffer
2278 // Buffer should become readable after commit by SendStreamMetaDataPacket
2279 auto packetBuffer = bufferManager.GetReadableBuffer();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002280 CHECK(packetBuffer.get());
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002281
Finn Williamsa0de0562020-04-22 12:27:37 +01002282 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002283 CHECK(packetBuffer->GetSize() == streamMetadataPacketsize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002284
2285 // Recommit to be read by sendCounterPacket
2286 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2287
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002288 // SendCounterDirectoryPacket
2289 CounterDirectory counterDirectory;
2290 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2291
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002292 // SendPeriodicCounterCapturePacket
2293 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2294 {
2295 { 1u, 23u },
2296 { 33u, 1207623u }
2297 });
2298
Sadik Armagan3896b472020-02-10 12:24:15 +00002299 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002300
2301 // The buffer is read by the send thread so it should not be in the readable buffer.
2302 auto readBuffer = bufferManager.GetReadableBuffer();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002303 CHECK(!readBuffer);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002304
2305 // Successfully reserved the buffer with requested size
2306 unsigned int reservedSize = 0;
2307 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002308 CHECK(reservedSize == 512);
2309 CHECK(reservedBuffer.get());
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002310
Finn Williams09ad6f92019-12-19 17:05:18 +00002311 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2312 const auto metaDataPacketCount =
2313 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2314
Sadik Armagan1625efc2021-06-10 18:24:34 +01002315 CHECK(metaDataPacketCount >= 1);
2316 CHECK(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2317 CHECK(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
Finn Williams09ad6f92019-12-19 17:05:18 +00002318 // Check that we only received the packets we expected
Sadik Armagan1625efc2021-06-10 18:24:34 +01002319 CHECK(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002320}
2321
Sadik Armagan1625efc2021-06-10 18:24:34 +01002322TEST_CASE("SendThreadSendStreamMetadataPacket1")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002323{
2324 ProfilingStateMachine profilingStateMachine;
2325
2326 MockProfilingConnection mockProfilingConnection;
2327 BufferManager bufferManager(3, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002328 SendCounterPacket sendCounterPacket(bufferManager,
2329 arm::pipe::ARMNN_SOFTWARE_INFO,
2330 arm::pipe::ARMNN_SOFTWARE_VERSION,
2331 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002332 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2333 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002334
2335 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002336 CHECK_THROWS_AS(sendThread.Stop(), arm::pipe::ProfilingException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002337}
2338
Sadik Armagan1625efc2021-06-10 18:24:34 +01002339TEST_CASE("SendThreadSendStreamMetadataPacket2")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002340{
2341 ProfilingStateMachine profilingStateMachine;
2342 SetNotConnectedProfilingState(profilingStateMachine);
2343
2344 MockProfilingConnection mockProfilingConnection;
2345 BufferManager bufferManager(3, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002346 SendCounterPacket sendCounterPacket(bufferManager,
2347 arm::pipe::ARMNN_SOFTWARE_INFO,
2348 arm::pipe::ARMNN_SOFTWARE_VERSION,
2349 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002350 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2351 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002352
2353 // The profiling state is set to "NotConnected", so the send thread should throw an exception
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002354 CHECK_THROWS_AS(sendThread.Stop(), arm::pipe::ProfilingException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002355}
2356
Sadik Armagan1625efc2021-06-10 18:24:34 +01002357TEST_CASE("SendThreadSendStreamMetadataPacket3")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002358{
2359 ProfilingStateMachine profilingStateMachine;
2360 SetWaitingForAckProfilingState(profilingStateMachine);
2361
Finn Williamsa0de0562020-04-22 12:27:37 +01002362 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002363
2364 MockProfilingConnection mockProfilingConnection;
2365 BufferManager bufferManager(3, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002366 SendCounterPacket sendCounterPacket(bufferManager,
2367 arm::pipe::ARMNN_SOFTWARE_INFO,
2368 arm::pipe::ARMNN_SOFTWARE_VERSION,
2369 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002370 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2371 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002372
2373 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002374 // Wait for sendThread to join
Sadik Armagan1625efc2021-06-10 18:24:34 +01002375 CHECK_NOTHROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002376
Finn Williams09ad6f92019-12-19 17:05:18 +00002377 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2378 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2379
Sadik Armagan1625efc2021-06-10 18:24:34 +01002380 CHECK(writtenDataSize >= 1u);
2381 CHECK(mockProfilingConnection.CheckForPacket(
Finn Williams09ad6f92019-12-19 17:05:18 +00002382 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002383}
2384
Sadik Armagan1625efc2021-06-10 18:24:34 +01002385TEST_CASE("SendThreadSendStreamMetadataPacket4")
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002386{
2387 ProfilingStateMachine profilingStateMachine;
2388 SetWaitingForAckProfilingState(profilingStateMachine);
2389
Finn Williamsa0de0562020-04-22 12:27:37 +01002390 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002391
2392 MockProfilingConnection mockProfilingConnection;
2393 BufferManager bufferManager(3, 1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002394 SendCounterPacket sendCounterPacket(bufferManager,
2395 arm::pipe::ARMNN_SOFTWARE_INFO,
2396 arm::pipe::ARMNN_SOFTWARE_VERSION,
2397 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002398 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2399 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002400
2401 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002402 // Wait for sendThread to join
2403 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002404
Sadik Armagan3896b472020-02-10 12:24:15 +00002405 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002406 // Check that the profiling state is still "WaitingForAck"
Sadik Armagan1625efc2021-06-10 18:24:34 +01002407 CHECK((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002408
Finn Williams3e2969d2019-12-06 17:47:36 +00002409 // Check that the buffer contains at least one Stream Metadata packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002410 CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002411
2412 mockProfilingConnection.Clear();
2413
Sadik Armagan3896b472020-02-10 12:24:15 +00002414 sendThread.Stop();
2415 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002416
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002417 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002418 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002419
Sadik Armagan3896b472020-02-10 12:24:15 +00002420 // Wait for sendThread to join
Sadik Armagan1625efc2021-06-10 18:24:34 +01002421 CHECK_NOTHROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002422
2423 // Check that the profiling state is still "WaitingForAck"
Sadik Armagan1625efc2021-06-10 18:24:34 +01002424 CHECK((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002425
Finn Williams09ad6f92019-12-19 17:05:18 +00002426 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2427 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2428
Sadik Armagan1625efc2021-06-10 18:24:34 +01002429 CHECK(writtenDataSize >= 1u);
2430 CHECK(mockProfilingConnection.CheckForPacket(
Finn Williams09ad6f92019-12-19 17:05:18 +00002431 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002432}
2433
Sadik Armagan1625efc2021-06-10 18:24:34 +01002434}