From 92fa15b8ad514a026093748a0cf0771eb99bbc6f Mon Sep 17 00:00:00 2001 From: Jan Eilers Date: Tue, 15 Oct 2019 15:23:25 +0100 Subject: IVGCVSW-3929 Create the Timeline Event Class Binary Packet * Added some utility functions * Added WriteTimelineEventClassBinaryPacket * Added related unit tests Signed-off-by: Jan Eilers Change-Id: Ib1fca3f8642bf5bbddb5ce51f2d90581a86359b1 --- src/profiling/ProfilingUtils.cpp | 115 +++++++++++++++++++++++++++++ src/profiling/ProfilingUtils.hpp | 5 ++ src/profiling/test/TimelinePacketTests.cpp | 90 ++++++++++++++++++++++ 3 files changed, 210 insertions(+) (limited to 'src/profiling') diff --git a/src/profiling/ProfilingUtils.cpp b/src/profiling/ProfilingUtils.cpp index e458a3f720..6c5437a597 100644 --- a/src/profiling/ProfilingUtils.cpp +++ b/src/profiling/ProfilingUtils.cpp @@ -239,6 +239,63 @@ std::string GetProcessName() return name; } +/// Creates a timeline packet header +/// +/// \params +/// packetFamiliy Timeline Packet Family +/// packetClass Timeline Packet Class +/// packetType Timeline Packet Type +/// streamId Stream identifier +/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number +/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted. +/// +/// \returns +/// Pair of uint32_t containing word0 and word1 of the header +std::pair CreateTimelinePacketHeader(uint32_t packetFamily, + uint32_t packetClass, + uint32_t packetType, + uint32_t streamId, + uint32_t sequenceNumbered, + uint32_t dataLength) +{ + // Packet header word 0: + // 26:31 [6] packet_family: timeline Packet Family, value 0b000001 + // 19:25 [7] packet_class: packet class + // 16:18 [3] packet_type: packet type + // 8:15 [8] reserved: all zeros + // 0:7 [8] stream_id: stream identifier + uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) | + ((packetClass & 0x0000007F) << 19) | + ((packetType & 0x00000007) << 16) | + ((streamId & 0x00000007) << 0); + + // Packet header word 1: + // 25:31 [7] reserved: all zeros + // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number + // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted + uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | + ((dataLength & 0x00FFFFFF) << 0); + + return std::make_pair(packetHeaderWord0, packetHeaderWord1); +} + +/// Creates a packet header for the timeline messages: +/// * declareLabel +/// * declareEntity +/// * declareEventClass +/// * declareRelationship +/// * declareEvent +/// +/// \param +/// Data lenght of the message body in byte +/// +/// \returns +/// Pair of uint32_t containing word0 and word1 of the header +std::pair CreateTimelineMessagePacketHeader(unsigned int dataLength) +{ + return CreateTimelinePacketHeader(1,0,1,0,0,dataLength); +} + TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label, unsigned char* buffer, @@ -528,6 +585,64 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, return TimelinePacketStatus::Ok; } +TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, + unsigned char* buffer, + unsigned int bufferSize, + unsigned int& numberOfBytesWritten) +{ + // Initialize the ouput value + numberOfBytesWritten = 0; + + // Check that the given buffer is valid + if (buffer == nullptr || bufferSize == 0) + { + return TimelinePacketStatus::BufferExhaustion; + } + + // Utils + unsigned int uint32_t_size = sizeof(uint32_t); + unsigned int uint64_t_size = sizeof(uint64_t); + + // dec_id of the timeline message + uint32_t decId = 2; + + // Calculate the length of the data (in bytes) + unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID + + // Calculate the timeline binary packet size (in bytes) + unsigned int packetSize = 2 * uint32_t_size + // Header (2 words) + packetBodySize; // Body + + // Check whether the timeline binary packet fits in the given buffer + if (packetSize > bufferSize) + { + return TimelinePacketStatus::BufferExhaustion; + } + + // Create packet header + uint32_t dataLength = boost::numeric_cast(packetBodySize); + std::pair packetHeader = CreateTimelineMessagePacketHeader(dataLength); + + // Initialize the offset for writing in the buffer + unsigned int offset = 0; + + // Write the timeline binary packet header to the buffer + WriteUint32(buffer, offset, packetHeader.first); + offset += uint32_t_size; + WriteUint32(buffer, offset, packetHeader.second); + offset += uint32_t_size; + + // Write the timeline binary packet payload to the buffer + WriteUint32(buffer, offset, decId); // dec_id + offset += uint32_t_size; + WriteUint64(buffer, offset, profilingGuid); // Profiling GUID + + // Update the number of bytes written + numberOfBytesWritten = packetSize; + + return TimelinePacketStatus::Ok; +} + } // namespace profiling } // namespace armnn diff --git a/src/profiling/ProfilingUtils.hpp b/src/profiling/ProfilingUtils.hpp index 47a6d3f67d..8c0251dc41 100644 --- a/src/profiling/ProfilingUtils.hpp +++ b/src/profiling/ProfilingUtils.hpp @@ -141,6 +141,11 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, unsigned int bufferSize, unsigned int& numberOfBytesWritten); +TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, + unsigned char* buffer, + unsigned int bufferSize, + unsigned int& numberOfBytesWritten); + class BufferExhaustion : public armnn::Exception { using Exception::Exception; diff --git a/src/profiling/test/TimelinePacketTests.cpp b/src/profiling/test/TimelinePacketTests.cpp index b039c373b2..d7a8e46df8 100644 --- a/src/profiling/test/TimelinePacketTests.cpp +++ b/src/profiling/test/TimelinePacketTests.cpp @@ -342,4 +342,94 @@ BOOST_AUTO_TEST_CASE(TimelineEntityPacketTest4) BOOST_CHECK(readProfilingGuid == profilingGuid); } +BOOST_AUTO_TEST_SUITE_END() + + +BOOST_AUTO_TEST_SUITE(TimelineEventClassTests) + +BOOST_AUTO_TEST_CASE(TimelineEventClassTest1) +{ + const uint64_t profilingGuid = 123456u; + unsigned int numberOfBytesWritten = 789u; + TimelinePacketStatus result = WriteTimelineEventClassBinaryPacket(profilingGuid, + nullptr, + 512u, + numberOfBytesWritten); + BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion); + BOOST_CHECK(numberOfBytesWritten == 0); +} + +BOOST_AUTO_TEST_CASE(TimelineEventClassTest2) +{ + std::vector buffer(512, 0); + + const uint64_t profilingGuid = 123456u; + unsigned int numberOfBytesWritten = 789u; + TimelinePacketStatus result = WriteTimelineEventClassBinaryPacket(profilingGuid, + buffer.data(), + 0, + numberOfBytesWritten); + BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion); + BOOST_CHECK(numberOfBytesWritten == 0); +} + +BOOST_AUTO_TEST_CASE(TimelineEventClassTest3) +{ + std::vector buffer(10, 0); + + const uint64_t profilingGuid = 123456u; + unsigned int numberOfBytesWritten = 789u; + TimelinePacketStatus result = WriteTimelineEventClassBinaryPacket(profilingGuid, + buffer.data(), + boost::numeric_cast(buffer.size()), + numberOfBytesWritten); + BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion); + BOOST_CHECK(numberOfBytesWritten == 0); +} + +BOOST_AUTO_TEST_CASE(TimelineEventClassTest4) +{ + std::vector buffer(512, 0); + + const uint64_t profilingGuid = 123456u; + unsigned int numberOfBytesWritten = 789u; + TimelinePacketStatus result = WriteTimelineEventClassBinaryPacket(profilingGuid, + buffer.data(), + boost::numeric_cast(buffer.size()), + numberOfBytesWritten); + BOOST_CHECK(result == TimelinePacketStatus::Ok); + BOOST_CHECK(numberOfBytesWritten == 20); + + unsigned int uint32_t_size = sizeof(uint32_t); + + // Check the packet header + unsigned int offset = 0; + uint32_t packetHeaderWord0 = ReadUint32(buffer.data(), offset); + uint32_t packetFamily = (packetHeaderWord0 >> 26) & 0x0000003F; + uint32_t packetClass = (packetHeaderWord0 >> 19) & 0x0000007F; + uint32_t packetType = (packetHeaderWord0 >> 16) & 0x00000007; + uint32_t streamId = (packetHeaderWord0 >> 0) & 0x00000007; + BOOST_CHECK(packetFamily == 1); + BOOST_CHECK(packetClass == 0); + BOOST_CHECK(packetType == 1); + BOOST_CHECK(streamId == 0); + + offset += uint32_t_size; + uint32_t packetHeaderWord1 = ReadUint32(buffer.data(), offset); + uint32_t sequenceNumbered = (packetHeaderWord1 >> 24) & 0x00000001; + uint32_t dataLength = (packetHeaderWord1 >> 0) & 0x00FFFFFF; + BOOST_CHECK(sequenceNumbered == 0); + BOOST_CHECK(dataLength == 12); + + // Check the decl_id + offset += uint32_t_size; + uint32_t declId = ReadUint32(buffer.data(), offset); + BOOST_CHECK(declId == uint32_t(2)); + + // Check the profiling GUID + offset += uint32_t_size; + uint64_t readProfilingGuid = ReadUint64(buffer.data(), offset); + BOOST_CHECK(readProfilingGuid == profilingGuid); +} + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file -- cgit v1.2.1