using namespace armnn::profiling;
-size_t GetDataLength(const MockStreamCounterBuffer& mockStreamCounterBuffer, size_t packetOffset)
-{
- // The data length is the written in the second byte
- return ReadUint32(mockStreamCounterBuffer.GetBuffer(),
- boost::numeric_cast<unsigned int>(packetOffset + sizeof(uint32_t)));
-}
-
-size_t GetPacketSize(const MockStreamCounterBuffer& mockStreamCounterBuffer, size_t packetOffset)
-{
- // The packet size is the data length plus the size of the packet header (always two words big)
- return GetDataLength(mockStreamCounterBuffer, packetOffset) + 2 * sizeof(uint32_t);
-}
-
BOOST_AUTO_TEST_SUITE(SendCounterPacketTests)
BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest)
{
- unsigned int size = 0;
-
- MockBuffer mockBuffer(512);
+ MockBufferManager mockBuffer(512);
MockSendCounterPacket sendCounterPacket(mockBuffer);
sendCounterPacket.SendStreamMetaDataPacket();
- const char* buffer = reinterpret_cast<const char*>(mockBuffer.GetReadBuffer(size));
+
+ auto packetBuffer = mockBuffer.GetReadableBuffer();
+ const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
+ mockBuffer.MarkRead(packetBuffer);
+
CounterDirectory counterDirectory;
sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
+ packetBuffer = mockBuffer.GetReadableBuffer();
+ buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
+
BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
+ mockBuffer.MarkRead(packetBuffer);
+
uint64_t timestamp = 0;
std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs;
sendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
+ packetBuffer = mockBuffer.GetReadableBuffer();
+ buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
+
BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
+ mockBuffer.MarkRead(packetBuffer);
+
uint32_t capturePeriod = 0;
std::vector<uint16_t> selectedCounterIds;
sendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
+ packetBuffer = mockBuffer.GetReadableBuffer();
+ buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
+
BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
+
+ mockBuffer.MarkRead(packetBuffer);
}
BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest)
{
// Error no space left in buffer
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer1(10);
+ MockBufferManager mockBuffer1(10);
SendCounterPacket sendPacket1(mockProfilingConnection, mockBuffer1);
uint32_t capturePeriod = 1000;
BufferExhaustion);
// Packet without any counters
- MockBuffer mockBuffer2(512);
+ MockBufferManager mockBuffer2(512);
SendCounterPacket sendPacket2(mockProfilingConnection, mockBuffer2);
sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
- unsigned int sizeRead = 0;
- const unsigned char* readBuffer2 = mockBuffer2.GetReadBuffer(sizeRead);
+ auto readBuffer2 = mockBuffer2.GetReadableBuffer();
uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
BOOST_TEST(period == 1000); // capture period
// Full packet message
- MockBuffer mockBuffer3(512);
+ MockBufferManager mockBuffer3(512);
SendCounterPacket sendPacket3(mockProfilingConnection, mockBuffer3);
selectedCounterIds.reserve(5);
selectedCounterIds.emplace_back(400);
selectedCounterIds.emplace_back(500);
sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
- sizeRead = 0;
- const unsigned char* readBuffer3 = mockBuffer3.GetReadBuffer(sizeRead);
+ auto readBuffer3 = mockBuffer3.GetReadableBuffer();
headerWord0 = ReadUint32(readBuffer3, 0);
headerWord1 = ReadUint32(readBuffer3, 4);
{
// Error no space left in buffer
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer1(10);
+ MockBufferManager mockBuffer1(10);
SendCounterPacket sendPacket1(mockProfilingConnection, mockBuffer1);
auto captureTimestamp = std::chrono::steady_clock::now();
BufferExhaustion);
// Packet without any counters
- MockBuffer mockBuffer2(512);
+ MockBufferManager mockBuffer2(512);
SendCounterPacket sendPacket2(mockProfilingConnection, mockBuffer2);
sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
- unsigned int sizeRead = 0;
- const unsigned char* readBuffer2 = mockBuffer2.GetReadBuffer(sizeRead);
+ auto readBuffer2 = mockBuffer2.GetReadableBuffer();
uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
BOOST_TEST(time == readTimestamp); // capture period
// Full packet message
- MockBuffer mockBuffer3(512);
+ MockBufferManager mockBuffer3(512);
SendCounterPacket sendPacket3(mockProfilingConnection, mockBuffer3);
indexValuePairs.reserve(5);
indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(3, 400));
indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(4, 500));
sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
- sizeRead = 0;
- const unsigned char* readBuffer3 = mockBuffer3.GetReadBuffer(sizeRead);
+ auto readBuffer3 = mockBuffer3.GetReadableBuffer();
headerWord0 = ReadUint32(readBuffer3, 0);
headerWord1 = ReadUint32(readBuffer3, 4);
// Error no space left in buffer
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer1(10);
+ MockBufferManager mockBuffer1(10);
SendCounterPacket sendPacket1(mockProfilingConnection, mockBuffer1);
BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion);
uint32_t packetEntries = 6;
- MockBuffer mockBuffer2(512);
+ MockBufferManager mockBuffer2(512);
SendCounterPacket sendPacket2(mockProfilingConnection, mockBuffer2);
sendPacket2.SendStreamMetaDataPacket();
- unsigned int sizeRead = 0;
- const unsigned char* readBuffer2 = mockBuffer2.GetReadBuffer(sizeRead);
+ auto readBuffer2 = mockBuffer2.GetReadableBuffer();
uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
offset += sizeUint32;
BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved
+ const unsigned char* readData2 = readBuffer2->GetReadableData();
+
offset += sizeUint32;
if (infoSize)
{
- BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readBuffer2[offset]), GetSoftwareInfo().c_str()) == 0);
+ BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
offset += infoSize;
}
if (hardwareVersionSize)
{
- BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readBuffer2[offset]), GetHardwareVersion().c_str()) == 0);
+ BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
offset += hardwareVersionSize;
}
if (softwareVersionSize)
{
- BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readBuffer2[offset]), GetSoftwareVersion().c_str()) == 0);
+ BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
offset += softwareVersionSize;
}
if (processNameSize)
{
- BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readBuffer2[offset]), GetProcessName().c_str()) == 0);
+ BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
offset += processNameSize;
}
BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a device for testing
BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a device for testing
BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter set for testing
BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter set for testing
BOOST_AUTO_TEST_CASE(CreateEventRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter for testing
BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter for testing
BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter for testing
BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter for testing
BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a counter for testing
BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a category for testing
BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a category for testing
BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest2)
{
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(0);
+ MockBufferManager mockBuffer(0);
SendCounterPacketTest sendCounterPacketTest(mockProfilingConnection, mockBuffer);
// Create a category for testing
// Buffer with not enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(10);
+ MockBufferManager mockBuffer(10);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
armnn::profiling::BufferExhaustion);
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
- // Get the read buffer
- unsigned int sizeRead = 0;
- const unsigned char* readBuffer = mockBuffer.GetReadBuffer(sizeRead);
+ // Get the readable buffer
+ auto readBuffer = mockBuffer.GetReadableBuffer();
// Check the packet header
uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
uint32_t deviceRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
6u * uint32_t_size + // body_header
bodyHeaderWord1; // device_records_pointer_table_offset
+
+ const unsigned char* readData = readBuffer->GetReadableData();
+
for (uint32_t i = 0; i < deviceRecordCount; i++)
{
// Get the device record offset
BOOST_CHECK(deviceRecordNameNullTerminator == '\0');
std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
std::memcpy(deviceRecordNameBuffer.data(),
- readBuffer + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
+ readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
deviceRecords.push_back(deviceRecord);
BOOST_CHECK(counterSetRecordNameNullTerminator == '\0');
std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
std::memcpy(counterSetRecordNameBuffer.data(),
- readBuffer + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
+ readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
counterSetRecords.push_back(counterSetRecord);
BOOST_CHECK(categoryRecordNameNullTerminator == '\0');
std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
std::memcpy(categoryRecordNameBuffer.data(),
- readBuffer +
+ readData +
categoryRecordPoolOffset +
categoryRecord.name_offset +
uint32_t_size,
BOOST_CHECK(eventRecordNameNullTerminator == '\0');
std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
std::memcpy(eventRecordNameBuffer.data(),
- readBuffer +
+ readData +
eventRecordPoolOffset +
eventRecord.name_offset +
uint32_t_size,
BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0');
std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
std::memcpy(eventRecordDescriptionBuffer.data(),
- readBuffer +
+ readData +
eventRecordPoolOffset +
eventRecord.description_offset +
uint32_t_size,
BOOST_CHECK(eventRecordUnitsNullTerminator == '\0');
std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
std::memcpy(eventRecordUnitsBuffer.data(),
- readBuffer +
+ readData +
eventRecordPoolOffset +
eventRecord.units_offset +
uint32_t_size,
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
}
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
}
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
}
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
}
// Buffer with enough space
MockProfilingConnection mockProfilingConnection;
- MockBuffer mockBuffer(1024);
+ MockBufferManager mockBuffer(1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
}
BOOST_AUTO_TEST_CASE(SendThreadTest0)
{
MockProfilingConnection mockProfilingConnection;
- MockStreamCounterBuffer mockStreamCounterBuffer(0);
+ MockStreamCounterBuffer mockStreamCounterBuffer(1, 0);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockStreamCounterBuffer);
// Try to start the send thread many times, it must only start once
BOOST_AUTO_TEST_CASE(SendThreadTest1)
{
- size_t totalWrittenSize = 0;
+ unsigned int totalWrittenSize = 0;
MockProfilingConnection mockProfilingConnection;
- MockStreamCounterBuffer mockStreamCounterBuffer(100);
+ MockStreamCounterBuffer mockStreamCounterBuffer(5,1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockStreamCounterBuffer);
sendCounterPacket.Start();
sendCounterPacket.SendStreamMetaDataPacket();
// Get the size of the Stream Metadata Packet
- size_t streamMetadataPacketsize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ std::string processName = GetProcessName().substr(0, 60);
+ unsigned int processNameSize = boost::numeric_cast<unsigned int>(processName.size()) > 0 ?
+ boost::numeric_cast<unsigned int>(processName.size()) + 1 : 0;
+ unsigned int streamMetadataPacketsize = 118 + processNameSize;
totalWrittenSize += streamMetadataPacketsize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
// Get the size of the Counter Directory Packet
- size_t counterDirectoryPacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int counterDirectoryPacketSize = 32;
totalWrittenSize += counterDirectoryPacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- size_t periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int periodicCounterCapturePacketSize = 28;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 22;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 46;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 40;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 30;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
// To test an exact value of the "read size" in the mock buffer, wait a second to allow the send thread to
// read all what's remaining in the buffer
- std::this_thread::sleep_for(std::chrono::seconds(1));
+ std::this_thread::sleep_for(std::chrono::seconds(2));
sendCounterPacket.Stop();
- BOOST_CHECK(mockStreamCounterBuffer.GetBufferSize() == totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadableBufferSize() == totalWrittenSize);
BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
- BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
}
BOOST_AUTO_TEST_CASE(SendThreadTest2)
{
- size_t totalWrittenSize = 0;
+ unsigned int totalWrittenSize = 0;
MockProfilingConnection mockProfilingConnection;
- MockStreamCounterBuffer mockStreamCounterBuffer(100);
+ MockStreamCounterBuffer mockStreamCounterBuffer(5, 1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockStreamCounterBuffer);
sendCounterPacket.Start();
sendCounterPacket.SendStreamMetaDataPacket();
// Get the size of the Stream Metadata Packet
- size_t streamMetadataPacketsize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ std::string processName = GetProcessName().substr(0, 60);
+ unsigned int processNameSize = boost::numeric_cast<unsigned int>(processName.size()) > 0 ?
+ boost::numeric_cast<unsigned int>(processName.size()) + 1 : 0;
+ unsigned int streamMetadataPacketsize = 118 + processNameSize;
totalWrittenSize += streamMetadataPacketsize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
// Get the size of the Counter Directory Packet
- size_t counterDirectoryPacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int counterDirectoryPacketSize = 32;
totalWrittenSize += counterDirectoryPacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- size_t periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int periodicCounterCapturePacketSize = 28;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 22;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 46;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 40;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 30;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
// To test an exact value of the "read size" in the mock buffer, wait a second to allow the send thread to
// read all what's remaining in the buffer
- std::this_thread::sleep_for(std::chrono::seconds(1));
+ std::this_thread::sleep_for(std::chrono::seconds(2));
sendCounterPacket.Stop();
- BOOST_CHECK(mockStreamCounterBuffer.GetBufferSize() == totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadableBufferSize() == totalWrittenSize);
BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
- BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
}
BOOST_AUTO_TEST_CASE(SendThreadTest3)
{
- size_t totalWrittenSize = 0;
+ unsigned int totalWrittenSize = 0;
MockProfilingConnection mockProfilingConnection;
- MockStreamCounterBuffer mockStreamCounterBuffer(100);
+ MockStreamCounterBuffer mockStreamCounterBuffer(10, 1024);
SendCounterPacket sendCounterPacket(mockProfilingConnection, mockStreamCounterBuffer);
sendCounterPacket.Start();
sendCounterPacket.SendStreamMetaDataPacket();
// Get the size of the Stream Metadata Packet
- size_t streamMetadataPacketsize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ std::string processName = GetProcessName().substr(0, 60);
+ unsigned int processNameSize = boost::numeric_cast<unsigned int>(processName.size()) > 0 ?
+ boost::numeric_cast<unsigned int>(processName.size()) + 1 : 0;
+ unsigned int streamMetadataPacketsize = 118 + processNameSize;
totalWrittenSize += streamMetadataPacketsize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
// Get the size of the Counter Directory Packet
- size_t counterDirectoryPacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int counterDirectoryPacketSize =32;
totalWrittenSize += counterDirectoryPacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- size_t periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ unsigned int periodicCounterCapturePacketSize = 28;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 22;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 46;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
});
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 40;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
// Get the size of the Periodic Counter Capture Packet
- periodicCounterCapturePacketSize = GetPacketSize(mockStreamCounterBuffer, totalWrittenSize);
+ periodicCounterCapturePacketSize = 30;
totalWrittenSize += periodicCounterCapturePacketSize;
sendCounterPacket.SetReadyToRead();
// thread is not guaranteed to flush the buffer)
sendCounterPacket.Stop();
- BOOST_CHECK(mockStreamCounterBuffer.GetBufferSize() == totalWrittenSize);
- BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
- BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadableBufferSize() <= totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() <= totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
+ BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
}
BOOST_AUTO_TEST_SUITE_END()
bool m_IsOpen;
};
-class MockBuffer : public IBufferWrapper
+class MockPacketBuffer : public IPacketBuffer
{
public:
- MockBuffer(unsigned int size)
- : m_BufferSize(size)
- , m_Buffer(std::make_unique<unsigned char[]>(size))
- {}
+ MockPacketBuffer(unsigned int maxSize)
+ : m_MaxSize(maxSize),
+ m_Size(0)
+ {
+ m_Data = std::make_unique<unsigned char[]>(m_MaxSize);
+ }
+
+ ~MockPacketBuffer() {}
+
+ const unsigned char* const GetReadableData() const override { return m_Data.get(); }
+
+ unsigned int GetSize() const override { return m_Size; }
+
+ void MarkRead() override { m_Size = 0;}
+
+ void Commit(unsigned int size) override { m_Size = size; }
+
+ void Release() override { m_Size = 0; }
+
+ unsigned char* GetWritableData() override { return m_Data.get(); }
+
+private:
+ unsigned int m_MaxSize;
+ unsigned int m_Size;
+ std::unique_ptr<unsigned char[]> m_Data;
+};
- unsigned char* Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
+class MockBufferManager : public IBufferManager
+{
+public:
+ MockBufferManager(unsigned int size)
+ : m_BufferSize(size),
+ m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
+
+ ~MockBufferManager() {}
+
+ std::unique_ptr<IPacketBuffer> Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
{
if (requestedSize > m_BufferSize)
{
reservedSize = requestedSize;
}
- return m_Buffer.get();
+ return std::move(m_Buffer);
+ }
+
+ void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override
+ {
+ packetBuffer->Commit(size);
+ m_Buffer = std::move(packetBuffer);
}
- void Commit(unsigned int size) override {}
+ std::unique_ptr<IPacketBuffer> GetReadableBuffer() override
+ {
+ return std::move(m_Buffer);
+ }
- const unsigned char* GetReadBuffer(unsigned int& size) override
+ void Release(std::unique_ptr<IPacketBuffer>& packetBuffer) override
{
- size = static_cast<unsigned int>(strlen(reinterpret_cast<const char*>(m_Buffer.get())) + 1);
- return m_Buffer.get();
+ packetBuffer->Release();
+ m_Buffer = std::move(packetBuffer);
}
- void Release(unsigned int size) override {}
+ void MarkRead(std::unique_ptr<IPacketBuffer>& packetBuffer) override
+ {
+ packetBuffer->MarkRead();
+ m_Buffer = std::move(packetBuffer);
+ }
private:
unsigned int m_BufferSize;
- std::unique_ptr<unsigned char[]> m_Buffer;
+ std::unique_ptr<IPacketBuffer> m_Buffer;
};
-class MockStreamCounterBuffer : public IBufferWrapper
+class MockStreamCounterBuffer : public IBufferManager
{
public:
- MockStreamCounterBuffer(unsigned int size)
- : m_Buffer(size, 0)
- , m_CommittedSize(0)
- , m_ReadSize(0)
- {}
-
- unsigned char* Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
+ MockStreamCounterBuffer(unsigned int numberOfBuffers = 5, unsigned int maxPacketSize = 4096)
+ : m_MaxBufferSize(maxPacketSize)
+ , m_ReadableSize(0)
+ , m_CommittedSize(0)
+ , m_ReadSize(0)
{
- std::unique_lock<std::mutex>(m_Mutex);
+ m_AvailableList.reserve(numberOfBuffers);
+ for (unsigned int i = 0; i < numberOfBuffers; ++i)
+ {
+ std::unique_ptr<IPacketBuffer> buffer = std::make_unique<MockPacketBuffer>(maxPacketSize);
+ m_AvailableList.emplace_back(std::move(buffer));
+ }
+ m_ReadableList.reserve(numberOfBuffers);
+ }
- // Get the buffer size and the available size in the buffer past the committed size
- size_t bufferSize = m_Buffer.size();
- size_t availableSize = bufferSize - m_CommittedSize;
+ ~MockStreamCounterBuffer() {}
- // Check whether the buffer needs to be resized
- if (requestedSize > availableSize)
+ std::unique_ptr<IPacketBuffer> Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
+ {
+ std::unique_lock<std::mutex> availableListLock(m_AvailableMutex, std::defer_lock);
+ if (requestedSize > m_MaxBufferSize)
+ {
+ throw armnn::Exception("Maximum buffer size that can be requested is [" +
+ std::to_string(m_MaxBufferSize) + "] bytes");
+ }
+ availableListLock.lock();
+ if (m_AvailableList.empty())
{
- // Resize the buffer
- size_t newSize = m_CommittedSize + requestedSize;
- m_Buffer.resize(newSize, 0);
+ throw armnn::profiling::BufferExhaustion("Buffer not available");
}
-
- // Set the reserved size
+ std::unique_ptr<IPacketBuffer> buffer = std::move(m_AvailableList.back());
+ m_AvailableList.pop_back();
+ availableListLock.unlock();
reservedSize = requestedSize;
-
- // Get a pointer to the beginning of the part of buffer available for writing
- return m_Buffer.data() + m_CommittedSize;
+ return buffer;
}
- void Commit(unsigned int size) override
+ void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override
{
- std::unique_lock<std::mutex>(m_Mutex);
-
- // Update the committed size
+ std::unique_lock<std::mutex> readableListLock(m_ReadableMutex, std::defer_lock);
+ packetBuffer.get()->Commit(size);
+ readableListLock.lock();
+ m_ReadableList.push_back(std::move(packetBuffer));
+ readableListLock.unlock();
+ m_ReadDataAvailable.notify_one();
m_CommittedSize += size;
}
- const unsigned char* GetReadBuffer(unsigned int& size) override
+ void Release(std::unique_ptr<IPacketBuffer>& packetBuffer) override
{
- std::unique_lock<std::mutex>(m_Mutex);
-
- // Get the size available for reading
- size = boost::numeric_cast<unsigned int>(m_CommittedSize - m_ReadSize);
-
- // Get a pointer to the beginning of the part of buffer available for reading
- const unsigned char* readBuffer = m_Buffer.data() + m_ReadSize;
-
- // Update the read size
- m_ReadSize = m_CommittedSize;
-
- return readBuffer;
+ std::unique_lock<std::mutex> availableListLock(m_AvailableMutex, std::defer_lock);
+ packetBuffer.get()->Release();
+ availableListLock.lock();
+ m_AvailableList.push_back(std::move(packetBuffer));
+ availableListLock.unlock();
+ m_CommittedSize = 0;
+ m_ReadSize = 0;
+ m_ReadableSize = 0;
}
- void Release(unsigned int size) override
+ std::unique_ptr<IPacketBuffer> GetReadableBuffer() override
{
- std::unique_lock<std::mutex>(m_Mutex);
-
- if (size == 0)
+ std::unique_lock<std::mutex> readableListLock(m_ReadableMutex);
+ if (!m_ReadableList.empty())
{
- // Nothing to release
- return;
+ std::unique_ptr<IPacketBuffer> buffer = std::move(m_ReadableList.back());
+ m_ReadableSize+=buffer->GetSize();
+ m_ReadableList.pop_back();
+ readableListLock.unlock();
+ return buffer;
}
+ return nullptr;
+ }
- // Get the buffer size
- size_t bufferSize = m_Buffer.size();
-
- // Remove the last "size" bytes from the buffer
- if (size < bufferSize)
- {
- // Resize the buffer
- size_t newSize = bufferSize - size;
- m_Buffer.resize(newSize);
- }
- else
- {
- // Clear the whole buffer
- m_Buffer.clear();
- }
+ void MarkRead(std::unique_ptr<IPacketBuffer>& packetBuffer) override
+ {
+ std::unique_lock<std::mutex> availableListLock(m_AvailableMutex, std::defer_lock);
+ // increase read size
+ m_ReadSize += packetBuffer->GetSize();
+ packetBuffer->MarkRead();
+ availableListLock.lock();
+ m_AvailableList.push_back(std::move(packetBuffer));
+ availableListLock.unlock();
}
- size_t GetBufferSize() const { return m_Buffer.size(); }
- size_t GetCommittedSize() const { return m_CommittedSize; }
- size_t GetReadSize() const { return m_ReadSize; }
- const unsigned char* GetBuffer() const { return m_Buffer.data(); }
+ unsigned int GetReadableBufferSize() const
+ {
+ return m_ReadableSize;
+ }
+ unsigned int GetCommittedSize() const { return m_CommittedSize; }
+ unsigned int GetReadSize() const { return m_ReadSize; }
private:
- // This mock uses an ever-expanding vector to simulate a counter stream buffer
- std::vector<unsigned char> m_Buffer;
+ unsigned int m_MaxBufferSize;
+ std::vector<std::unique_ptr<IPacketBuffer>> m_AvailableList;
+ std::vector<std::unique_ptr<IPacketBuffer>> m_ReadableList;
+ std::mutex m_AvailableMutex;
+ std::mutex m_ReadableMutex;
+ std::condition_variable m_ReadDataAvailable;
+
+ // The size of the buffer that can be read
+ unsigned int m_ReadableSize;
// The size of the buffer that has been committed for reading
- size_t m_CommittedSize;
+ unsigned int m_CommittedSize;
// The size of the buffer that has already been read
- size_t m_ReadSize;
-
- // This mock buffer provides basic synchronization
- std::mutex m_Mutex;
+ unsigned int m_ReadSize;
};
class MockSendCounterPacket : public ISendCounterPacket
{
public:
- MockSendCounterPacket(IBufferWrapper& sendBuffer) : m_Buffer(sendBuffer) {}
+ MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
void SendStreamMetaDataPacket() override
{
std::string message("SendStreamMetaDataPacket");
unsigned int reserved = 0;
- unsigned char* buffer = m_Buffer.Reserve(1024, reserved);
- memcpy(buffer, message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
+ memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ m_BufferManager.Commit(buffer, reserved);
}
void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
{
std::string message("SendCounterDirectoryPacket");
unsigned int reserved = 0;
- unsigned char* buffer = m_Buffer.Reserve(1024, reserved);
- memcpy(buffer, message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
+ memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ m_BufferManager.Commit(buffer, reserved);
}
void SendPeriodicCounterCapturePacket(uint64_t timestamp,
{
std::string message("SendPeriodicCounterCapturePacket");
unsigned int reserved = 0;
- unsigned char* buffer = m_Buffer.Reserve(1024, reserved);
- memcpy(buffer, message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
+ memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ m_BufferManager.Commit(buffer, reserved);
}
void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
{
std::string message("SendPeriodicCounterSelectionPacket");
unsigned int reserved = 0;
- unsigned char* buffer = m_Buffer.Reserve(1024, reserved);
- memcpy(buffer, message.c_str(), static_cast<unsigned int>(message.size()) + 1);
- m_Buffer.Commit(reserved);
+ std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
+ memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
+ m_BufferManager.Commit(buffer, reserved);
}
void SetReadyToRead() override {}
private:
- IBufferWrapper& m_Buffer;
+ IBufferManager& m_BufferManager;
};
class MockCounterDirectory : public ICounterDirectory
class SendCounterPacketTest : public SendCounterPacket
{
public:
- SendCounterPacketTest(IProfilingConnection& profilingconnection, IBufferWrapper& buffer)
+ SendCounterPacketTest(IProfilingConnection& profilingconnection, IBufferManager& buffer)
: SendCounterPacket(profilingconnection, buffer)
{}