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