From b884ea4441097b2a8231aed90b41a637524944c2 Mon Sep 17 00:00:00 2001 From: Jan Eilers Date: Wed, 16 Oct 2019 09:54:15 +0100 Subject: Refactor ProfilingUtils * Added decl_id to all swtrace stream messages * Adjusted unit tests to include decl_id * Refactored code to reduce code duplication Signed-off-by: Jan Eilers Change-Id: I7a607765d9e65b1c08e4f914acbeda392400f6a4 --- src/profiling/ProfilingUtils.cpp | 104 ++++++++--------------------- src/profiling/test/TimelinePacketTests.cpp | 27 ++++---- 2 files changed, 41 insertions(+), 90 deletions(-) (limited to 'src/profiling') diff --git a/src/profiling/ProfilingUtils.cpp b/src/profiling/ProfilingUtils.cpp index 6c5437a597..798cfa4f29 100644 --- a/src/profiling/ProfilingUtils.cpp +++ b/src/profiling/ProfilingUtils.cpp @@ -327,12 +327,13 @@ TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, unsigned int swTraceLabelSize = boost::numeric_cast(swTraceLabel.size()) * uint32_t_size; // Calculate the length of the data (in bytes) - unsigned int timelineLabelPacketDataLength = uint64_t_size + // Profiling GUID + unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id + uint64_t_size + // Profiling GUID swTraceLabelSize; // Label // Calculate the timeline binary packet size (in bytes) unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words) - timelineLabelPacketDataLength; // Profiling GUID + label + timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label // Check whether the timeline binary packet fits in the given buffer if (timelineLabelPacketSize > bufferSize) @@ -340,37 +341,21 @@ TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, return TimelinePacketStatus::BufferExhaustion; } - // 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 packetFamily = 1; - uint32_t packetClass = 0; - uint32_t packetType = 1; - uint32_t streamId = 0; - 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 sequenceNumbered = 0; - uint32_t dataLength = boost::numeric_cast(timelineLabelPacketDataLength); // Profiling GUID + label - uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | - ((dataLength & 0x00FFFFFF) << 0); + // Create packet header + uint32_t dataLength = boost::numeric_cast(timelineLabelPacketDataLength); // decl_id + GUID + label + 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, packetHeaderWord0); + WriteUint32(buffer, offset, packetHeader.first); + offset += uint32_t_size; + WriteUint32(buffer, offset, packetHeader.second); offset += uint32_t_size; - WriteUint32(buffer, offset, packetHeaderWord1); + + // Write decl_Id to the buffer + WriteUint32(buffer, offset, 0u); offset += uint32_t_size; // Write the timeline binary packet payload to the buffer @@ -412,7 +397,8 @@ TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, // Calculate the timeline binary packet size (in bytes) unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words) - timelineEntityPacketDataLength; // Profiling GUID + uint32_t_size + // decl_Id + timelineEntityPacketDataLength; // Profiling GUID // Check whether the timeline binary packet fits in the given buffer if (timelineEntityPacketSize > bufferSize) @@ -420,37 +406,21 @@ TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, return TimelinePacketStatus::BufferExhaustion; } - // 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 packetFamily = 1; - uint32_t packetClass = 0; - uint32_t packetType = 1; - uint32_t streamId = 0; - 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 sequenceNumbered = 0; - uint32_t dataLength = boost::numeric_cast(timelineEntityPacketDataLength); // Profiling GUID - uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | - ((dataLength & 0x00FFFFFF) << 0); + // Create packet header + uint32_t dataLength = boost::numeric_cast(timelineEntityPacketDataLength); + 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, packetHeaderWord0); + WriteUint32(buffer, offset, packetHeader.first); + offset += uint32_t_size; + WriteUint32(buffer, offset, packetHeader.second); offset += uint32_t_size; - WriteUint32(buffer, offset, packetHeaderWord1); + + // Write the decl_Id to the buffer + WriteUint32(buffer, offset, 1u); offset += uint32_t_size; // Write the timeline binary packet payload to the buffer @@ -478,21 +448,6 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, // Utils unsigned int uint32_t_size = sizeof(uint32_t); - // 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 packetFamily = 1; - uint32_t packetClass = 0; - uint32_t packetType = 0; - uint32_t streamId = 0; - uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) | - ((packetClass & 0x0000007F) << 19) | - ((packetType & 0x00000007) << 16) | - ((streamId & 0x00000007) << 0); - // the payload/data of the packet consists of swtrace event definitions encoded according // to the swtrace directory specification. The messages being the five defined below: // | decl_id | decl_name | ui_name | arg_types | arg_names | @@ -545,22 +500,17 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, return TimelinePacketStatus::BufferExhaustion; } - // 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 sequenceNumbered = 0; + // Create packet header uint32_t dataLength = boost::numeric_cast(messagesDataLength); - uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | - ((dataLength & 0x00FFFFFF) << 0); + std::pair packetHeader = CreateTimelinePacketHeader(1,0,0,0,0,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, packetHeaderWord0); + WriteUint32(buffer, offset, packetHeader.first); offset += uint32_t_size; - WriteUint32(buffer, offset, packetHeaderWord1); + WriteUint32(buffer, offset, packetHeader.second); offset += uint32_t_size; for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i) diff --git a/src/profiling/test/TimelinePacketTests.cpp b/src/profiling/test/TimelinePacketTests.cpp index d7a8e46df8..759d825580 100644 --- a/src/profiling/test/TimelinePacketTests.cpp +++ b/src/profiling/test/TimelinePacketTests.cpp @@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest5) boost::numeric_cast(buffer.size()), numberOfBytesWritten); BOOST_CHECK(result == TimelinePacketStatus::Ok); - BOOST_CHECK(numberOfBytesWritten == 32); + BOOST_CHECK(numberOfBytesWritten == 36); unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); @@ -109,7 +109,12 @@ BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest5) uint32_t sequenceNumbered = (packetHeaderWord1 >> 24) & 0x00000001; uint32_t dataLength = (packetHeaderWord1 >> 0) & 0x00FFFFFF; BOOST_CHECK(sequenceNumbered == 0); - BOOST_CHECK(dataLength == 24); + BOOST_CHECK(dataLength == 28); + + // Check decl_Id + offset += uint32_t_size; + uint32_t decl_Id = ReadUint32(buffer.data(), offset); + BOOST_CHECK(decl_Id == uint32_t(0)); // Check the profiling GUID offset += uint32_t_size; @@ -258,10 +263,6 @@ BOOST_AUTO_TEST_CASE(TimelineMessageDirectoryPacketTest3) swTraceDeclNameLength - 1) == 0); // The length of the label } -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(TimelineEntityTests) - BOOST_AUTO_TEST_CASE(TimelineEntityPacketTest1) { const uint64_t profilingGuid = 123456u; @@ -313,7 +314,7 @@ BOOST_AUTO_TEST_CASE(TimelineEntityPacketTest4) boost::numeric_cast(buffer.size()), numberOfBytesWritten); BOOST_CHECK(result == TimelinePacketStatus::Ok); - BOOST_CHECK(numberOfBytesWritten == 16); + BOOST_CHECK(numberOfBytesWritten == 20); unsigned int uint32_t_size = sizeof(uint32_t); @@ -336,17 +337,17 @@ BOOST_AUTO_TEST_CASE(TimelineEntityPacketTest4) BOOST_CHECK(sequenceNumbered == 0); BOOST_CHECK(dataLength == 8); + // Check decl_Id + offset += uint32_t_size; + uint32_t decl_Id = ReadUint32(buffer.data(), offset); + BOOST_CHECK(decl_Id == uint32_t(1)); + // Check the profiling GUID offset += uint32_t_size; uint64_t readProfilingGuid = ReadUint64(buffer.data(), offset); BOOST_CHECK(readProfilingGuid == profilingGuid); } -BOOST_AUTO_TEST_SUITE_END() - - -BOOST_AUTO_TEST_SUITE(TimelineEventClassTests) - BOOST_AUTO_TEST_CASE(TimelineEventClassTest1) { const uint64_t profilingGuid = 123456u; @@ -432,4 +433,4 @@ BOOST_AUTO_TEST_CASE(TimelineEventClassTest4) BOOST_CHECK(readProfilingGuid == profilingGuid); } -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END() // TimelinePacketTests \ No newline at end of file -- cgit v1.2.1