Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 1 | // |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 6 | #include "SendCounterPacketTests.hpp" |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 7 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 8 | #include <CounterDirectory.hpp> |
| 9 | #include <BufferManager.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 10 | #include <EncodeVersion.hpp> |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 11 | #include <ProfilingUtils.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 12 | #include <SendCounterPacket.hpp> |
| 13 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 14 | #include <armnn/Exceptions.hpp> |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 15 | #include <armnn/Conversion.hpp> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 16 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 17 | #include <boost/test/unit_test.hpp> |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 18 | #include <boost/numeric/conversion/cast.hpp> |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 19 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 20 | #include <chrono> |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 21 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 22 | using namespace armnn::profiling; |
| 23 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 24 | BOOST_AUTO_TEST_SUITE(SendCounterPacketTests) |
| 25 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 26 | BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest) |
| 27 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 28 | MockBufferManager mockBuffer(512); |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 29 | MockSendCounterPacket sendCounterPacket(mockBuffer); |
| 30 | |
| 31 | sendCounterPacket.SendStreamMetaDataPacket(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 32 | |
| 33 | auto packetBuffer = mockBuffer.GetReadableBuffer(); |
| 34 | const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData()); |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 35 | |
| 36 | BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0); |
| 37 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 38 | mockBuffer.MarkRead(packetBuffer); |
| 39 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 40 | CounterDirectory counterDirectory; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 41 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 42 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 43 | packetBuffer = mockBuffer.GetReadableBuffer(); |
| 44 | buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData()); |
| 45 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 46 | BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0); |
| 47 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 48 | mockBuffer.MarkRead(packetBuffer); |
| 49 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 50 | uint64_t timestamp = 0; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 51 | std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs; |
| 52 | |
| 53 | sendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs); |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 54 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 55 | packetBuffer = mockBuffer.GetReadableBuffer(); |
| 56 | buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData()); |
| 57 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 58 | BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0); |
| 59 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 60 | mockBuffer.MarkRead(packetBuffer); |
| 61 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 62 | uint32_t capturePeriod = 0; |
| 63 | std::vector<uint16_t> selectedCounterIds; |
| 64 | sendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds); |
| 65 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 66 | packetBuffer = mockBuffer.GetReadableBuffer(); |
| 67 | buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData()); |
| 68 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 69 | BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 70 | |
| 71 | mockBuffer.MarkRead(packetBuffer); |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 74 | BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest) |
| 75 | { |
| 76 | // Error no space left in buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 77 | MockBufferManager mockBuffer1(10); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 78 | SendCounterPacket sendPacket1(mockBuffer1); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 79 | |
| 80 | uint32_t capturePeriod = 1000; |
| 81 | std::vector<uint16_t> selectedCounterIds; |
| 82 | BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds), |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 83 | BufferExhaustion); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 84 | |
| 85 | // Packet without any counters |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 86 | MockBufferManager mockBuffer2(512); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 87 | SendCounterPacket sendPacket2(mockBuffer2); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 88 | |
| 89 | sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 90 | auto readBuffer2 = mockBuffer2.GetReadableBuffer(); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 91 | |
| 92 | uint32_t headerWord0 = ReadUint32(readBuffer2, 0); |
| 93 | uint32_t headerWord1 = ReadUint32(readBuffer2, 4); |
| 94 | uint32_t period = ReadUint32(readBuffer2, 8); |
| 95 | |
| 96 | BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family |
| 97 | BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id |
| 98 | BOOST_TEST(headerWord1 == 4); // data lenght |
| 99 | BOOST_TEST(period == 1000); // capture period |
| 100 | |
| 101 | // Full packet message |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 102 | MockBufferManager mockBuffer3(512); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 103 | SendCounterPacket sendPacket3(mockBuffer3); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 104 | |
| 105 | selectedCounterIds.reserve(5); |
| 106 | selectedCounterIds.emplace_back(100); |
| 107 | selectedCounterIds.emplace_back(200); |
| 108 | selectedCounterIds.emplace_back(300); |
| 109 | selectedCounterIds.emplace_back(400); |
| 110 | selectedCounterIds.emplace_back(500); |
| 111 | sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 112 | auto readBuffer3 = mockBuffer3.GetReadableBuffer(); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 113 | |
| 114 | headerWord0 = ReadUint32(readBuffer3, 0); |
| 115 | headerWord1 = ReadUint32(readBuffer3, 4); |
| 116 | period = ReadUint32(readBuffer3, 8); |
| 117 | |
| 118 | BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family |
| 119 | BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id |
| 120 | BOOST_TEST(headerWord1 == 14); // data lenght |
| 121 | BOOST_TEST(period == 1000); // capture period |
| 122 | |
| 123 | uint16_t counterId = 0; |
| 124 | uint32_t offset = 12; |
| 125 | |
| 126 | // Counter Ids |
| 127 | for(const uint16_t& id : selectedCounterIds) |
| 128 | { |
| 129 | counterId = ReadUint16(readBuffer3, offset); |
| 130 | BOOST_TEST(counterId == id); |
| 131 | offset += 2; |
| 132 | } |
| 133 | } |
| 134 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 135 | BOOST_AUTO_TEST_CASE(SendPeriodicCounterCapturePacketTest) |
| 136 | { |
| 137 | // Error no space left in buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 138 | MockBufferManager mockBuffer1(10); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 139 | SendCounterPacket sendPacket1(mockBuffer1); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 140 | |
| 141 | auto captureTimestamp = std::chrono::steady_clock::now(); |
| 142 | uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count()); |
| 143 | std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs; |
| 144 | |
| 145 | BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs), |
| 146 | BufferExhaustion); |
| 147 | |
| 148 | // Packet without any counters |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 149 | MockBufferManager mockBuffer2(512); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 150 | SendCounterPacket sendPacket2(mockBuffer2); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 151 | |
| 152 | sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 153 | auto readBuffer2 = mockBuffer2.GetReadableBuffer(); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 154 | |
| 155 | uint32_t headerWord0 = ReadUint32(readBuffer2, 0); |
| 156 | uint32_t headerWord1 = ReadUint32(readBuffer2, 4); |
| 157 | uint64_t readTimestamp = ReadUint64(readBuffer2, 8); |
| 158 | |
| 159 | BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 1); // packet family |
| 160 | BOOST_TEST(((headerWord0 >> 19) & 0x3F) == 0); // packet class |
| 161 | BOOST_TEST(((headerWord0 >> 16) & 0x3) == 0); // packet type |
| 162 | BOOST_TEST(headerWord1 == 8); // data length |
| 163 | BOOST_TEST(time == readTimestamp); // capture period |
| 164 | |
| 165 | // Full packet message |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 166 | MockBufferManager mockBuffer3(512); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 167 | SendCounterPacket sendPacket3(mockBuffer3); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 168 | |
| 169 | indexValuePairs.reserve(5); |
| 170 | indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(0, 100)); |
| 171 | indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(1, 200)); |
| 172 | indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(2, 300)); |
| 173 | indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(3, 400)); |
| 174 | indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(4, 500)); |
| 175 | sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 176 | auto readBuffer3 = mockBuffer3.GetReadableBuffer(); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 177 | |
| 178 | headerWord0 = ReadUint32(readBuffer3, 0); |
| 179 | headerWord1 = ReadUint32(readBuffer3, 4); |
| 180 | uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8); |
| 181 | |
| 182 | BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 1); // packet family |
| 183 | BOOST_TEST(((headerWord0 >> 19) & 0x3F) == 0); // packet class |
| 184 | BOOST_TEST(((headerWord0 >> 16) & 0x3) == 0); // packet type |
| 185 | BOOST_TEST(headerWord1 == 38); // data length |
| 186 | BOOST_TEST(time == readTimestamp2); // capture period |
| 187 | |
| 188 | uint16_t counterIndex = 0; |
| 189 | uint32_t counterValue = 100; |
| 190 | uint32_t offset = 16; |
| 191 | |
| 192 | // Counter Ids |
| 193 | for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it) |
| 194 | { |
| 195 | // Check Counter Index |
| 196 | uint16_t readIndex = ReadUint16(readBuffer3, offset); |
| 197 | BOOST_TEST(counterIndex == readIndex); |
| 198 | counterIndex++; |
| 199 | offset += 2; |
| 200 | |
| 201 | // Check Counter Value |
| 202 | uint32_t readValue = ReadUint32(readBuffer3, offset); |
| 203 | BOOST_TEST(counterValue == readValue); |
| 204 | counterValue += 100; |
| 205 | offset += 4; |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 210 | BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest) |
| 211 | { |
| 212 | using boost::numeric_cast; |
| 213 | |
| 214 | uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t)); |
| 215 | |
| 216 | // Error no space left in buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 217 | MockBufferManager mockBuffer1(10); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 218 | SendCounterPacket sendPacket1(mockBuffer1); |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 219 | BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 220 | |
| 221 | // Full metadata packet |
| 222 | |
| 223 | std::string processName = GetProcessName().substr(0, 60); |
| 224 | |
| 225 | uint32_t infoSize = numeric_cast<uint32_t>(GetSoftwareInfo().size()) > 0 ? |
| 226 | numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1 : 0; |
| 227 | uint32_t hardwareVersionSize = numeric_cast<uint32_t>(GetHardwareVersion().size()) > 0 ? |
| 228 | numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1 : 0; |
| 229 | uint32_t softwareVersionSize = numeric_cast<uint32_t>(GetSoftwareVersion().size()) > 0 ? |
| 230 | numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1 : 0; |
| 231 | uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ? |
| 232 | numeric_cast<uint32_t>(processName.size()) + 1 : 0; |
| 233 | |
Ferran Balaguer | 5bf1d32 | 2019-09-13 13:31:40 +0100 | [diff] [blame] | 234 | uint32_t packetEntries = 6; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 235 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 236 | MockBufferManager mockBuffer2(512); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 237 | SendCounterPacket sendPacket2(mockBuffer2); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 238 | sendPacket2.SendStreamMetaDataPacket(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 239 | auto readBuffer2 = mockBuffer2.GetReadableBuffer(); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 240 | |
| 241 | uint32_t headerWord0 = ReadUint32(readBuffer2, 0); |
| 242 | uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32); |
| 243 | |
| 244 | BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family |
| 245 | BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 0); // packet id |
| 246 | |
| 247 | uint32_t totalLength = numeric_cast<uint32_t>(2 * sizeUint32 + 10 * sizeUint32 + infoSize + hardwareVersionSize + |
| 248 | softwareVersionSize + processNameSize + sizeUint32 + |
| 249 | 2 * packetEntries * sizeUint32); |
| 250 | |
| 251 | BOOST_TEST(headerWord1 == totalLength - (2 * sizeUint32)); // data length |
| 252 | |
| 253 | uint32_t offset = sizeUint32 * 2; |
| 254 | BOOST_TEST(ReadUint32(readBuffer2, offset) == SendCounterPacket::PIPE_MAGIC); // pipe_magic |
| 255 | offset += sizeUint32; |
| 256 | BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0)); // stream_metadata_version |
| 257 | offset += sizeUint32; |
| 258 | BOOST_TEST(ReadUint32(readBuffer2, offset) == SendCounterPacket::MAX_METADATA_PACKET_LENGTH); // max_data_len |
| 259 | offset += sizeUint32; |
| 260 | BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast<uint32_t>(getpid())); // pid |
| 261 | offset += sizeUint32; |
| 262 | uint32_t poolOffset = 10 * sizeUint32; |
| 263 | BOOST_TEST(ReadUint32(readBuffer2, offset) == (infoSize ? poolOffset : 0)); // offset_info |
| 264 | offset += sizeUint32; |
| 265 | poolOffset += infoSize; |
| 266 | BOOST_TEST(ReadUint32(readBuffer2, offset) == (hardwareVersionSize ? poolOffset : 0)); // offset_hw_version |
| 267 | offset += sizeUint32; |
| 268 | poolOffset += hardwareVersionSize; |
| 269 | BOOST_TEST(ReadUint32(readBuffer2, offset) == (softwareVersionSize ? poolOffset : 0)); // offset_sw_version |
| 270 | offset += sizeUint32; |
| 271 | poolOffset += softwareVersionSize; |
| 272 | BOOST_TEST(ReadUint32(readBuffer2, offset) == (processNameSize ? poolOffset : 0)); // offset_process_name |
| 273 | offset += sizeUint32; |
| 274 | poolOffset += processNameSize; |
| 275 | BOOST_TEST(ReadUint32(readBuffer2, offset) == (packetEntries ? poolOffset : 0)); // offset_packet_version_table |
| 276 | offset += sizeUint32; |
| 277 | BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved |
| 278 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 279 | const unsigned char* readData2 = readBuffer2->GetReadableData(); |
| 280 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 281 | offset += sizeUint32; |
| 282 | if (infoSize) |
| 283 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 284 | BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 285 | offset += infoSize; |
| 286 | } |
| 287 | |
| 288 | if (hardwareVersionSize) |
| 289 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 290 | BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 291 | offset += hardwareVersionSize; |
| 292 | } |
| 293 | |
| 294 | if (softwareVersionSize) |
| 295 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 296 | BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 297 | offset += softwareVersionSize; |
| 298 | } |
| 299 | |
| 300 | if (processNameSize) |
| 301 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 302 | BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 303 | offset += processNameSize; |
| 304 | } |
| 305 | |
| 306 | if (packetEntries) |
| 307 | { |
| 308 | BOOST_TEST((ReadUint32(readBuffer2, offset) >> 16) == packetEntries); |
| 309 | offset += sizeUint32; |
Ferran Balaguer | 5bf1d32 | 2019-09-13 13:31:40 +0100 | [diff] [blame] | 310 | for (uint32_t i = 0; i < packetEntries - 1; ++i) |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 311 | { |
| 312 | BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 0); |
| 313 | BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == i); |
| 314 | offset += sizeUint32; |
| 315 | BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0)); |
| 316 | offset += sizeUint32; |
| 317 | } |
Ferran Balaguer | 5bf1d32 | 2019-09-13 13:31:40 +0100 | [diff] [blame] | 318 | |
| 319 | BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 1); |
| 320 | BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == 0); |
| 321 | offset += sizeUint32; |
| 322 | BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0)); |
| 323 | offset += sizeUint32; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | BOOST_TEST(offset == totalLength); |
| 327 | } |
| 328 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 329 | BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest) |
| 330 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 331 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 332 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 333 | |
| 334 | // Create a device for testing |
| 335 | uint16_t deviceUid = 27; |
| 336 | const std::string deviceName = "some_device"; |
| 337 | uint16_t deviceCores = 3; |
| 338 | const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores); |
| 339 | |
| 340 | // Create a device record |
| 341 | SendCounterPacket::DeviceRecord deviceRecord; |
| 342 | std::string errorMessage; |
| 343 | bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage); |
| 344 | |
| 345 | BOOST_CHECK(result); |
| 346 | BOOST_CHECK(errorMessage.empty()); |
| 347 | BOOST_CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4] |
| 348 | |
| 349 | uint16_t deviceRecordWord0[] |
| 350 | { |
| 351 | static_cast<uint16_t>(deviceRecord[0] >> 16), |
| 352 | static_cast<uint16_t>(deviceRecord[0]) |
| 353 | }; |
| 354 | BOOST_CHECK(deviceRecordWord0[0] == deviceUid); // uid |
| 355 | BOOST_CHECK(deviceRecordWord0[1] == deviceCores); // cores |
| 356 | BOOST_CHECK(deviceRecord[1] == 0); // name_offset |
| 357 | BOOST_CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name) |
| 358 | BOOST_CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name |
| 359 | } |
| 360 | |
| 361 | BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest) |
| 362 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 363 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 364 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 365 | |
| 366 | // Create a device for testing |
| 367 | uint16_t deviceUid = 27; |
| 368 | const std::string deviceName = "some€£invalid‡device"; |
| 369 | uint16_t deviceCores = 3; |
| 370 | const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores); |
| 371 | |
| 372 | // Create a device record |
| 373 | SendCounterPacket::DeviceRecord deviceRecord; |
| 374 | std::string errorMessage; |
| 375 | bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage); |
| 376 | |
| 377 | BOOST_CHECK(!result); |
| 378 | BOOST_CHECK(!errorMessage.empty()); |
| 379 | BOOST_CHECK(deviceRecord.empty()); |
| 380 | } |
| 381 | |
| 382 | BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest) |
| 383 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 384 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 385 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 386 | |
| 387 | // Create a counter set for testing |
| 388 | uint16_t counterSetUid = 27; |
| 389 | const std::string counterSetName = "some_counter_set"; |
| 390 | uint16_t counterSetCount = 3421; |
| 391 | const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount); |
| 392 | |
| 393 | // Create a counter set record |
| 394 | SendCounterPacket::CounterSetRecord counterSetRecord; |
| 395 | std::string errorMessage; |
| 396 | bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage); |
| 397 | |
| 398 | BOOST_CHECK(result); |
| 399 | BOOST_CHECK(errorMessage.empty()); |
| 400 | BOOST_CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6] |
| 401 | |
| 402 | uint16_t counterSetRecordWord0[] |
| 403 | { |
| 404 | static_cast<uint16_t>(counterSetRecord[0] >> 16), |
| 405 | static_cast<uint16_t>(counterSetRecord[0]) |
| 406 | }; |
| 407 | BOOST_CHECK(counterSetRecordWord0[0] == counterSetUid); // uid |
| 408 | BOOST_CHECK(counterSetRecordWord0[1] == counterSetCount); // cores |
| 409 | BOOST_CHECK(counterSetRecord[1] == 0); // name_offset |
| 410 | BOOST_CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name) |
| 411 | BOOST_CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name |
| 412 | } |
| 413 | |
| 414 | BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest) |
| 415 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 416 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 417 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 418 | |
| 419 | // Create a counter set for testing |
| 420 | uint16_t counterSetUid = 27; |
| 421 | const std::string counterSetName = "some invalid_counter€£set"; |
| 422 | uint16_t counterSetCount = 3421; |
| 423 | const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount); |
| 424 | |
| 425 | // Create a counter set record |
| 426 | SendCounterPacket::CounterSetRecord counterSetRecord; |
| 427 | std::string errorMessage; |
| 428 | bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage); |
| 429 | |
| 430 | BOOST_CHECK(!result); |
| 431 | BOOST_CHECK(!errorMessage.empty()); |
| 432 | BOOST_CHECK(counterSetRecord.empty()); |
| 433 | } |
| 434 | |
| 435 | BOOST_AUTO_TEST_CASE(CreateEventRecordTest) |
| 436 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 437 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 438 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 439 | |
| 440 | // Create a counter for testing |
| 441 | uint16_t counterUid = 7256; |
| 442 | uint16_t maxCounterUid = 132; |
| 443 | uint16_t deviceUid = 132; |
| 444 | uint16_t counterSetUid = 4497; |
| 445 | uint16_t counterClass = 1; |
| 446 | uint16_t counterInterpolation = 1; |
| 447 | double counterMultiplier = 1234.567f; |
| 448 | const std::string counterName = "some_valid_counter"; |
| 449 | const std::string counterDescription = "a_counter_for_testing"; |
| 450 | const std::string counterUnits = "Mrads2"; |
| 451 | const CounterPtr counter = std::make_unique<Counter>(counterUid, |
| 452 | maxCounterUid, |
| 453 | counterClass, |
| 454 | counterInterpolation, |
| 455 | counterMultiplier, |
| 456 | counterName, |
| 457 | counterDescription, |
| 458 | counterUnits, |
| 459 | deviceUid, |
| 460 | counterSetUid); |
| 461 | BOOST_ASSERT(counter); |
| 462 | |
| 463 | // Create an event record |
| 464 | SendCounterPacket::EventRecord eventRecord; |
| 465 | std::string errorMessage; |
| 466 | bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage); |
| 467 | |
| 468 | BOOST_CHECK(result); |
| 469 | BOOST_CHECK(errorMessage.empty()); |
| 470 | BOOST_CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3] |
| 471 | |
| 472 | uint16_t eventRecordWord0[] |
| 473 | { |
| 474 | static_cast<uint16_t>(eventRecord[0] >> 16), |
| 475 | static_cast<uint16_t>(eventRecord[0]) |
| 476 | }; |
| 477 | uint16_t eventRecordWord1[] |
| 478 | { |
| 479 | static_cast<uint16_t>(eventRecord[1] >> 16), |
| 480 | static_cast<uint16_t>(eventRecord[1]) |
| 481 | }; |
| 482 | uint16_t eventRecordWord2[] |
| 483 | { |
| 484 | static_cast<uint16_t>(eventRecord[2] >> 16), |
| 485 | static_cast<uint16_t>(eventRecord[2]) |
| 486 | }; |
| 487 | uint32_t eventRecordWord34[] |
| 488 | { |
| 489 | eventRecord[3], |
| 490 | eventRecord[4] |
| 491 | }; |
| 492 | BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid |
| 493 | BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid |
| 494 | BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device |
| 495 | BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set |
| 496 | BOOST_CHECK(eventRecordWord2[0] == counterClass); // class |
| 497 | BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation |
| 498 | BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier |
| 499 | |
| 500 | ARMNN_NO_CONVERSION_WARN_BEGIN |
| 501 | uint32_t counterNameOffset = 0; // The name is the first item in pool |
| 502 | uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset |
| 503 | 4u + // Counter name length (uint32_t) |
| 504 | counterName.size() + // 18u |
| 505 | 1u + // Null-terminator |
| 506 | 1u; // Rounding to the next word |
| 507 | size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset |
| 508 | 4u + // Counter description length (uint32_t) |
| 509 | counterDescription.size() + // 21u |
| 510 | 1u + // Null-terminator |
| 511 | 2u; // Rounding to the next word |
| 512 | ARMNN_NO_CONVERSION_WARN_END |
| 513 | |
| 514 | BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset |
| 515 | BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset |
| 516 | BOOST_CHECK(eventRecord[7] == counterUnitsOffset); // units_offset |
| 517 | |
| 518 | auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data() + 8u); // The start of the pool |
| 519 | size_t uint32_t_size = sizeof(uint32_t); |
| 520 | |
| 521 | // The length of the SWTrace string (name) |
| 522 | BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1); |
| 523 | // The counter name |
| 524 | BOOST_CHECK(std::memcmp(eventRecordPool + |
| 525 | counterNameOffset + // Offset |
| 526 | uint32_t_size /* The length of the name */, |
| 527 | counterName.data(), |
| 528 | counterName.size()) == 0); // name |
| 529 | // The null-terminator at the end of the name |
| 530 | BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0'); |
| 531 | |
| 532 | // The length of the SWTrace string (description) |
| 533 | BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1); |
| 534 | // The counter description |
| 535 | BOOST_CHECK(std::memcmp(eventRecordPool + |
| 536 | counterDescriptionOffset + // Offset |
| 537 | uint32_t_size /* The length of the description */, |
| 538 | counterDescription.data(), |
| 539 | counterDescription.size()) == 0); // description |
| 540 | // The null-terminator at the end of the description |
| 541 | BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0'); |
| 542 | |
| 543 | // The length of the SWTrace namestring (units) |
| 544 | BOOST_CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1); |
| 545 | // The counter units |
| 546 | BOOST_CHECK(std::memcmp(eventRecordPool + |
| 547 | counterUnitsOffset + // Offset |
| 548 | uint32_t_size /* The length of the units */, |
| 549 | counterUnits.data(), |
| 550 | counterUnits.size()) == 0); // units |
| 551 | // The null-terminator at the end of the units |
| 552 | BOOST_CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0'); |
| 553 | } |
| 554 | |
| 555 | BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest) |
| 556 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 557 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 558 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 559 | |
| 560 | // Create a counter for testing |
| 561 | uint16_t counterUid = 44312; |
| 562 | uint16_t maxCounterUid = 345; |
| 563 | uint16_t deviceUid = 101; |
| 564 | uint16_t counterSetUid = 34035; |
| 565 | uint16_t counterClass = 0; |
| 566 | uint16_t counterInterpolation = 1; |
| 567 | double counterMultiplier = 4435.0023f; |
| 568 | const std::string counterName = "some_valid_counter"; |
| 569 | const std::string counterDescription = "a_counter_for_testing"; |
| 570 | const CounterPtr counter = std::make_unique<Counter>(counterUid, |
| 571 | maxCounterUid, |
| 572 | counterClass, |
| 573 | counterInterpolation, |
| 574 | counterMultiplier, |
| 575 | counterName, |
| 576 | counterDescription, |
| 577 | "", |
| 578 | deviceUid, |
| 579 | counterSetUid); |
| 580 | BOOST_ASSERT(counter); |
| 581 | |
| 582 | // Create an event record |
| 583 | SendCounterPacket::EventRecord eventRecord; |
| 584 | std::string errorMessage; |
| 585 | bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage); |
| 586 | |
| 587 | BOOST_CHECK(result); |
| 588 | BOOST_CHECK(errorMessage.empty()); |
| 589 | BOOST_CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7] |
| 590 | |
| 591 | uint16_t eventRecordWord0[] |
| 592 | { |
| 593 | static_cast<uint16_t>(eventRecord[0] >> 16), |
| 594 | static_cast<uint16_t>(eventRecord[0]) |
| 595 | }; |
| 596 | uint16_t eventRecordWord1[] |
| 597 | { |
| 598 | static_cast<uint16_t>(eventRecord[1] >> 16), |
| 599 | static_cast<uint16_t>(eventRecord[1]) |
| 600 | }; |
| 601 | uint16_t eventRecordWord2[] |
| 602 | { |
| 603 | static_cast<uint16_t>(eventRecord[2] >> 16), |
| 604 | static_cast<uint16_t>(eventRecord[2]) |
| 605 | }; |
| 606 | uint32_t eventRecordWord34[] |
| 607 | { |
| 608 | eventRecord[3], |
| 609 | eventRecord[4] |
| 610 | }; |
| 611 | BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid |
| 612 | BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid |
| 613 | BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device |
| 614 | BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set |
| 615 | BOOST_CHECK(eventRecordWord2[0] == counterClass); // class |
| 616 | BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation |
| 617 | BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier |
| 618 | |
| 619 | ARMNN_NO_CONVERSION_WARN_BEGIN |
| 620 | uint32_t counterNameOffset = 0; // The name is the first item in pool |
| 621 | uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset |
| 622 | 4u + // Counter name length (uint32_t) |
| 623 | counterName.size() + // 18u |
| 624 | 1u + // Null-terminator |
| 625 | 1u; // Rounding to the next word |
| 626 | ARMNN_NO_CONVERSION_WARN_END |
| 627 | |
| 628 | BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset |
| 629 | BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset |
| 630 | BOOST_CHECK(eventRecord[7] == 0); // units_offset |
| 631 | |
| 632 | auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data() + 8u); // The start of the pool |
| 633 | size_t uint32_t_size = sizeof(uint32_t); |
| 634 | |
| 635 | // The length of the SWTrace string (name) |
| 636 | BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1); |
| 637 | // The counter name |
| 638 | BOOST_CHECK(std::memcmp(eventRecordPool + |
| 639 | counterNameOffset + // Offset |
| 640 | uint32_t_size, // The length of the name |
| 641 | counterName.data(), |
| 642 | counterName.size()) == 0); // name |
| 643 | // The null-terminator at the end of the name |
| 644 | BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0'); |
| 645 | |
| 646 | // The length of the SWTrace string (description) |
| 647 | BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1); |
| 648 | // The counter description |
| 649 | BOOST_CHECK(std::memcmp(eventRecordPool + |
| 650 | counterDescriptionOffset + // Offset |
| 651 | uint32_t_size, // The length of the description |
| 652 | counterDescription.data(), |
| 653 | counterDescription.size()) == 0); // description |
| 654 | // The null-terminator at the end of the description |
| 655 | BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0'); |
| 656 | } |
| 657 | |
| 658 | BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1) |
| 659 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 660 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 661 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 662 | |
| 663 | // Create a counter for testing |
| 664 | uint16_t counterUid = 7256; |
| 665 | uint16_t maxCounterUid = 132; |
| 666 | uint16_t deviceUid = 132; |
| 667 | uint16_t counterSetUid = 4497; |
| 668 | uint16_t counterClass = 1; |
| 669 | uint16_t counterInterpolation = 1; |
| 670 | double counterMultiplier = 1234.567f; |
| 671 | const std::string counterName = "some_invalid_counter £££"; // Invalid name |
| 672 | const std::string counterDescription = "a_counter_for_testing"; |
| 673 | const std::string counterUnits = "Mrads2"; |
| 674 | const CounterPtr counter = std::make_unique<Counter>(counterUid, |
| 675 | maxCounterUid, |
| 676 | counterClass, |
| 677 | counterInterpolation, |
| 678 | counterMultiplier, |
| 679 | counterName, |
| 680 | counterDescription, |
| 681 | counterUnits, |
| 682 | deviceUid, |
| 683 | counterSetUid); |
| 684 | BOOST_ASSERT(counter); |
| 685 | |
| 686 | // Create an event record |
| 687 | SendCounterPacket::EventRecord eventRecord; |
| 688 | std::string errorMessage; |
| 689 | bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage); |
| 690 | |
| 691 | BOOST_CHECK(!result); |
| 692 | BOOST_CHECK(!errorMessage.empty()); |
| 693 | BOOST_CHECK(eventRecord.empty()); |
| 694 | } |
| 695 | |
| 696 | BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2) |
| 697 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 698 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 699 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 700 | |
| 701 | // Create a counter for testing |
| 702 | uint16_t counterUid = 7256; |
| 703 | uint16_t maxCounterUid = 132; |
| 704 | uint16_t deviceUid = 132; |
| 705 | uint16_t counterSetUid = 4497; |
| 706 | uint16_t counterClass = 1; |
| 707 | uint16_t counterInterpolation = 1; |
| 708 | double counterMultiplier = 1234.567f; |
| 709 | const std::string counterName = "some_invalid_counter"; |
| 710 | const std::string counterDescription = "an invalid d€scription"; // Invalid description |
| 711 | const std::string counterUnits = "Mrads2"; |
| 712 | const CounterPtr counter = std::make_unique<Counter>(counterUid, |
| 713 | maxCounterUid, |
| 714 | counterClass, |
| 715 | counterInterpolation, |
| 716 | counterMultiplier, |
| 717 | counterName, |
| 718 | counterDescription, |
| 719 | counterUnits, |
| 720 | deviceUid, |
| 721 | counterSetUid); |
| 722 | BOOST_ASSERT(counter); |
| 723 | |
| 724 | // Create an event record |
| 725 | SendCounterPacket::EventRecord eventRecord; |
| 726 | std::string errorMessage; |
| 727 | bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage); |
| 728 | |
| 729 | BOOST_CHECK(!result); |
| 730 | BOOST_CHECK(!errorMessage.empty()); |
| 731 | BOOST_CHECK(eventRecord.empty()); |
| 732 | } |
| 733 | |
| 734 | BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3) |
| 735 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 736 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 737 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 738 | |
| 739 | // Create a counter for testing |
| 740 | uint16_t counterUid = 7256; |
| 741 | uint16_t maxCounterUid = 132; |
| 742 | uint16_t deviceUid = 132; |
| 743 | uint16_t counterSetUid = 4497; |
| 744 | uint16_t counterClass = 1; |
| 745 | uint16_t counterInterpolation = 1; |
| 746 | double counterMultiplier = 1234.567f; |
| 747 | const std::string counterName = "some_invalid_counter"; |
| 748 | const std::string counterDescription = "a valid description"; |
| 749 | const std::string counterUnits = "Mrad s2"; // Invalid units |
| 750 | const CounterPtr counter = std::make_unique<Counter>(counterUid, |
| 751 | maxCounterUid, |
| 752 | counterClass, |
| 753 | counterInterpolation, |
| 754 | counterMultiplier, |
| 755 | counterName, |
| 756 | counterDescription, |
| 757 | counterUnits, |
| 758 | deviceUid, |
| 759 | counterSetUid); |
| 760 | BOOST_ASSERT(counter); |
| 761 | |
| 762 | // Create an event record |
| 763 | SendCounterPacket::EventRecord eventRecord; |
| 764 | std::string errorMessage; |
| 765 | bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage); |
| 766 | |
| 767 | BOOST_CHECK(!result); |
| 768 | BOOST_CHECK(!errorMessage.empty()); |
| 769 | BOOST_CHECK(eventRecord.empty()); |
| 770 | } |
| 771 | |
| 772 | BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest) |
| 773 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 774 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 775 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 776 | |
| 777 | // Create a category for testing |
| 778 | const std::string categoryName = "some_category"; |
| 779 | uint16_t deviceUid = 1302; |
| 780 | uint16_t counterSetUid = 20734; |
| 781 | const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid); |
| 782 | BOOST_ASSERT(category); |
| 783 | category->m_Counters = { 11u, 23u, 5670u }; |
| 784 | |
| 785 | // Create a collection of counters |
| 786 | Counters counters; |
| 787 | counters.insert(std::make_pair<uint16_t, CounterPtr>(11, |
| 788 | CounterPtr(new Counter(11, |
| 789 | 1234, |
| 790 | 0, |
| 791 | 1, |
| 792 | 534.0003f, |
| 793 | "counter1", |
| 794 | "the first counter", |
| 795 | "millipi2", |
| 796 | 0, |
| 797 | 0)))); |
| 798 | counters.insert(std::make_pair<uint16_t, CounterPtr>(23, |
| 799 | CounterPtr(new Counter(23, |
| 800 | 344, |
| 801 | 1, |
| 802 | 1, |
| 803 | 534.0003f, |
| 804 | "this is counter 2", |
| 805 | "the second counter", |
| 806 | "", |
| 807 | 0, |
| 808 | 0)))); |
| 809 | counters.insert(std::make_pair<uint16_t, CounterPtr>(5670, |
| 810 | CounterPtr(new Counter(5670, |
| 811 | 31, |
| 812 | 0, |
| 813 | 0, |
| 814 | 534.0003f, |
| 815 | "and this is number 3", |
| 816 | "the third counter", |
| 817 | "blah_per_second", |
| 818 | 0, |
| 819 | 0)))); |
| 820 | Counter* counter1 = counters.find(11)->second.get(); |
| 821 | Counter* counter2 = counters.find(23)->second.get(); |
| 822 | Counter* counter3 = counters.find(5670)->second.get(); |
| 823 | BOOST_ASSERT(counter1); |
| 824 | BOOST_ASSERT(counter2); |
| 825 | BOOST_ASSERT(counter3); |
| 826 | uint16_t categoryEventCount = boost::numeric_cast<uint16_t>(counters.size()); |
| 827 | |
| 828 | // Create a category record |
| 829 | SendCounterPacket::CategoryRecord categoryRecord; |
| 830 | std::string errorMessage; |
| 831 | bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage); |
| 832 | |
| 833 | BOOST_CHECK(result); |
| 834 | BOOST_CHECK(errorMessage.empty()); |
| 835 | BOOST_CHECK(categoryRecord.size() == 80); // Size in words: header [4] + event pointer table [3] + |
| 836 | // category name [5] + event records [68 = 22 + 20 + 26] |
| 837 | |
| 838 | uint16_t categoryRecordWord0[] |
| 839 | { |
| 840 | static_cast<uint16_t>(categoryRecord[0] >> 16), |
| 841 | static_cast<uint16_t>(categoryRecord[0]) |
| 842 | }; |
| 843 | uint16_t categoryRecordWord1[] |
| 844 | { |
| 845 | static_cast<uint16_t>(categoryRecord[1] >> 16), |
| 846 | static_cast<uint16_t>(categoryRecord[1]) |
| 847 | }; |
| 848 | BOOST_CHECK(categoryRecordWord0[0] == deviceUid); // device |
| 849 | BOOST_CHECK(categoryRecordWord0[1] == counterSetUid); // counter_set |
| 850 | BOOST_CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count |
| 851 | BOOST_CHECK(categoryRecordWord1[1] == 0); // reserved |
| 852 | |
| 853 | size_t uint32_t_size = sizeof(uint32_t); |
| 854 | |
| 855 | ARMNN_NO_CONVERSION_WARN_BEGIN |
| 856 | uint32_t eventPointerTableOffset = 0; // The event pointer table is the first item in pool |
| 857 | uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset |
| 858 | categoryEventCount * uint32_t_size; // The size of the event pointer table |
| 859 | ARMNN_NO_CONVERSION_WARN_END |
| 860 | |
| 861 | BOOST_CHECK(categoryRecord[2] == eventPointerTableOffset); // event_pointer_table_offset |
| 862 | BOOST_CHECK(categoryRecord[3] == categoryNameOffset); // name_offset |
| 863 | |
| 864 | auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data() + 4u); // The start of the pool |
| 865 | |
| 866 | // The event pointer table |
| 867 | uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size]; |
| 868 | uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size]; |
| 869 | uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size]; |
| 870 | BOOST_CHECK(eventRecord0Offset == 32); |
| 871 | BOOST_CHECK(eventRecord1Offset == 120); |
| 872 | BOOST_CHECK(eventRecord2Offset == 200); |
| 873 | |
| 874 | // The length of the SWTrace namestring (name) |
| 875 | BOOST_CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1); |
| 876 | // The category name |
| 877 | BOOST_CHECK(std::memcmp(categoryRecordPool + |
| 878 | categoryNameOffset + // Offset |
| 879 | uint32_t_size, // The length of the name |
| 880 | categoryName.data(), |
| 881 | categoryName.size()) == 0); // name |
| 882 | // The null-terminator at the end of the name |
| 883 | BOOST_CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0'); |
| 884 | |
| 885 | // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records, |
| 886 | // as the event records already have a number of unit tests dedicated to them |
| 887 | |
| 888 | // Counter1 UID and max counter UID |
| 889 | uint16_t eventRecord0Word0[2] = { 0u, 0u }; |
| 890 | std::memcpy(eventRecord0Word0, categoryRecordPool + eventRecord0Offset, sizeof(eventRecord0Word0)); |
| 891 | BOOST_CHECK(eventRecord0Word0[0] == counter1->m_Uid); |
| 892 | BOOST_CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid); |
| 893 | |
| 894 | // Counter1 name |
| 895 | uint32_t counter1NameOffset = 0; |
| 896 | std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size); |
| 897 | BOOST_CHECK(counter1NameOffset == 0); |
| 898 | // The length of the SWTrace string (name) |
| 899 | BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record |
| 900 | 8u * uint32_t_size + // Offset to the event record pool |
| 901 | counter1NameOffset // Offset to the name of the counter |
| 902 | ] == counter1->m_Name.size() + 1); // The length of the name including the |
| 903 | // null-terminator |
| 904 | // The counter1 name |
| 905 | BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool |
| 906 | eventRecord0Offset + // Offset to the event record |
| 907 | 8u * uint32_t_size + // Offset to the event record pool |
| 908 | counter1NameOffset + // Offset to the name of the counter |
| 909 | uint32_t_size, // The length of the name |
| 910 | counter1->m_Name.data(), |
| 911 | counter1->m_Name.size()) == 0); // name |
| 912 | // The null-terminator at the end of the counter1 name |
| 913 | BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record |
| 914 | 8u * uint32_t_size + // Offset to the event record pool |
| 915 | counter1NameOffset + // Offset to the name of the counter |
| 916 | uint32_t_size + // The length of the name |
| 917 | counter1->m_Name.size() // The name of the counter |
| 918 | ] == '\0'); |
| 919 | |
| 920 | // Counter2 name |
| 921 | uint32_t counter2NameOffset = 0; |
| 922 | std::memcpy(&counter2NameOffset, categoryRecordPool + eventRecord1Offset + 5u * uint32_t_size, uint32_t_size); |
| 923 | BOOST_CHECK(counter2NameOffset == 0); |
| 924 | // The length of the SWTrace string (name) |
| 925 | BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record |
| 926 | 8u * uint32_t_size + // Offset to the event record pool |
| 927 | counter2NameOffset // Offset to the name of the counter |
| 928 | ] == counter2->m_Name.size() + 1); // The length of the name including the |
| 929 | // null-terminator |
| 930 | // The counter2 name |
| 931 | BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool |
| 932 | eventRecord1Offset + // Offset to the event record |
| 933 | 8u * uint32_t_size + // Offset to the event record pool |
| 934 | counter2NameOffset + // Offset to the name of the counter |
| 935 | uint32_t_size, // The length of the name |
| 936 | counter2->m_Name.data(), |
| 937 | counter2->m_Name.size()) == 0); // name |
| 938 | // The null-terminator at the end of the counter2 name |
| 939 | BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record |
| 940 | 8u * uint32_t_size + // Offset to the event record pool |
| 941 | counter2NameOffset + // Offset to the name of the counter |
| 942 | uint32_t_size + // The length of the name |
| 943 | counter2->m_Name.size() // The name of the counter |
| 944 | ] == '\0'); |
| 945 | |
| 946 | // Counter3 name |
| 947 | uint32_t counter3NameOffset = 0; |
| 948 | std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size); |
| 949 | BOOST_CHECK(counter3NameOffset == 0); |
| 950 | // The length of the SWTrace string (name) |
| 951 | BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record |
| 952 | 8u * uint32_t_size + // Offset to the event record pool |
| 953 | counter3NameOffset // Offset to the name of the counter |
| 954 | ] == counter3->m_Name.size() + 1); // The length of the name including the |
| 955 | // null-terminator |
| 956 | // The counter3 name |
| 957 | BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool |
| 958 | eventRecord2Offset + // Offset to the event record |
| 959 | 8u * uint32_t_size + // Offset to the event record pool |
| 960 | counter3NameOffset + // Offset to the name of the counter |
| 961 | uint32_t_size, // The length of the name |
| 962 | counter3->m_Name.data(), |
| 963 | counter3->m_Name.size()) == 0); // name |
| 964 | // The null-terminator at the end of the counter3 name |
| 965 | BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record |
| 966 | 8u * uint32_t_size + // Offset to the event record pool |
| 967 | counter3NameOffset + // Offset to the name of the counter |
| 968 | uint32_t_size + // The length of the name |
| 969 | counter3->m_Name.size() // The name of the counter |
| 970 | ] == '\0'); |
| 971 | } |
| 972 | |
| 973 | BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1) |
| 974 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 975 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 976 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 977 | |
| 978 | // Create a category for testing |
| 979 | const std::string categoryName = "some invalid category"; |
| 980 | uint16_t deviceUid = 1302; |
| 981 | uint16_t counterSetUid = 20734; |
| 982 | const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid); |
| 983 | BOOST_CHECK(category); |
| 984 | |
| 985 | // Create a category record |
| 986 | Counters counters; |
| 987 | SendCounterPacket::CategoryRecord categoryRecord; |
| 988 | std::string errorMessage; |
| 989 | bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage); |
| 990 | |
| 991 | BOOST_CHECK(!result); |
| 992 | BOOST_CHECK(!errorMessage.empty()); |
| 993 | BOOST_CHECK(categoryRecord.empty()); |
| 994 | } |
| 995 | |
| 996 | BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest2) |
| 997 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 998 | MockBufferManager mockBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 999 | SendCounterPacketTest sendCounterPacketTest(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1000 | |
| 1001 | // Create a category for testing |
| 1002 | const std::string categoryName = "some_category"; |
| 1003 | uint16_t deviceUid = 1302; |
| 1004 | uint16_t counterSetUid = 20734; |
| 1005 | const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid); |
| 1006 | BOOST_CHECK(category); |
| 1007 | category->m_Counters = { 11u, 23u, 5670u }; |
| 1008 | |
| 1009 | // Create a collection of counters |
| 1010 | Counters counters; |
| 1011 | counters.insert(std::make_pair<uint16_t, CounterPtr>(11, |
| 1012 | CounterPtr(new Counter(11, |
| 1013 | 1234, |
| 1014 | 0, |
| 1015 | 1, |
| 1016 | 534.0003f, |
| 1017 | "count€r1", // Invalid name |
| 1018 | "the first counter", |
| 1019 | "millipi2", |
| 1020 | 0, |
| 1021 | 0)))); |
| 1022 | |
| 1023 | Counter* counter1 = counters.find(11)->second.get(); |
| 1024 | BOOST_CHECK(counter1); |
| 1025 | |
| 1026 | // Create a category record |
| 1027 | SendCounterPacket::CategoryRecord categoryRecord; |
| 1028 | std::string errorMessage; |
| 1029 | bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage); |
| 1030 | |
| 1031 | BOOST_CHECK(!result); |
| 1032 | BOOST_CHECK(!errorMessage.empty()); |
| 1033 | BOOST_CHECK(categoryRecord.empty()); |
| 1034 | } |
| 1035 | |
| 1036 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest1) |
| 1037 | { |
| 1038 | // The counter directory used for testing |
| 1039 | CounterDirectory counterDirectory; |
| 1040 | |
| 1041 | // Register a device |
| 1042 | const std::string device1Name = "device1"; |
| 1043 | const Device* device1 = nullptr; |
| 1044 | BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3)); |
| 1045 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 1); |
| 1046 | BOOST_CHECK(device1); |
| 1047 | |
| 1048 | // Register a device |
| 1049 | const std::string device2Name = "device2"; |
| 1050 | const Device* device2 = nullptr; |
| 1051 | BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name)); |
| 1052 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 2); |
| 1053 | BOOST_CHECK(device2); |
| 1054 | |
| 1055 | // Buffer with not enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1056 | MockBufferManager mockBuffer(10); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1057 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1058 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), |
| 1059 | armnn::profiling::BufferExhaustion); |
| 1060 | } |
| 1061 | |
| 1062 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest2) |
| 1063 | { |
| 1064 | // The counter directory used for testing |
| 1065 | CounterDirectory counterDirectory; |
| 1066 | |
| 1067 | // Register a device |
| 1068 | const std::string device1Name = "device1"; |
| 1069 | const Device* device1 = nullptr; |
| 1070 | BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3)); |
| 1071 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 1); |
| 1072 | BOOST_CHECK(device1); |
| 1073 | |
| 1074 | // Register a device |
| 1075 | const std::string device2Name = "device2"; |
| 1076 | const Device* device2 = nullptr; |
| 1077 | BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name)); |
| 1078 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 2); |
| 1079 | BOOST_CHECK(device2); |
| 1080 | |
| 1081 | // Register a counter set |
| 1082 | const std::string counterSet1Name = "counterset1"; |
| 1083 | const CounterSet* counterSet1 = nullptr; |
| 1084 | BOOST_CHECK_NO_THROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name)); |
| 1085 | BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1); |
| 1086 | BOOST_CHECK(counterSet1); |
| 1087 | |
| 1088 | // Register a category associated to "device1" and "counterset1" |
| 1089 | const std::string category1Name = "category1"; |
| 1090 | const Category* category1 = nullptr; |
| 1091 | BOOST_CHECK_NO_THROW(category1 = counterDirectory.RegisterCategory(category1Name, |
| 1092 | device1->m_Uid, |
| 1093 | counterSet1->m_Uid)); |
| 1094 | BOOST_CHECK(counterDirectory.GetCategoryCount() == 1); |
| 1095 | BOOST_CHECK(category1); |
| 1096 | |
| 1097 | // Register a category not associated to "device2" but no counter set |
| 1098 | const std::string category2Name = "category2"; |
| 1099 | const Category* category2 = nullptr; |
| 1100 | BOOST_CHECK_NO_THROW(category2 = counterDirectory.RegisterCategory(category2Name, |
| 1101 | device2->m_Uid)); |
| 1102 | BOOST_CHECK(counterDirectory.GetCategoryCount() == 2); |
| 1103 | BOOST_CHECK(category2); |
| 1104 | |
| 1105 | // Register a counter associated to "category1" |
| 1106 | const Counter* counter1 = nullptr; |
| 1107 | BOOST_CHECK_NO_THROW(counter1 = counterDirectory.RegisterCounter(category1Name, |
| 1108 | 0, |
| 1109 | 1, |
| 1110 | 123.45f, |
| 1111 | "counter1", |
| 1112 | "counter1description", |
| 1113 | std::string("counter1units"))); |
| 1114 | BOOST_CHECK(counterDirectory.GetCounterCount() == 3); |
| 1115 | BOOST_CHECK(counter1); |
| 1116 | |
| 1117 | // Register a counter associated to "category1" |
| 1118 | const Counter* counter2 = nullptr; |
| 1119 | BOOST_CHECK_NO_THROW(counter2 = counterDirectory.RegisterCounter(category1Name, |
| 1120 | 1, |
| 1121 | 0, |
| 1122 | 330.1245656765f, |
| 1123 | "counter2", |
| 1124 | "counter2description", |
| 1125 | std::string("counter2units"), |
| 1126 | armnn::EmptyOptional(), |
| 1127 | device2->m_Uid, |
| 1128 | 0)); |
| 1129 | BOOST_CHECK(counterDirectory.GetCounterCount() == 4); |
| 1130 | BOOST_CHECK(counter2); |
| 1131 | |
| 1132 | // Register a counter associated to "category2" |
| 1133 | const Counter* counter3 = nullptr; |
| 1134 | BOOST_CHECK_NO_THROW(counter3 = counterDirectory.RegisterCounter(category2Name, |
| 1135 | 1, |
| 1136 | 1, |
| 1137 | 0.0000045399f, |
| 1138 | "counter3", |
| 1139 | "counter3description", |
| 1140 | armnn::EmptyOptional(), |
| 1141 | 5, |
| 1142 | device2->m_Uid, |
| 1143 | counterSet1->m_Uid)); |
| 1144 | BOOST_CHECK(counterDirectory.GetCounterCount() == 9); |
| 1145 | BOOST_CHECK(counter3); |
| 1146 | |
| 1147 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1148 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1149 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1150 | BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory)); |
| 1151 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1152 | // Get the readable buffer |
| 1153 | auto readBuffer = mockBuffer.GetReadableBuffer(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1154 | |
| 1155 | // Check the packet header |
| 1156 | uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0); |
| 1157 | uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4); |
| 1158 | BOOST_TEST(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family |
| 1159 | BOOST_TEST(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 1160 | BOOST_TEST(packetHeaderWord1 == 936); // data_length |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1161 | |
| 1162 | // Check the body header |
| 1163 | uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8); |
| 1164 | uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12); |
| 1165 | uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16); |
| 1166 | uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20); |
| 1167 | uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24); |
| 1168 | uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28); |
| 1169 | uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16); |
| 1170 | uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16); |
| 1171 | uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16); |
| 1172 | BOOST_TEST(deviceRecordCount == 2); // device_records_count |
| 1173 | BOOST_TEST(bodyHeaderWord1 == 0); // device_records_pointer_table_offset |
| 1174 | BOOST_TEST(counterSetRecordCount == 1); // counter_set_count |
| 1175 | BOOST_TEST(bodyHeaderWord3 == 8); // counter_set_pointer_table_offset |
| 1176 | BOOST_TEST(categoryRecordCount == 2); // categories_count |
| 1177 | BOOST_TEST(bodyHeaderWord5 == 12); // categories_pointer_table_offset |
| 1178 | |
| 1179 | // Check the device records pointer table |
| 1180 | uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32); |
| 1181 | uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36); |
| 1182 | BOOST_TEST(deviceRecordOffset0 == 0); // Device record offset for "device1" |
| 1183 | BOOST_TEST(deviceRecordOffset1 == 20); // Device record offset for "device2" |
| 1184 | |
| 1185 | // Check the counter set pointer table |
| 1186 | uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40); |
| 1187 | BOOST_TEST(counterSetRecordOffset0 == 40); // Counter set record offset for "counterset1" |
| 1188 | |
| 1189 | // Check the category pointer table |
| 1190 | uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44); |
| 1191 | uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48); |
| 1192 | BOOST_TEST(categoryRecordOffset0 == 64); // Category record offset for "category1" |
| 1193 | BOOST_TEST(categoryRecordOffset1 == 476); // Category record offset for "category2" |
| 1194 | |
| 1195 | // Get the device record pool offset |
| 1196 | uint32_t uint32_t_size = sizeof(uint32_t); |
| 1197 | uint32_t packetBodyPoolOffset = 2u * uint32_t_size + // packet_header |
| 1198 | 6u * uint32_t_size + // body_header |
| 1199 | deviceRecordCount * uint32_t_size + // Size of device_records_pointer_table |
| 1200 | counterSetRecordCount * uint32_t_size + // Size of counter_set_pointer_table |
| 1201 | categoryRecordCount * uint32_t_size; // Size of categories_pointer_table |
| 1202 | |
| 1203 | // Device record structure/collection used for testing |
| 1204 | struct DeviceRecord |
| 1205 | { |
| 1206 | uint16_t uid; |
| 1207 | uint16_t cores; |
| 1208 | uint32_t name_offset; |
| 1209 | uint32_t name_length; |
| 1210 | std::string name; |
| 1211 | }; |
| 1212 | std::vector<DeviceRecord> deviceRecords; |
| 1213 | uint32_t deviceRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header |
| 1214 | 6u * uint32_t_size + // body_header |
| 1215 | bodyHeaderWord1; // device_records_pointer_table_offset |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1216 | |
| 1217 | const unsigned char* readData = readBuffer->GetReadableData(); |
| 1218 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1219 | for (uint32_t i = 0; i < deviceRecordCount; i++) |
| 1220 | { |
| 1221 | // Get the device record offset |
| 1222 | uint32_t deviceRecordOffset = ReadUint32(readBuffer, deviceRecordsPointerTableOffset + i * uint32_t_size); |
| 1223 | |
| 1224 | // Collect the data for the device record |
| 1225 | uint32_t deviceRecordWord0 = ReadUint32(readBuffer, |
| 1226 | packetBodyPoolOffset + deviceRecordOffset + 0 * uint32_t_size); |
| 1227 | uint32_t deviceRecordWord1 = ReadUint32(readBuffer, |
| 1228 | packetBodyPoolOffset + deviceRecordOffset + 1 * uint32_t_size); |
| 1229 | DeviceRecord deviceRecord; |
| 1230 | deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid |
| 1231 | deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores |
| 1232 | deviceRecord.name_offset = deviceRecordWord1; // name_offset |
| 1233 | |
| 1234 | uint32_t deviceRecordPoolOffset = packetBodyPoolOffset + // Packet body offset |
| 1235 | deviceRecordOffset + // Device record offset |
| 1236 | 2 * uint32_t_size + // Device record header |
| 1237 | deviceRecord.name_offset; // Device name offset |
| 1238 | uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset); |
| 1239 | deviceRecord.name_length = deviceRecordNameLength; // name_length |
| 1240 | unsigned char deviceRecordNameNullTerminator = // name null-terminator |
| 1241 | ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1); |
| 1242 | BOOST_CHECK(deviceRecordNameNullTerminator == '\0'); |
| 1243 | std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1); |
| 1244 | std::memcpy(deviceRecordNameBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1245 | readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size()); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1246 | deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name |
| 1247 | |
| 1248 | deviceRecords.push_back(deviceRecord); |
| 1249 | } |
| 1250 | |
| 1251 | // Check that the device records are correct |
| 1252 | BOOST_CHECK(deviceRecords.size() == 2); |
| 1253 | for (const DeviceRecord& deviceRecord : deviceRecords) |
| 1254 | { |
| 1255 | const Device* device = counterDirectory.GetDevice(deviceRecord.uid); |
| 1256 | BOOST_CHECK(device); |
| 1257 | BOOST_CHECK(device->m_Uid == deviceRecord.uid); |
| 1258 | BOOST_CHECK(device->m_Cores == deviceRecord.cores); |
| 1259 | BOOST_CHECK(device->m_Name == deviceRecord.name); |
| 1260 | } |
| 1261 | |
| 1262 | // Counter set record structure/collection used for testing |
| 1263 | struct CounterSetRecord |
| 1264 | { |
| 1265 | uint16_t uid; |
| 1266 | uint16_t count; |
| 1267 | uint32_t name_offset; |
| 1268 | uint32_t name_length; |
| 1269 | std::string name; |
| 1270 | }; |
| 1271 | std::vector<CounterSetRecord> counterSetRecords; |
| 1272 | uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header |
| 1273 | 6u * uint32_t_size + // body_header |
| 1274 | bodyHeaderWord3; // counter_set_pointer_table_offset |
| 1275 | for (uint32_t i = 0; i < counterSetRecordCount; i++) |
| 1276 | { |
| 1277 | // Get the counter set record offset |
| 1278 | uint32_t counterSetRecordOffset = ReadUint32(readBuffer, |
| 1279 | counterSetRecordsPointerTableOffset + i * uint32_t_size); |
| 1280 | |
| 1281 | // Collect the data for the counter set record |
| 1282 | uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, |
| 1283 | packetBodyPoolOffset + counterSetRecordOffset + 0 * uint32_t_size); |
| 1284 | uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, |
| 1285 | packetBodyPoolOffset + counterSetRecordOffset + 1 * uint32_t_size); |
| 1286 | CounterSetRecord counterSetRecord; |
| 1287 | counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid |
| 1288 | counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count |
| 1289 | counterSetRecord.name_offset = counterSetRecordWord1; // name_offset |
| 1290 | |
| 1291 | uint32_t counterSetRecordPoolOffset = packetBodyPoolOffset + // Packet body offset |
| 1292 | counterSetRecordOffset + // Counter set record offset |
| 1293 | 2 * uint32_t_size + // Counter set record header |
| 1294 | counterSetRecord.name_offset; // Counter set name offset |
| 1295 | uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset); |
| 1296 | counterSetRecord.name_length = counterSetRecordNameLength; // name_length |
| 1297 | unsigned char counterSetRecordNameNullTerminator = // name null-terminator |
| 1298 | ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1); |
| 1299 | BOOST_CHECK(counterSetRecordNameNullTerminator == '\0'); |
| 1300 | std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1); |
| 1301 | std::memcpy(counterSetRecordNameBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1302 | readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size()); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1303 | counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name |
| 1304 | |
| 1305 | counterSetRecords.push_back(counterSetRecord); |
| 1306 | } |
| 1307 | |
| 1308 | // Check that the counter set records are correct |
| 1309 | BOOST_CHECK(counterSetRecords.size() == 1); |
| 1310 | for (const CounterSetRecord& counterSetRecord : counterSetRecords) |
| 1311 | { |
| 1312 | const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid); |
| 1313 | BOOST_CHECK(counterSet); |
| 1314 | BOOST_CHECK(counterSet->m_Uid == counterSetRecord.uid); |
| 1315 | BOOST_CHECK(counterSet->m_Count == counterSetRecord.count); |
| 1316 | BOOST_CHECK(counterSet->m_Name == counterSetRecord.name); |
| 1317 | } |
| 1318 | |
| 1319 | // Event record structure/collection used for testing |
| 1320 | struct EventRecord |
| 1321 | { |
| 1322 | uint16_t counter_uid; |
| 1323 | uint16_t max_counter_uid; |
| 1324 | uint16_t device; |
| 1325 | uint16_t counter_set; |
| 1326 | uint16_t counter_class; |
| 1327 | uint16_t interpolation; |
| 1328 | double multiplier; |
| 1329 | uint32_t name_offset; |
| 1330 | uint32_t name_length; |
| 1331 | std::string name; |
| 1332 | uint32_t description_offset; |
| 1333 | uint32_t description_length; |
| 1334 | std::string description; |
| 1335 | uint32_t units_offset; |
| 1336 | uint32_t units_length; |
| 1337 | std::string units; |
| 1338 | }; |
| 1339 | // Category record structure/collection used for testing |
| 1340 | struct CategoryRecord |
| 1341 | { |
| 1342 | uint16_t device; |
| 1343 | uint16_t counter_set; |
| 1344 | uint16_t event_count; |
| 1345 | uint32_t event_pointer_table_offset; |
| 1346 | uint32_t name_offset; |
| 1347 | uint32_t name_length; |
| 1348 | std::string name; |
| 1349 | std::vector<uint32_t> event_pointer_table; |
| 1350 | std::vector<EventRecord> event_records; |
| 1351 | }; |
| 1352 | std::vector<CategoryRecord> categoryRecords; |
| 1353 | uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header |
| 1354 | 6u * uint32_t_size + // body_header |
| 1355 | bodyHeaderWord5; // categories_pointer_table_offset |
| 1356 | for (uint32_t i = 0; i < categoryRecordCount; i++) |
| 1357 | { |
| 1358 | // Get the category record offset |
| 1359 | uint32_t categoryRecordOffset = ReadUint32(readBuffer, categoryRecordsPointerTableOffset + i * uint32_t_size); |
| 1360 | |
| 1361 | // Collect the data for the category record |
| 1362 | uint32_t categoryRecordWord0 = ReadUint32(readBuffer, |
| 1363 | packetBodyPoolOffset + categoryRecordOffset + 0 * uint32_t_size); |
| 1364 | uint32_t categoryRecordWord1 = ReadUint32(readBuffer, |
| 1365 | packetBodyPoolOffset + categoryRecordOffset + 1 * uint32_t_size); |
| 1366 | uint32_t categoryRecordWord2 = ReadUint32(readBuffer, |
| 1367 | packetBodyPoolOffset + categoryRecordOffset + 2 * uint32_t_size); |
| 1368 | uint32_t categoryRecordWord3 = ReadUint32(readBuffer, |
| 1369 | packetBodyPoolOffset + categoryRecordOffset + 3 * uint32_t_size); |
| 1370 | CategoryRecord categoryRecord; |
| 1371 | categoryRecord.device = static_cast<uint16_t>(categoryRecordWord0 >> 16); // device |
| 1372 | categoryRecord.counter_set = static_cast<uint16_t>(categoryRecordWord0); // counter_set |
| 1373 | categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count |
| 1374 | categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset |
| 1375 | categoryRecord.name_offset = categoryRecordWord3; // name_offset |
| 1376 | |
| 1377 | uint32_t categoryRecordPoolOffset = packetBodyPoolOffset + // Packet body offset |
| 1378 | categoryRecordOffset + // Category record offset |
| 1379 | 4 * uint32_t_size; // Category record header |
| 1380 | |
| 1381 | uint32_t categoryRecordNameLength = ReadUint32(readBuffer, |
| 1382 | categoryRecordPoolOffset + categoryRecord.name_offset); |
| 1383 | categoryRecord.name_length = categoryRecordNameLength; // name_length |
| 1384 | unsigned char categoryRecordNameNullTerminator = |
| 1385 | ReadUint8(readBuffer, |
| 1386 | categoryRecordPoolOffset + |
| 1387 | categoryRecord.name_offset + |
| 1388 | uint32_t_size + |
| 1389 | categoryRecordNameLength - 1); // name null-terminator |
| 1390 | BOOST_CHECK(categoryRecordNameNullTerminator == '\0'); |
| 1391 | std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1); |
| 1392 | std::memcpy(categoryRecordNameBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1393 | readData + |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1394 | categoryRecordPoolOffset + |
| 1395 | categoryRecord.name_offset + |
| 1396 | uint32_t_size, |
| 1397 | categoryRecordNameBuffer.size()); |
| 1398 | categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name |
| 1399 | |
| 1400 | categoryRecord.event_pointer_table.resize(categoryRecord.event_count); |
| 1401 | for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++) |
| 1402 | { |
| 1403 | uint32_t eventRecordOffset = ReadUint32(readBuffer, |
| 1404 | categoryRecordPoolOffset + |
| 1405 | categoryRecord.event_pointer_table_offset + |
| 1406 | eventIndex * uint32_t_size); |
| 1407 | categoryRecord.event_pointer_table[eventIndex] = eventRecordOffset; |
| 1408 | |
| 1409 | // Collect the data for the event record |
| 1410 | uint32_t eventRecordWord0 = ReadUint32(readBuffer, |
| 1411 | categoryRecordPoolOffset + eventRecordOffset + 0 * uint32_t_size); |
| 1412 | uint32_t eventRecordWord1 = ReadUint32(readBuffer, |
| 1413 | categoryRecordPoolOffset + eventRecordOffset + 1 * uint32_t_size); |
| 1414 | uint32_t eventRecordWord2 = ReadUint32(readBuffer, |
| 1415 | categoryRecordPoolOffset + eventRecordOffset + 2 * uint32_t_size); |
| 1416 | uint64_t eventRecordWord34 = ReadUint64(readBuffer, |
| 1417 | categoryRecordPoolOffset + eventRecordOffset + 3 * uint32_t_size); |
| 1418 | uint32_t eventRecordWord5 = ReadUint32(readBuffer, |
| 1419 | categoryRecordPoolOffset + eventRecordOffset + 5 * uint32_t_size); |
| 1420 | uint32_t eventRecordWord6 = ReadUint32(readBuffer, |
| 1421 | categoryRecordPoolOffset + eventRecordOffset + 6 * uint32_t_size); |
| 1422 | uint32_t eventRecordWord7 = ReadUint32(readBuffer, |
| 1423 | categoryRecordPoolOffset + eventRecordOffset + 7 * uint32_t_size); |
| 1424 | EventRecord eventRecord; |
| 1425 | eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid |
| 1426 | eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid |
| 1427 | eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device |
| 1428 | eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set |
| 1429 | eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class |
| 1430 | eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation |
| 1431 | std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier |
| 1432 | eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset |
| 1433 | eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset |
| 1434 | eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset |
| 1435 | |
| 1436 | uint32_t eventRecordPoolOffset = categoryRecordPoolOffset + // Category record pool offset |
| 1437 | eventRecordOffset + // Event record offset |
| 1438 | 8 * uint32_t_size; // Event record header |
| 1439 | |
| 1440 | uint32_t eventRecordNameLength = ReadUint32(readBuffer, |
| 1441 | eventRecordPoolOffset + eventRecord.name_offset); |
| 1442 | eventRecord.name_length = eventRecordNameLength; // name_length |
| 1443 | unsigned char eventRecordNameNullTerminator = |
| 1444 | ReadUint8(readBuffer, |
| 1445 | eventRecordPoolOffset + |
| 1446 | eventRecord.name_offset + |
| 1447 | uint32_t_size + |
| 1448 | eventRecordNameLength - 1); // name null-terminator |
| 1449 | BOOST_CHECK(eventRecordNameNullTerminator == '\0'); |
| 1450 | std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1); |
| 1451 | std::memcpy(eventRecordNameBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1452 | readData + |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1453 | eventRecordPoolOffset + |
| 1454 | eventRecord.name_offset + |
| 1455 | uint32_t_size, |
| 1456 | eventRecordNameBuffer.size()); |
| 1457 | eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name |
| 1458 | |
| 1459 | uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer, |
| 1460 | eventRecordPoolOffset + eventRecord.description_offset); |
| 1461 | eventRecord.description_length = eventRecordDescriptionLength; // description_length |
| 1462 | unsigned char eventRecordDescriptionNullTerminator = |
| 1463 | ReadUint8(readBuffer, |
| 1464 | eventRecordPoolOffset + |
| 1465 | eventRecord.description_offset + |
| 1466 | uint32_t_size + |
| 1467 | eventRecordDescriptionLength - 1); // description null-terminator |
| 1468 | BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0'); |
| 1469 | std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1); |
| 1470 | std::memcpy(eventRecordDescriptionBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1471 | readData + |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1472 | eventRecordPoolOffset + |
| 1473 | eventRecord.description_offset + |
| 1474 | uint32_t_size, |
| 1475 | eventRecordDescriptionBuffer.size()); |
| 1476 | eventRecord.description.assign(eventRecordDescriptionBuffer.begin(), |
| 1477 | eventRecordDescriptionBuffer.end()); // description |
| 1478 | |
| 1479 | if (eventRecord.units_offset > 0) |
| 1480 | { |
| 1481 | uint32_t eventRecordUnitsLength = ReadUint32(readBuffer, |
| 1482 | eventRecordPoolOffset + eventRecord.units_offset); |
| 1483 | eventRecord.units_length = eventRecordUnitsLength; // units_length |
| 1484 | unsigned char eventRecordUnitsNullTerminator = |
| 1485 | ReadUint8(readBuffer, |
| 1486 | eventRecordPoolOffset + |
| 1487 | eventRecord.units_offset + |
| 1488 | uint32_t_size + |
| 1489 | eventRecordUnitsLength - 1); // units null-terminator |
| 1490 | BOOST_CHECK(eventRecordUnitsNullTerminator == '\0'); |
| 1491 | std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1); |
| 1492 | std::memcpy(eventRecordUnitsBuffer.data(), |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1493 | readData + |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1494 | eventRecordPoolOffset + |
| 1495 | eventRecord.units_offset + |
| 1496 | uint32_t_size, |
| 1497 | eventRecordUnitsBuffer.size()); |
| 1498 | eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units |
| 1499 | } |
| 1500 | |
| 1501 | categoryRecord.event_records.push_back(eventRecord); |
| 1502 | } |
| 1503 | |
| 1504 | categoryRecords.push_back(categoryRecord); |
| 1505 | } |
| 1506 | |
| 1507 | // Check that the category records are correct |
| 1508 | BOOST_CHECK(categoryRecords.size() == 2); |
| 1509 | for (const CategoryRecord& categoryRecord : categoryRecords) |
| 1510 | { |
| 1511 | const Category* category = counterDirectory.GetCategory(categoryRecord.name); |
| 1512 | BOOST_CHECK(category); |
| 1513 | BOOST_CHECK(category->m_Name == categoryRecord.name); |
| 1514 | BOOST_CHECK(category->m_DeviceUid == categoryRecord.device); |
| 1515 | BOOST_CHECK(category->m_CounterSetUid == categoryRecord.counter_set); |
| 1516 | BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count); |
| 1517 | |
| 1518 | // Check that the event records are correct |
| 1519 | for (const EventRecord& eventRecord : categoryRecord.event_records) |
| 1520 | { |
| 1521 | const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid); |
| 1522 | BOOST_CHECK(counter); |
| 1523 | BOOST_CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid); |
| 1524 | BOOST_CHECK(counter->m_DeviceUid == eventRecord.device); |
| 1525 | BOOST_CHECK(counter->m_CounterSetUid == eventRecord.counter_set); |
| 1526 | BOOST_CHECK(counter->m_Class == eventRecord.counter_class); |
| 1527 | BOOST_CHECK(counter->m_Interpolation == eventRecord.interpolation); |
| 1528 | BOOST_CHECK(counter->m_Multiplier == eventRecord.multiplier); |
| 1529 | BOOST_CHECK(counter->m_Name == eventRecord.name); |
| 1530 | BOOST_CHECK(counter->m_Description == eventRecord.description); |
| 1531 | BOOST_CHECK(counter->m_Units == eventRecord.units); |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest3) |
| 1537 | { |
| 1538 | // Using a mock counter directory that allows to register invalid objects |
| 1539 | MockCounterDirectory counterDirectory; |
| 1540 | |
| 1541 | // Register an invalid device |
| 1542 | const std::string deviceName = "inv@lid dev!c€"; |
| 1543 | const Device* device = nullptr; |
| 1544 | BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3)); |
| 1545 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 1); |
| 1546 | BOOST_CHECK(device); |
| 1547 | |
| 1548 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1549 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1550 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1551 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException); |
| 1552 | } |
| 1553 | |
| 1554 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest4) |
| 1555 | { |
| 1556 | // Using a mock counter directory that allows to register invalid objects |
| 1557 | MockCounterDirectory counterDirectory; |
| 1558 | |
| 1559 | // Register an invalid counter set |
| 1560 | const std::string counterSetName = "inv@lid count€rs€t"; |
| 1561 | const CounterSet* counterSet = nullptr; |
| 1562 | BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName)); |
| 1563 | BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1); |
| 1564 | BOOST_CHECK(counterSet); |
| 1565 | |
| 1566 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1567 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1568 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1569 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException); |
| 1570 | } |
| 1571 | |
| 1572 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest5) |
| 1573 | { |
| 1574 | // Using a mock counter directory that allows to register invalid objects |
| 1575 | MockCounterDirectory counterDirectory; |
| 1576 | |
| 1577 | // Register an invalid category |
| 1578 | const std::string categoryName = "c@t€gory"; |
| 1579 | const Category* category = nullptr; |
| 1580 | BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName)); |
| 1581 | BOOST_CHECK(counterDirectory.GetCategoryCount() == 1); |
| 1582 | BOOST_CHECK(category); |
| 1583 | |
| 1584 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1585 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1586 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1587 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException); |
| 1588 | } |
| 1589 | |
| 1590 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest6) |
| 1591 | { |
| 1592 | // Using a mock counter directory that allows to register invalid objects |
| 1593 | MockCounterDirectory counterDirectory; |
| 1594 | |
| 1595 | // Register an invalid device |
| 1596 | const std::string deviceName = "inv@lid dev!c€"; |
| 1597 | const Device* device = nullptr; |
| 1598 | BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3)); |
| 1599 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 1); |
| 1600 | BOOST_CHECK(device); |
| 1601 | |
| 1602 | // Register an invalid counter set |
| 1603 | const std::string counterSetName = "inv@lid count€rs€t"; |
| 1604 | const CounterSet* counterSet = nullptr; |
| 1605 | BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName)); |
| 1606 | BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1); |
| 1607 | BOOST_CHECK(counterSet); |
| 1608 | |
| 1609 | // Register an invalid category associated to an invalid device and an invalid counter set |
| 1610 | const std::string categoryName = "c@t€gory"; |
| 1611 | const Category* category = nullptr; |
| 1612 | BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName, |
| 1613 | device->m_Uid, |
| 1614 | counterSet->m_Uid)); |
| 1615 | BOOST_CHECK(counterDirectory.GetCategoryCount() == 1); |
| 1616 | BOOST_CHECK(category); |
| 1617 | |
| 1618 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1619 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1620 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1621 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException); |
| 1622 | } |
| 1623 | |
| 1624 | BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest7) |
| 1625 | { |
| 1626 | // Using a mock counter directory that allows to register invalid objects |
| 1627 | MockCounterDirectory counterDirectory; |
| 1628 | |
| 1629 | // Register an valid device |
| 1630 | const std::string deviceName = "valid device"; |
| 1631 | const Device* device = nullptr; |
| 1632 | BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3)); |
| 1633 | BOOST_CHECK(counterDirectory.GetDeviceCount() == 1); |
| 1634 | BOOST_CHECK(device); |
| 1635 | |
| 1636 | // Register an valid counter set |
| 1637 | const std::string counterSetName = "valid counterset"; |
| 1638 | const CounterSet* counterSet = nullptr; |
| 1639 | BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName)); |
| 1640 | BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1); |
| 1641 | BOOST_CHECK(counterSet); |
| 1642 | |
| 1643 | // Register an valid category associated to a valid device and a valid counter set |
| 1644 | const std::string categoryName = "category"; |
| 1645 | const Category* category = nullptr; |
| 1646 | BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName, |
| 1647 | device->m_Uid, |
| 1648 | counterSet->m_Uid)); |
| 1649 | BOOST_CHECK(counterDirectory.GetCategoryCount() == 1); |
| 1650 | BOOST_CHECK(category); |
| 1651 | |
| 1652 | // Register an invalid counter associated to a valid category |
| 1653 | const Counter* counter = nullptr; |
| 1654 | BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(categoryName, |
| 1655 | 0, |
| 1656 | 1, |
| 1657 | 123.45f, |
| 1658 | "counter", |
| 1659 | "counter description", |
| 1660 | std::string("invalid counter units"), |
| 1661 | 5, |
| 1662 | device->m_Uid, |
| 1663 | counterSet->m_Uid)); |
| 1664 | BOOST_CHECK(counterDirectory.GetCounterCount() == 5); |
| 1665 | BOOST_CHECK(counter); |
| 1666 | |
| 1667 | // Buffer with enough space |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1668 | MockBufferManager mockBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1669 | SendCounterPacket sendCounterPacket(mockBuffer); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1670 | BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException); |
| 1671 | } |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 1672 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1673 | BOOST_AUTO_TEST_CASE(SendThreadTest0) |
| 1674 | { |
| 1675 | MockProfilingConnection mockProfilingConnection; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1676 | MockStreamCounterBuffer mockStreamCounterBuffer(0); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1677 | SendCounterPacket sendCounterPacket(mockStreamCounterBuffer); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1678 | |
| 1679 | // Try to start the send thread many times, it must only start once |
| 1680 | |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1681 | sendCounterPacket.Start(mockProfilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1682 | BOOST_CHECK(sendCounterPacket.IsRunning()); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1683 | sendCounterPacket.Start(mockProfilingConnection); |
| 1684 | sendCounterPacket.Start(mockProfilingConnection); |
| 1685 | sendCounterPacket.Start(mockProfilingConnection); |
| 1686 | sendCounterPacket.Start(mockProfilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1687 | BOOST_CHECK(sendCounterPacket.IsRunning()); |
| 1688 | |
| 1689 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 1690 | |
| 1691 | sendCounterPacket.Stop(); |
| 1692 | BOOST_CHECK(!sendCounterPacket.IsRunning()); |
| 1693 | } |
| 1694 | |
| 1695 | BOOST_AUTO_TEST_CASE(SendThreadTest1) |
| 1696 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1697 | unsigned int totalWrittenSize = 0; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1698 | |
| 1699 | MockProfilingConnection mockProfilingConnection; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1700 | MockStreamCounterBuffer mockStreamCounterBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1701 | SendCounterPacket sendCounterPacket(mockStreamCounterBuffer); |
| 1702 | sendCounterPacket.Start(mockProfilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1703 | |
| 1704 | // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for |
| 1705 | // something to become available for reading |
| 1706 | |
| 1707 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 1708 | |
| 1709 | CounterDirectory counterDirectory; |
| 1710 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 1711 | |
| 1712 | // Get the size of the Stream Metadata Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1713 | std::string processName = GetProcessName().substr(0, 60); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1714 | unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1715 | unsigned int streamMetadataPacketsize = 118 + processNameSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1716 | totalWrittenSize += streamMetadataPacketsize; |
| 1717 | |
| 1718 | sendCounterPacket.SetReadyToRead(); |
| 1719 | |
| 1720 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1721 | |
| 1722 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
| 1723 | |
| 1724 | // Get the size of the Counter Directory Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1725 | unsigned int counterDirectoryPacketSize = 32; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1726 | totalWrittenSize += counterDirectoryPacketSize; |
| 1727 | |
| 1728 | sendCounterPacket.SetReadyToRead(); |
| 1729 | |
| 1730 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1731 | |
| 1732 | sendCounterPacket.SendPeriodicCounterCapturePacket(123u, |
| 1733 | { |
| 1734 | { 1u, 23u }, |
| 1735 | { 33u, 1207623u } |
| 1736 | }); |
| 1737 | |
| 1738 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1739 | unsigned int periodicCounterCapturePacketSize = 28; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1740 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1741 | |
| 1742 | sendCounterPacket.SetReadyToRead(); |
| 1743 | |
| 1744 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1745 | |
| 1746 | sendCounterPacket.SendPeriodicCounterCapturePacket(44u, |
| 1747 | { |
| 1748 | { 211u, 923u } |
| 1749 | }); |
| 1750 | |
| 1751 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1752 | periodicCounterCapturePacketSize = 22; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1753 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1754 | |
| 1755 | sendCounterPacket.SendPeriodicCounterCapturePacket(1234u, |
| 1756 | { |
| 1757 | { 555u, 23u }, |
| 1758 | { 556u, 6u }, |
| 1759 | { 557u, 893454u }, |
| 1760 | { 558u, 1456623u }, |
| 1761 | { 559u, 571090u } |
| 1762 | }); |
| 1763 | |
| 1764 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1765 | periodicCounterCapturePacketSize = 46; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1766 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1767 | |
| 1768 | sendCounterPacket.SendPeriodicCounterCapturePacket(997u, |
| 1769 | { |
| 1770 | { 88u, 11u }, |
| 1771 | { 96u, 22u }, |
| 1772 | { 97u, 33u }, |
| 1773 | { 999u, 444u } |
| 1774 | }); |
| 1775 | |
| 1776 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1777 | periodicCounterCapturePacketSize = 40; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1778 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1779 | |
| 1780 | sendCounterPacket.SetReadyToRead(); |
| 1781 | |
| 1782 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1783 | |
| 1784 | sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u }); |
| 1785 | |
| 1786 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1787 | periodicCounterCapturePacketSize = 30; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1788 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1789 | |
| 1790 | sendCounterPacket.SetReadyToRead(); |
| 1791 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1792 | // To test an exact value of the "read size" in the mock buffer, wait two seconds to allow the send thread to |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1793 | // read all what's remaining in the buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1794 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1795 | |
| 1796 | sendCounterPacket.Stop(); |
| 1797 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1798 | BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1799 | BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize); |
| 1800 | BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | BOOST_AUTO_TEST_CASE(SendThreadTest2) |
| 1804 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1805 | unsigned int totalWrittenSize = 0; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1806 | |
| 1807 | MockProfilingConnection mockProfilingConnection; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1808 | MockStreamCounterBuffer mockStreamCounterBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1809 | SendCounterPacket sendCounterPacket(mockStreamCounterBuffer); |
| 1810 | sendCounterPacket.Start(mockProfilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1811 | |
| 1812 | // Adding many spurious "ready to read" signals throughout the test to check that the send thread is |
| 1813 | // capable of handling unnecessary read requests |
| 1814 | |
| 1815 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 1816 | |
| 1817 | sendCounterPacket.SetReadyToRead(); |
| 1818 | |
| 1819 | CounterDirectory counterDirectory; |
| 1820 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 1821 | |
| 1822 | // Get the size of the Stream Metadata Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1823 | std::string processName = GetProcessName().substr(0, 60); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1824 | unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1825 | unsigned int streamMetadataPacketsize = 118 + processNameSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1826 | totalWrittenSize += streamMetadataPacketsize; |
| 1827 | |
| 1828 | sendCounterPacket.SetReadyToRead(); |
| 1829 | |
| 1830 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1831 | |
| 1832 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
| 1833 | |
| 1834 | // Get the size of the Counter Directory Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1835 | unsigned int counterDirectoryPacketSize = 32; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1836 | totalWrittenSize += counterDirectoryPacketSize; |
| 1837 | |
| 1838 | sendCounterPacket.SetReadyToRead(); |
| 1839 | sendCounterPacket.SetReadyToRead(); |
| 1840 | |
| 1841 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1842 | |
| 1843 | sendCounterPacket.SendPeriodicCounterCapturePacket(123u, |
| 1844 | { |
| 1845 | { 1u, 23u }, |
| 1846 | { 33u, 1207623u } |
| 1847 | }); |
| 1848 | |
| 1849 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1850 | unsigned int periodicCounterCapturePacketSize = 28; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1851 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1852 | |
| 1853 | sendCounterPacket.SetReadyToRead(); |
| 1854 | |
| 1855 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1856 | |
| 1857 | sendCounterPacket.SetReadyToRead(); |
| 1858 | sendCounterPacket.SetReadyToRead(); |
| 1859 | sendCounterPacket.SetReadyToRead(); |
| 1860 | |
| 1861 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1862 | |
| 1863 | sendCounterPacket.SetReadyToRead(); |
| 1864 | sendCounterPacket.SendPeriodicCounterCapturePacket(44u, |
| 1865 | { |
| 1866 | { 211u, 923u } |
| 1867 | }); |
| 1868 | |
| 1869 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1870 | periodicCounterCapturePacketSize = 22; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1871 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1872 | |
| 1873 | sendCounterPacket.SendPeriodicCounterCapturePacket(1234u, |
| 1874 | { |
| 1875 | { 555u, 23u }, |
| 1876 | { 556u, 6u }, |
| 1877 | { 557u, 893454u }, |
| 1878 | { 558u, 1456623u }, |
| 1879 | { 559u, 571090u } |
| 1880 | }); |
| 1881 | |
| 1882 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1883 | periodicCounterCapturePacketSize = 46; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1884 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1885 | |
| 1886 | sendCounterPacket.SetReadyToRead(); |
| 1887 | sendCounterPacket.SendPeriodicCounterCapturePacket(997u, |
| 1888 | { |
| 1889 | { 88u, 11u }, |
| 1890 | { 96u, 22u }, |
| 1891 | { 97u, 33u }, |
| 1892 | { 999u, 444u } |
| 1893 | }); |
| 1894 | |
| 1895 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1896 | periodicCounterCapturePacketSize = 40; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1897 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1898 | |
| 1899 | sendCounterPacket.SetReadyToRead(); |
| 1900 | sendCounterPacket.SetReadyToRead(); |
| 1901 | |
| 1902 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1903 | |
| 1904 | sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u }); |
| 1905 | |
| 1906 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1907 | periodicCounterCapturePacketSize = 30; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1908 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1909 | |
| 1910 | sendCounterPacket.SetReadyToRead(); |
| 1911 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1912 | // To test an exact value of the "read size" in the mock buffer, wait two seconds to allow the send thread to |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1913 | // read all what's remaining in the buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1914 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1915 | |
| 1916 | sendCounterPacket.Stop(); |
| 1917 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1918 | BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1919 | BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize); |
| 1920 | BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | BOOST_AUTO_TEST_CASE(SendThreadTest3) |
| 1924 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1925 | unsigned int totalWrittenSize = 0; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1926 | |
| 1927 | MockProfilingConnection mockProfilingConnection; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1928 | MockStreamCounterBuffer mockStreamCounterBuffer(1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 1929 | SendCounterPacket sendCounterPacket(mockStreamCounterBuffer); |
| 1930 | sendCounterPacket.Start(mockProfilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1931 | |
| 1932 | // Not using pauses or "grace periods" to stress test the send thread |
| 1933 | |
| 1934 | sendCounterPacket.SetReadyToRead(); |
| 1935 | |
| 1936 | CounterDirectory counterDirectory; |
| 1937 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 1938 | |
| 1939 | // Get the size of the Stream Metadata Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1940 | std::string processName = GetProcessName().substr(0, 60); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 1941 | unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1942 | unsigned int streamMetadataPacketsize = 118 + processNameSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1943 | totalWrittenSize += streamMetadataPacketsize; |
| 1944 | |
| 1945 | sendCounterPacket.SetReadyToRead(); |
| 1946 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
| 1947 | |
| 1948 | // Get the size of the Counter Directory Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1949 | unsigned int counterDirectoryPacketSize =32; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1950 | totalWrittenSize += counterDirectoryPacketSize; |
| 1951 | |
| 1952 | sendCounterPacket.SetReadyToRead(); |
| 1953 | sendCounterPacket.SetReadyToRead(); |
| 1954 | sendCounterPacket.SendPeriodicCounterCapturePacket(123u, |
| 1955 | { |
| 1956 | { 1u, 23u }, |
| 1957 | { 33u, 1207623u } |
| 1958 | }); |
| 1959 | |
| 1960 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1961 | unsigned int periodicCounterCapturePacketSize = 28; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1962 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1963 | |
| 1964 | sendCounterPacket.SetReadyToRead(); |
| 1965 | sendCounterPacket.SetReadyToRead(); |
| 1966 | sendCounterPacket.SetReadyToRead(); |
| 1967 | sendCounterPacket.SetReadyToRead(); |
| 1968 | sendCounterPacket.SetReadyToRead(); |
| 1969 | sendCounterPacket.SendPeriodicCounterCapturePacket(44u, |
| 1970 | { |
| 1971 | { 211u, 923u } |
| 1972 | }); |
| 1973 | |
| 1974 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1975 | periodicCounterCapturePacketSize = 22; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1976 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1977 | |
| 1978 | sendCounterPacket.SendPeriodicCounterCapturePacket(1234u, |
| 1979 | { |
| 1980 | { 555u, 23u }, |
| 1981 | { 556u, 6u }, |
| 1982 | { 557u, 893454u }, |
| 1983 | { 558u, 1456623u }, |
| 1984 | { 559u, 571090u } |
| 1985 | }); |
| 1986 | |
| 1987 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 1988 | periodicCounterCapturePacketSize = 46; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 1989 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 1990 | |
| 1991 | sendCounterPacket.SetReadyToRead(); |
| 1992 | sendCounterPacket.SetReadyToRead(); |
| 1993 | sendCounterPacket.SendPeriodicCounterCapturePacket(997u, |
| 1994 | { |
| 1995 | { 88u, 11u }, |
| 1996 | { 96u, 22u }, |
| 1997 | { 97u, 33u }, |
| 1998 | { 999u, 444u } |
| 1999 | }); |
| 2000 | |
| 2001 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 2002 | periodicCounterCapturePacketSize = 40; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 2003 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 2004 | |
| 2005 | sendCounterPacket.SetReadyToRead(); |
| 2006 | sendCounterPacket.SetReadyToRead(); |
| 2007 | sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u }); |
| 2008 | |
| 2009 | // Get the size of the Periodic Counter Capture Packet |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 2010 | periodicCounterCapturePacketSize = 30; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 2011 | totalWrittenSize += periodicCounterCapturePacketSize; |
| 2012 | |
| 2013 | sendCounterPacket.SetReadyToRead(); |
| 2014 | |
| 2015 | // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send |
| 2016 | // thread is not guaranteed to flush the buffer) |
| 2017 | sendCounterPacket.Stop(); |
| 2018 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 2019 | BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize); |
| 2020 | BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize); |
| 2021 | BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize); |
| 2022 | BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize()); |
| 2023 | BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize()); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 2026 | BOOST_AUTO_TEST_CASE(SendThreadBufferTest) |
| 2027 | { |
| 2028 | MockProfilingConnection mockProfilingConnection; |
| 2029 | BufferManager bufferManager(1, 1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 2030 | SendCounterPacket sendCounterPacket(bufferManager, -1); |
| 2031 | sendCounterPacket.Start(mockProfilingConnection); |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 2032 | |
| 2033 | // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for |
| 2034 | // something to become available for reading |
| 2035 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 2036 | |
| 2037 | // SendStreamMetaDataPacket |
| 2038 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 2039 | |
| 2040 | // Read data from the buffer |
| 2041 | // Buffer should become readable after commit by SendStreamMetaDataPacket |
| 2042 | auto packetBuffer = bufferManager.GetReadableBuffer(); |
| 2043 | BOOST_TEST(packetBuffer.get()); |
| 2044 | |
| 2045 | std::string processName = GetProcessName().substr(0, 60); |
| 2046 | unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; |
| 2047 | unsigned int streamMetadataPacketsize = 118 + processNameSize; |
| 2048 | BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize); |
| 2049 | |
| 2050 | // Buffer is not available when SendStreamMetaDataPacket already occupied the buffer. |
| 2051 | unsigned int reservedSize = 0; |
| 2052 | auto reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2053 | BOOST_TEST(!reservedBuffer.get()); |
| 2054 | |
| 2055 | // Recommit to be read by sendCounterPacket |
| 2056 | bufferManager.Commit(packetBuffer, streamMetadataPacketsize); |
| 2057 | |
| 2058 | sendCounterPacket.SetReadyToRead(); |
| 2059 | |
| 2060 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 2061 | |
| 2062 | // The buffer is read by the send thread so it should not be in the readable buffer. |
| 2063 | auto readBuffer = bufferManager.GetReadableBuffer(); |
| 2064 | BOOST_TEST(!readBuffer); |
| 2065 | |
| 2066 | // Successfully reserved the buffer with requested size |
| 2067 | reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2068 | BOOST_TEST(reservedSize == 512); |
| 2069 | BOOST_TEST(reservedBuffer.get()); |
| 2070 | |
| 2071 | // Release the buffer to be used by sendCounterPacket |
| 2072 | bufferManager.Release(reservedBuffer); |
| 2073 | |
| 2074 | // SendCounterDirectoryPacket |
| 2075 | CounterDirectory counterDirectory; |
| 2076 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
| 2077 | |
| 2078 | // Read data from the buffer |
| 2079 | // Buffer should become readable after commit by SendCounterDirectoryPacket |
| 2080 | auto counterDirectoryPacketBuffer = bufferManager.GetReadableBuffer(); |
| 2081 | BOOST_TEST(counterDirectoryPacketBuffer.get()); |
| 2082 | |
| 2083 | // Get the size of the Counter Directory Packet |
| 2084 | unsigned int counterDirectoryPacketSize = 32; |
| 2085 | BOOST_TEST(counterDirectoryPacketBuffer->GetSize() == counterDirectoryPacketSize); |
| 2086 | |
| 2087 | // Buffer is not available when SendCounterDirectoryPacket already occupied the buffer. |
| 2088 | reservedSize = 0; |
| 2089 | reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2090 | BOOST_TEST(reservedSize == 0); |
| 2091 | BOOST_TEST(!reservedBuffer.get()); |
| 2092 | |
| 2093 | // Recommit to be read by sendCounterPacket |
| 2094 | bufferManager.Commit(counterDirectoryPacketBuffer, counterDirectoryPacketSize); |
| 2095 | |
| 2096 | sendCounterPacket.SetReadyToRead(); |
| 2097 | |
| 2098 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 2099 | |
| 2100 | // The buffer is read by the send thread so it should not be in the readable buffer. |
| 2101 | readBuffer = bufferManager.GetReadableBuffer(); |
| 2102 | BOOST_TEST(!readBuffer); |
| 2103 | |
| 2104 | // Successfully reserved the buffer with requested size |
| 2105 | reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2106 | BOOST_TEST(reservedSize == 512); |
| 2107 | BOOST_TEST(reservedBuffer.get()); |
| 2108 | |
| 2109 | // Release the buffer to be used by sendCounterPacket |
| 2110 | bufferManager.Release(reservedBuffer); |
| 2111 | |
| 2112 | // SendPeriodicCounterCapturePacket |
| 2113 | sendCounterPacket.SendPeriodicCounterCapturePacket(123u, |
| 2114 | { |
| 2115 | { 1u, 23u }, |
| 2116 | { 33u, 1207623u } |
| 2117 | }); |
| 2118 | |
| 2119 | // Read data from the buffer |
| 2120 | // Buffer should become readable after commit by SendPeriodicCounterCapturePacket |
| 2121 | auto periodicCounterCapturePacketBuffer = bufferManager.GetReadableBuffer(); |
| 2122 | BOOST_TEST(periodicCounterCapturePacketBuffer.get()); |
| 2123 | |
| 2124 | // Get the size of the Periodic Counter Capture Packet |
| 2125 | unsigned int periodicCounterCapturePacketSize = 28; |
| 2126 | BOOST_TEST(periodicCounterCapturePacketBuffer->GetSize() == periodicCounterCapturePacketSize); |
| 2127 | |
| 2128 | // Buffer is not available when SendPeriodicCounterCapturePacket already occupied the buffer. |
| 2129 | reservedSize = 0; |
| 2130 | reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2131 | BOOST_TEST(reservedSize == 0); |
| 2132 | BOOST_TEST(!reservedBuffer.get()); |
| 2133 | |
| 2134 | // Recommit to be read by sendCounterPacket |
| 2135 | bufferManager.Commit(periodicCounterCapturePacketBuffer, periodicCounterCapturePacketSize); |
| 2136 | |
| 2137 | sendCounterPacket.SetReadyToRead(); |
| 2138 | |
| 2139 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 2140 | |
| 2141 | // The buffer is read by the send thread so it should not be in the readable buffer. |
| 2142 | readBuffer = bufferManager.GetReadableBuffer(); |
| 2143 | BOOST_TEST(!readBuffer); |
| 2144 | |
| 2145 | // Successfully reserved the buffer with requested size |
| 2146 | reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2147 | BOOST_TEST(reservedSize == 512); |
| 2148 | BOOST_TEST(reservedBuffer.get()); |
| 2149 | |
| 2150 | sendCounterPacket.Stop(); |
| 2151 | } |
| 2152 | |
| 2153 | BOOST_AUTO_TEST_CASE(SendThreadBufferTest1) |
| 2154 | { |
| 2155 | MockWriteProfilingConnection mockProfilingConnection; |
| 2156 | BufferManager bufferManager(3, 1024); |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame^] | 2157 | SendCounterPacket sendCounterPacket(bufferManager, -1); |
| 2158 | sendCounterPacket.Start(mockProfilingConnection); |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 2159 | |
| 2160 | // SendStreamMetaDataPacket |
| 2161 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 2162 | |
| 2163 | // Read data from the buffer |
| 2164 | // Buffer should become readable after commit by SendStreamMetaDataPacket |
| 2165 | auto packetBuffer = bufferManager.GetReadableBuffer(); |
| 2166 | BOOST_TEST(packetBuffer.get()); |
| 2167 | |
| 2168 | std::string processName = GetProcessName().substr(0, 60); |
| 2169 | unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; |
| 2170 | unsigned int streamMetadataPacketsize = 118 + processNameSize; |
| 2171 | BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize); |
| 2172 | |
| 2173 | // Recommit to be read by sendCounterPacket |
| 2174 | bufferManager.Commit(packetBuffer, streamMetadataPacketsize); |
| 2175 | |
| 2176 | sendCounterPacket.SetReadyToRead(); |
| 2177 | |
| 2178 | // SendCounterDirectoryPacket |
| 2179 | CounterDirectory counterDirectory; |
| 2180 | sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); |
| 2181 | |
| 2182 | sendCounterPacket.SetReadyToRead(); |
| 2183 | |
| 2184 | // SendPeriodicCounterCapturePacket |
| 2185 | sendCounterPacket.SendPeriodicCounterCapturePacket(123u, |
| 2186 | { |
| 2187 | { 1u, 23u }, |
| 2188 | { 33u, 1207623u } |
| 2189 | }); |
| 2190 | |
| 2191 | sendCounterPacket.SetReadyToRead(); |
| 2192 | |
| 2193 | sendCounterPacket.Stop(); |
| 2194 | |
| 2195 | // The buffer is read by the send thread so it should not be in the readable buffer. |
| 2196 | auto readBuffer = bufferManager.GetReadableBuffer(); |
| 2197 | BOOST_TEST(!readBuffer); |
| 2198 | |
| 2199 | // Successfully reserved the buffer with requested size |
| 2200 | unsigned int reservedSize = 0; |
| 2201 | auto reservedBuffer = bufferManager.Reserve(512, reservedSize); |
| 2202 | BOOST_TEST(reservedSize == 512); |
| 2203 | BOOST_TEST(reservedBuffer.get()); |
| 2204 | |
| 2205 | // Check that data was actually written to the profiling connection in any order |
| 2206 | std::vector<uint32_t> writtenData = mockProfilingConnection.GetWrittenData(); |
| 2207 | std::vector<uint32_t> expectedOutput{streamMetadataPacketsize, 32, 28}; |
| 2208 | BOOST_TEST(writtenData.size() == 3); |
| 2209 | bool foundStreamMetaDataPacket = |
| 2210 | std::find(writtenData.begin(), writtenData.end(), streamMetadataPacketsize) != writtenData.end(); |
| 2211 | bool foundCounterDirectoryPacket = std::find(writtenData.begin(), writtenData.end(), 32) != writtenData.end(); |
| 2212 | bool foundPeriodicCounterCapturePacket = std::find(writtenData.begin(), writtenData.end(), 28) != writtenData.end(); |
| 2213 | BOOST_TEST(foundStreamMetaDataPacket); |
| 2214 | BOOST_TEST(foundCounterDirectoryPacket); |
| 2215 | BOOST_TEST(foundPeriodicCounterCapturePacket); |
| 2216 | } |
| 2217 | |
Ferran Balaguer | af5c46b | 2019-08-30 15:49:15 +0100 | [diff] [blame] | 2218 | BOOST_AUTO_TEST_SUITE_END() |