diff options
author | Narumol Prangnawarat <narumol.prangnawarat@arm.com> | 2019-09-30 16:20:20 +0100 |
---|---|---|
committer | Narumol Prangnawarat <narumol.prangnawarat@arm.com> | 2019-10-04 10:13:25 +0100 |
commit | 0ec068f220daf03049a9ffb5ac53118162f50106 (patch) | |
tree | bf0673657c6ff328a70346f7fb4f714143af17b6 /src/profiling/test/SendCounterPacketTests.cpp | |
parent | 6b0f3c6d7025fb26bd2a7733dbfba02afaa44313 (diff) | |
download | armnn-0ec068f220daf03049a9ffb5ac53118162f50106.tar.gz |
IVGCVSW-3904 Add more unit tests for send thread with BufferManager
* Add timeout parameter to wait for readable data
* Write all readable data to the profiling connection when ready to read
* Set ready to read when buffer exhaust
* Ensure that readable data get written to profiling connection
before the send thread is stopped
* Add MockWriteProfilingConnection to be able to test WritePacket
* Refactor BufferManager and the unit tests
Signed-off-by: Narumol Prangnawarat <narumol.prangnawarat@arm.com>
Change-Id: I80ae01bd8d0119a3a3a957069ae8ac521c005a12
Diffstat (limited to 'src/profiling/test/SendCounterPacketTests.cpp')
-rw-r--r-- | src/profiling/test/SendCounterPacketTests.cpp | 195 |
1 files changed, 194 insertions, 1 deletions
diff --git a/src/profiling/test/SendCounterPacketTests.cpp b/src/profiling/test/SendCounterPacketTests.cpp index ba1d470a50..0d21ec0b5d 100644 --- a/src/profiling/test/SendCounterPacketTests.cpp +++ b/src/profiling/test/SendCounterPacketTests.cpp @@ -5,10 +5,11 @@ #include "SendCounterPacketTests.hpp" +#include <CounterDirectory.hpp> +#include <BufferManager.hpp> #include <EncodeVersion.hpp> #include <ProfilingUtils.hpp> #include <SendCounterPacket.hpp> -#include <CounterDirectory.hpp> #include <armnn/Exceptions.hpp> #include <armnn/Conversion.hpp> @@ -2044,4 +2045,196 @@ BOOST_AUTO_TEST_CASE(SendThreadTest3) BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize()); } +BOOST_AUTO_TEST_CASE(SendThreadBufferTest) +{ + MockProfilingConnection mockProfilingConnection; + BufferManager bufferManager(1, 1024); + SendCounterPacket sendCounterPacket(mockProfilingConnection, bufferManager, -1); + sendCounterPacket.Start(); + + // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for + // something to become available for reading + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // SendStreamMetaDataPacket + sendCounterPacket.SendStreamMetaDataPacket(); + + // Read data from the buffer + // Buffer should become readable after commit by SendStreamMetaDataPacket + auto packetBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(packetBuffer.get()); + + std::string processName = GetProcessName().substr(0, 60); + unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; + unsigned int streamMetadataPacketsize = 118 + processNameSize; + BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize); + + // Buffer is not available when SendStreamMetaDataPacket already occupied the buffer. + unsigned int reservedSize = 0; + auto reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(!reservedBuffer.get()); + + // Recommit to be read by sendCounterPacket + bufferManager.Commit(packetBuffer, streamMetadataPacketsize); + + sendCounterPacket.SetReadyToRead(); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // The buffer is read by the send thread so it should not be in the readable buffer. + auto readBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(!readBuffer); + + // Successfully reserved the buffer with requested size + reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 512); + BOOST_TEST(reservedBuffer.get()); + + // Release the buffer to be used by sendCounterPacket + bufferManager.Release(reservedBuffer); + + // SendCounterDirectoryPacket + CounterDirectory counterDirectory; + sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); + + // Read data from the buffer + // Buffer should become readable after commit by SendCounterDirectoryPacket + auto counterDirectoryPacketBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(counterDirectoryPacketBuffer.get()); + + // Get the size of the Counter Directory Packet + unsigned int counterDirectoryPacketSize = 32; + BOOST_TEST(counterDirectoryPacketBuffer->GetSize() == counterDirectoryPacketSize); + + // Buffer is not available when SendCounterDirectoryPacket already occupied the buffer. + reservedSize = 0; + reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 0); + BOOST_TEST(!reservedBuffer.get()); + + // Recommit to be read by sendCounterPacket + bufferManager.Commit(counterDirectoryPacketBuffer, counterDirectoryPacketSize); + + sendCounterPacket.SetReadyToRead(); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // The buffer is read by the send thread so it should not be in the readable buffer. + readBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(!readBuffer); + + // Successfully reserved the buffer with requested size + reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 512); + BOOST_TEST(reservedBuffer.get()); + + // Release the buffer to be used by sendCounterPacket + bufferManager.Release(reservedBuffer); + + // SendPeriodicCounterCapturePacket + sendCounterPacket.SendPeriodicCounterCapturePacket(123u, + { + { 1u, 23u }, + { 33u, 1207623u } + }); + + // Read data from the buffer + // Buffer should become readable after commit by SendPeriodicCounterCapturePacket + auto periodicCounterCapturePacketBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(periodicCounterCapturePacketBuffer.get()); + + // Get the size of the Periodic Counter Capture Packet + unsigned int periodicCounterCapturePacketSize = 28; + BOOST_TEST(periodicCounterCapturePacketBuffer->GetSize() == periodicCounterCapturePacketSize); + + // Buffer is not available when SendPeriodicCounterCapturePacket already occupied the buffer. + reservedSize = 0; + reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 0); + BOOST_TEST(!reservedBuffer.get()); + + // Recommit to be read by sendCounterPacket + bufferManager.Commit(periodicCounterCapturePacketBuffer, periodicCounterCapturePacketSize); + + sendCounterPacket.SetReadyToRead(); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // The buffer is read by the send thread so it should not be in the readable buffer. + readBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(!readBuffer); + + // Successfully reserved the buffer with requested size + reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 512); + BOOST_TEST(reservedBuffer.get()); + + sendCounterPacket.Stop(); +} + +BOOST_AUTO_TEST_CASE(SendThreadBufferTest1) +{ + MockWriteProfilingConnection mockProfilingConnection; + BufferManager bufferManager(3, 1024); + SendCounterPacket sendCounterPacket(mockProfilingConnection, bufferManager, -1); + sendCounterPacket.Start(); + + // SendStreamMetaDataPacket + sendCounterPacket.SendStreamMetaDataPacket(); + + // Read data from the buffer + // Buffer should become readable after commit by SendStreamMetaDataPacket + auto packetBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(packetBuffer.get()); + + std::string processName = GetProcessName().substr(0, 60); + unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1; + unsigned int streamMetadataPacketsize = 118 + processNameSize; + BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize); + + // Recommit to be read by sendCounterPacket + bufferManager.Commit(packetBuffer, streamMetadataPacketsize); + + sendCounterPacket.SetReadyToRead(); + + // SendCounterDirectoryPacket + CounterDirectory counterDirectory; + sendCounterPacket.SendCounterDirectoryPacket(counterDirectory); + + sendCounterPacket.SetReadyToRead(); + + // SendPeriodicCounterCapturePacket + sendCounterPacket.SendPeriodicCounterCapturePacket(123u, + { + { 1u, 23u }, + { 33u, 1207623u } + }); + + sendCounterPacket.SetReadyToRead(); + + sendCounterPacket.Stop(); + + // The buffer is read by the send thread so it should not be in the readable buffer. + auto readBuffer = bufferManager.GetReadableBuffer(); + BOOST_TEST(!readBuffer); + + // Successfully reserved the buffer with requested size + unsigned int reservedSize = 0; + auto reservedBuffer = bufferManager.Reserve(512, reservedSize); + BOOST_TEST(reservedSize == 512); + BOOST_TEST(reservedBuffer.get()); + + // Check that data was actually written to the profiling connection in any order + std::vector<uint32_t> writtenData = mockProfilingConnection.GetWrittenData(); + std::vector<uint32_t> expectedOutput{streamMetadataPacketsize, 32, 28}; + BOOST_TEST(writtenData.size() == 3); + bool foundStreamMetaDataPacket = + std::find(writtenData.begin(), writtenData.end(), streamMetadataPacketsize) != writtenData.end(); + bool foundCounterDirectoryPacket = std::find(writtenData.begin(), writtenData.end(), 32) != writtenData.end(); + bool foundPeriodicCounterCapturePacket = std::find(writtenData.begin(), writtenData.end(), 28) != writtenData.end(); + BOOST_TEST(foundStreamMetaDataPacket); + BOOST_TEST(foundCounterDirectoryPacket); + BOOST_TEST(foundPeriodicCounterCapturePacket); +} + BOOST_AUTO_TEST_SUITE_END() |