From: Colm Donelan Date: Tue, 12 May 2020 15:36:46 +0000 (+0100) Subject: IVGCVSW-4775 Centralizing definition of ThreadIdSize to fix MLCE-189 X-Git-Tag: submit/tizen/20200730.023729~67 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5bb3d8a1edeab3f4fdbeb9baf7922cc52bd815fb;p=platform%2Fupstream%2Farmnn.git IVGCVSW-4775 Centralizing definition of ThreadIdSize to fix MLCE-189 * Introduce a constant definition of the size of a POSIX thread ID. * Update all code to use the new constant definition. * Update all unit tests to use the new constant definition. Signed-off-by: Colm Donelan Change-Id: I836ab1a77ed13f774e66fd7b425923c24b9a6dab --- diff --git a/src/armnn/test/RuntimeTests.cpp b/src/armnn/test/RuntimeTests.cpp index 2fc4b50..c4a9626 100644 --- a/src/armnn/test/RuntimeTests.cpp +++ b/src/armnn/test/RuntimeTests.cpp @@ -835,8 +835,7 @@ BOOST_AUTO_TEST_CASE(ProfilingEnableCpuRef) // Validate inference data size = inferenceReadableBuffer->GetSize(); - unsigned int threadId_size = sizeof(std::thread::id); // Is platform dependent - BOOST_CHECK(size == 1208 + 8 * threadId_size); + BOOST_CHECK(size == 1208 + 8 * ThreadIdSize); readableData = inferenceReadableBuffer->GetReadableData(); BOOST_CHECK(readableData != nullptr); @@ -844,7 +843,7 @@ BOOST_AUTO_TEST_CASE(ProfilingEnableCpuRef) offset = 0; // Verify Header - VerifyTimelineHeaderBinary(readableData, offset, 1200 + 8 * threadId_size); + VerifyTimelineHeaderBinary(readableData, offset, 1200 + 8 * ThreadIdSize); // Inference timeline trace // Inference entity diff --git a/src/profiling/ProfilingUtils.cpp b/src/profiling/ProfilingUtils.cpp index 6d2565c..4e5fcf8 100644 --- a/src/profiling/ProfilingUtils.cpp +++ b/src/profiling/ProfilingUtils.cpp @@ -640,7 +640,6 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, unsigned int uint8_t_size = sizeof(uint8_t); unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // 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: @@ -719,7 +718,7 @@ TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, // Write the stream header uint8_t streamVersion = 4; uint8_t pointerBytes = boost::numeric_cast(uint64_t_size); // All GUIDs are uint64_t - uint8_t threadIdBytes = boost::numeric_cast(threadId_size); + uint8_t threadIdBytes = boost::numeric_cast(ThreadIdSize); switch (threadIdBytes) { case 4: // Typically Windows and Android @@ -813,7 +812,6 @@ TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, // Utils unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // decl_id of the timeline message uint32_t declId = 4; @@ -821,7 +819,7 @@ TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, // Calculate the length of the data (in bytes) unsigned int timelineEventDataLength = uint32_t_size + // decl_id uint64_t_size + // Timestamp - threadId_size + // Thread id + ThreadIdSize + // Thread id uint64_t_size; // Profiling GUID // Check whether the timeline binary packet fits in the given buffer @@ -838,8 +836,8 @@ TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, offset += uint32_t_size; WriteUint64(buffer, offset, timestamp); // Timestamp offset += uint64_t_size; - WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id - offset += threadId_size; + WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id + offset += ThreadIdSize; WriteUint64(buffer, offset, profilingGuid); // Profiling GUID offset += uint64_t_size; // Update the number of bytes written diff --git a/src/profiling/ProfilingUtils.hpp b/src/profiling/ProfilingUtils.hpp index 127534a..b9af456 100644 --- a/src/profiling/ProfilingUtils.hpp +++ b/src/profiling/ProfilingUtils.hpp @@ -28,6 +28,8 @@ namespace armnn namespace profiling { +constexpr unsigned int ThreadIdSize = sizeof(std::thread::id); // Is platform dependent + struct SwTraceHeader { uint8_t m_StreamVersion; diff --git a/src/profiling/test/ProfilingTestUtils.cpp b/src/profiling/test/ProfilingTestUtils.cpp index c138682..73dbf88 100644 --- a/src/profiling/test/ProfilingTestUtils.cpp +++ b/src/profiling/test/ProfilingTestUtils.cpp @@ -263,7 +263,6 @@ void VerifyTimelineEventBinaryPacket(Optional timestamp, // Utils unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // Reading TimelineEventBinaryPacket // Check the decl_id @@ -284,8 +283,8 @@ void VerifyTimelineEventBinaryPacket(Optional timestamp, // Check the thread id offset += uint64_t_size; - std::vector readThreadId(threadId_size, 0); - ReadBytes(readableData, offset, threadId_size, readThreadId.data()); + std::vector readThreadId(ThreadIdSize, 0); + ReadBytes(readableData, offset, ThreadIdSize, readThreadId.data()); if (threadId.has_value()) { BOOST_CHECK(readThreadId == threadId.value()); @@ -296,7 +295,7 @@ void VerifyTimelineEventBinaryPacket(Optional timestamp, } // Check the event GUID - offset += threadId_size; + offset += ThreadIdSize; uint64_t readEventGuid = ReadUint64(readableData, offset); if (eventGuid.has_value()) { @@ -936,8 +935,7 @@ void VerifyPostOptimisationStructureTestImpl(armnn::BackendId backendId) // Validate inference data size = inferenceReadableBuffer->GetSize(); - unsigned int threadId_size = sizeof(std::thread::id); // Is platform dependent - BOOST_CHECK(size == 1516 + 10 * threadId_size); + BOOST_CHECK(size == 1516 + 10 * ThreadIdSize); readableData = inferenceReadableBuffer->GetReadableData(); BOOST_CHECK(readableData != nullptr); @@ -945,7 +943,7 @@ void VerifyPostOptimisationStructureTestImpl(armnn::BackendId backendId) offset = 0; // Verify Header - VerifyTimelineHeaderBinary(readableData, offset, 1508 + 10 * threadId_size); + VerifyTimelineHeaderBinary(readableData, offset, 1508 + 10 * ThreadIdSize); // Inference timeline trace // Inference entity diff --git a/src/profiling/test/SendTimelinePacketTests.cpp b/src/profiling/test/SendTimelinePacketTests.cpp index 4a13ebf..6d5bf49 100644 --- a/src/profiling/test/SendTimelinePacketTests.cpp +++ b/src/profiling/test/SendTimelinePacketTests.cpp @@ -35,7 +35,6 @@ BOOST_AUTO_TEST_CASE(SendTimelineMessageDirectoryPackageTest) unsigned int uint8_t_size = sizeof(uint8_t); unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // Check the packet header unsigned int offset = 0; @@ -65,7 +64,7 @@ BOOST_AUTO_TEST_CASE(SendTimelineMessageDirectoryPackageTest) BOOST_CHECK(readPointerBytes == uint64_t_size); offset += uint8_t_size; uint8_t readThreadIdBytes = ReadUint8(packetBuffer, offset); - BOOST_CHECK(readThreadIdBytes == threadId_size); + BOOST_CHECK(readThreadIdBytes == ThreadIdSize); offset += uint8_t_size; uint32_t DeclCount = ReadUint32(packetBuffer, offset); @@ -211,7 +210,6 @@ BOOST_AUTO_TEST_CASE(SendEventClassAfterTimelineEntityPacketTest) { unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // Is platform dependent MockBufferManager bufferManager(512); TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager); @@ -338,7 +336,7 @@ BOOST_AUTO_TEST_CASE(SendEventClassAfterTimelineEntityPacketTest) uint32_t eventBinaryPacketSequenceNumbered = (eventBinaryPacketHeaderWord1 >> 24) & 0x00000001; uint32_t eventBinaryPacketDataLength = (eventBinaryPacketHeaderWord1 >> 0) & 0x00FFFFFF; BOOST_CHECK(eventBinaryPacketSequenceNumbered == 0); - BOOST_CHECK(eventBinaryPacketDataLength == 20 + threadId_size); + BOOST_CHECK(eventBinaryPacketDataLength == 20 + ThreadIdSize); // Check the decl_id offset += uint32_t_size; @@ -352,12 +350,12 @@ BOOST_AUTO_TEST_CASE(SendEventClassAfterTimelineEntityPacketTest) // Check the thread id offset += uint64_t_size; - std::vector readThreadId(threadId_size, 0); - ReadBytes(packetBuffer, offset, threadId_size, readThreadId.data()); + std::vector readThreadId(ThreadIdSize, 0); + ReadBytes(packetBuffer, offset, ThreadIdSize, readThreadId.data()); BOOST_CHECK(readThreadId == threadId); // Check the profiling GUID - offset += threadId_size; + offset += ThreadIdSize; readProfilingGuid = ReadUint64(packetBuffer, offset); BOOST_CHECK(readProfilingGuid == eventProfilingGuid); } diff --git a/src/profiling/test/TimelinePacketTests.cpp b/src/profiling/test/TimelinePacketTests.cpp index 5401712..e57ed2a 100644 --- a/src/profiling/test/TimelinePacketTests.cpp +++ b/src/profiling/test/TimelinePacketTests.cpp @@ -429,7 +429,6 @@ BOOST_AUTO_TEST_CASE(TimelineMessageDirectoryPacketTestFullConstruction) unsigned int uint8_t_size = sizeof(uint8_t); unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // Check the packet header unsigned int offset = 0; @@ -459,7 +458,7 @@ BOOST_AUTO_TEST_CASE(TimelineMessageDirectoryPacketTestFullConstruction) BOOST_CHECK(readPointerBytes == uint64_t_size); offset += uint8_t_size; uint8_t readThreadIdBytes = ReadUint8(buffer.data(), offset); - BOOST_CHECK(readThreadIdBytes == threadId_size); + BOOST_CHECK(readThreadIdBytes == ThreadIdSize); // Check the number of declarations offset += uint8_t_size; @@ -742,9 +741,7 @@ BOOST_AUTO_TEST_CASE(TimelineEventPacketTestFullConstructionOfData) unsigned int uint32_t_size = sizeof(uint32_t); unsigned int uint64_t_size = sizeof(uint64_t); - unsigned int threadId_size = sizeof(std::thread::id); // Is platform dependent - - BOOST_CHECK(numberOfBytesWritten == 20 + threadId_size); + BOOST_CHECK(numberOfBytesWritten == 20 + ThreadIdSize); unsigned int offset = 0; // Check the decl_id @@ -758,12 +755,12 @@ BOOST_AUTO_TEST_CASE(TimelineEventPacketTestFullConstructionOfData) // Check the thread id offset += uint64_t_size; - std::vector readThreadId(threadId_size, 0); - ReadBytes(buffer.data(), offset, threadId_size, readThreadId.data()); + std::vector readThreadId(ThreadIdSize, 0); + ReadBytes(buffer.data(), offset, ThreadIdSize, readThreadId.data()); BOOST_CHECK(readThreadId == threadId); // Check the profiling GUID - offset += threadId_size; + offset += ThreadIdSize; uint64_t readProfilingGuid = ReadUint64(buffer.data(), offset); BOOST_CHECK(readProfilingGuid == profilingGuid); } diff --git a/src/profiling/test/TimelineUtilityMethodsTests.cpp b/src/profiling/test/TimelineUtilityMethodsTests.cpp index caffcc7..388d38a 100644 --- a/src/profiling/test/TimelineUtilityMethodsTests.cpp +++ b/src/profiling/test/TimelineUtilityMethodsTests.cpp @@ -429,8 +429,8 @@ BOOST_AUTO_TEST_CASE(RecordEventTest) auto readableBuffer = mockBufferManager.GetReadableBuffer(); BOOST_CHECK(readableBuffer != nullptr); unsigned int size = readableBuffer->GetSize(); - unsigned int threadId_size = sizeof(std::thread::id); // Is platform dependent - BOOST_CHECK(size == 92 + threadId_size); + + BOOST_CHECK(size == 92 + ThreadIdSize); const unsigned char* readableData = readableBuffer->GetReadableData(); BOOST_CHECK(readableData != nullptr); @@ -439,7 +439,7 @@ BOOST_AUTO_TEST_CASE(RecordEventTest) unsigned int offset = 0; // Verify Header - VerifyTimelineHeaderBinary(readableData, offset, 84 + threadId_size); + VerifyTimelineHeaderBinary(readableData, offset, 84 + ThreadIdSize); // First dataset sent: TimelineEntityBinaryPacket VerifyTimelineEventBinaryPacket(EmptyOptional(), EmptyOptional(), EmptyOptional(), readableData, offset); diff --git a/src/timelineDecoder/tests/TimelineTests.cpp b/src/timelineDecoder/tests/TimelineTests.cpp index 1f55515..bff2536 100644 --- a/src/timelineDecoder/tests/TimelineTests.cpp +++ b/src/timelineDecoder/tests/TimelineTests.cpp @@ -73,7 +73,6 @@ BOOST_AUTO_TEST_CASE(TimelineDirectoryTest) uint32_t uint8_t_size = sizeof(uint8_t); uint32_t uint32_t_size = sizeof(uint32_t); uint32_t uint64_t_size = sizeof(uint64_t); - uint32_t threadId_size = sizeof(std::thread::id); profiling::BufferManager bufferManager(5); profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager); @@ -107,7 +106,7 @@ BOOST_AUTO_TEST_CASE(TimelineDirectoryTest) BOOST_CHECK(readPointerBytes == uint64_t_size); offset += uint8_t_size; uint8_t readThreadIdBytes = ReadUint8(packetBuffer, offset); - BOOST_CHECK(readThreadIdBytes == threadId_size); + BOOST_CHECK(readThreadIdBytes == armnn::profiling::ThreadIdSize); offset += uint8_t_size; uint32_t declarationSize = profiling::ReadUint32(packetBuffer, offset); @@ -144,7 +143,6 @@ BOOST_AUTO_TEST_CASE(TimelineDirectoryTest) BOOST_AUTO_TEST_CASE(TimelineCaptureTest) { - unsigned int threadIdSize = sizeof(std::thread::id); profiling::BufferManager bufferManager(50); profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager); @@ -158,7 +156,8 @@ BOOST_AUTO_TEST_CASE(TimelineCaptureTest) TimelineCaptureCommandHandler timelineCaptureCommandHandler( - 1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, threadIdSize); + 1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, + armnn::profiling::ThreadIdSize); using Status = ITimelineDecoder::TimelineStatus; BOOST_CHECK(timelineDecoder.SetEntityCallback(PushEntity) == Status::TimelineStatus_Success); @@ -175,16 +174,16 @@ BOOST_AUTO_TEST_CASE(TimelineCaptureTest) const std::thread::id threadId = std::this_thread::get_id(); // need to do a bit of work here to extract the value from threadId - unsigned char* uCharThreadId = new unsigned char[threadIdSize]();; + unsigned char* uCharThreadId = new unsigned char[armnn::profiling::ThreadIdSize]();; uint64_t uint64ThreadId; - profiling::WriteBytes(uCharThreadId, 0, &threadId, threadIdSize); + profiling::WriteBytes(uCharThreadId, 0, &threadId, armnn::profiling::ThreadIdSize); - if (threadIdSize == 4) + if (armnn::profiling::ThreadIdSize == 4) { uint64ThreadId = profiling::ReadUint32(uCharThreadId, 0); } - else if (threadIdSize == 8) + else if (armnn::profiling::ThreadIdSize == 8) { uint64ThreadId = profiling::ReadUint64(uCharThreadId, 0); } @@ -256,7 +255,6 @@ BOOST_AUTO_TEST_CASE(TimelineCaptureTest) BOOST_AUTO_TEST_CASE(TimelineCaptureTestMultipleStringsInBuffer) { - unsigned int threadIdSize = sizeof(std::thread::id); profiling::BufferManager bufferManager(50); profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager); @@ -269,7 +267,8 @@ BOOST_AUTO_TEST_CASE(TimelineCaptureTestMultipleStringsInBuffer) const TimelineDecoder::Model& model = timelineDecoder.GetModel(); TimelineCaptureCommandHandler timelineCaptureCommandHandler( - 1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, threadIdSize); + 1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, + armnn::profiling::ThreadIdSize); using Status = ITimelineDecoder::TimelineStatus; BOOST_CHECK(timelineDecoder.SetEntityCallback(PushEntity) == Status::TimelineStatus_Success); @@ -286,15 +285,16 @@ BOOST_AUTO_TEST_CASE(TimelineCaptureTestMultipleStringsInBuffer) const std::thread::id threadId = std::this_thread::get_id(); // need to do a bit of work here to extract the value from threadId - unsigned char* uCharThreadId = new unsigned char[threadIdSize]();; + unsigned char* uCharThreadId = new unsigned char[armnn::profiling::ThreadIdSize](); uint64_t uint64ThreadId; - profiling::WriteBytes(uCharThreadId, 0, &threadId, threadIdSize); + profiling::WriteBytes(uCharThreadId, 0, &threadId, armnn::profiling::ThreadIdSize); - if ( threadIdSize == 4 ) + if ( armnn::profiling::ThreadIdSize == 4 ) { uint64ThreadId = profiling::ReadUint32(uCharThreadId, 0); - } else if ( threadIdSize == 8 ) + } + else if ( armnn::profiling::ThreadIdSize == 8 ) { uint64ThreadId = profiling::ReadUint64(uCharThreadId, 0); }