IVGCVSW-4065 Use platform-specific thread id size in Timeline packets

 * Using std::thread::id as a general data type for thread id
 * Added new profiling util functions for reading/writing a thread id
   to/from a buffer
 * Fixed code and unit tests accordingly

Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Change-Id: I1aaa3bdb740c8a97010f655b1e9f7581b52e7aff
diff --git a/src/profiling/ISendTimelinePacket.hpp b/src/profiling/ISendTimelinePacket.hpp
index 34157b8..a954134 100644
--- a/src/profiling/ISendTimelinePacket.hpp
+++ b/src/profiling/ISendTimelinePacket.hpp
@@ -10,6 +10,7 @@
 #include <algorithm>
 #include <string>
 #include <vector>
+#include <thread>
 
 namespace armnn
 {
@@ -29,7 +30,9 @@
     virtual void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) = 0;
 
     /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
-    virtual void SendTimelineEventBinaryPacket(uint64_t timestamp, uint32_t threadId, uint64_t profilingGuid) = 0;
+    virtual void SendTimelineEventBinaryPacket(uint64_t timestamp,
+                                               std::thread::id threadId,
+                                               uint64_t profilingGuid) = 0;
 
     /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
     virtual void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) = 0;
diff --git a/src/profiling/ProfilingUtils.cpp b/src/profiling/ProfilingUtils.cpp
index ef72feb..93eca25 100644
--- a/src/profiling/ProfilingUtils.cpp
+++ b/src/profiling/ProfilingUtils.cpp
@@ -87,6 +87,13 @@
     return counterUids;
 }
 
+void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset,  const void* value, unsigned int valueSize)
+{
+    BOOST_ASSERT(packetBuffer);
+
+    WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
+}
+
 void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value)
 {
     BOOST_ASSERT(packetBuffer);
@@ -108,6 +115,17 @@
     WriteUint16(packetBuffer->GetWritableData(), offset, value);
 }
 
+void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
+{
+    BOOST_ASSERT(buffer);
+    BOOST_ASSERT(value);
+
+    for (unsigned int i = 0; i < valueSize; i++, offset++)
+    {
+        buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
+    }
+}
+
 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
 {
     BOOST_ASSERT(buffer);
@@ -140,6 +158,13 @@
     buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
 }
 
+void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
+{
+    BOOST_ASSERT(packetBuffer);
+
+    ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
+}
+
 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
 {
     BOOST_ASSERT(packetBuffer);
@@ -168,6 +193,17 @@
     return ReadUint8(packetBuffer->GetReadableData(), offset);
 }
 
+void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
+{
+    BOOST_ASSERT(buffer);
+    BOOST_ASSERT(outValue);
+
+    for (unsigned int i = 0; i < valueSize; i++, offset++)
+    {
+        outValue[i] = static_cast<uint8_t>(buffer[offset]);
+    }
+}
+
 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
 {
     BOOST_ASSERT(buffer);
@@ -769,7 +805,7 @@
 }
 
 TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
-                                                    uint32_t threadId,
+                                                    std::thread::id threadId,
                                                     uint64_t profilingGuid,
                                                     unsigned char* buffer,
                                                     unsigned int bufferSize,
@@ -787,6 +823,7 @@
     // 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;
@@ -794,7 +831,7 @@
     // Calculate the length of the data (in bytes)
     unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id
                                                  uint64_t_size + // Timestamp
-                                                 uint32_t_size + // Thread id
+                                                 threadId_size + // Thread id
                                                  uint64_t_size;  // Profiling GUID
 
     // Calculate the timeline binary packet size (in bytes)
@@ -824,8 +861,8 @@
     offset += uint32_t_size;
     WriteUint64(buffer, offset, timestamp); // Timestamp
     offset += uint64_t_size;
-    WriteUint32(buffer, offset, threadId); // Thread id
-    offset += uint32_t_size;
+    WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id
+    offset += threadId_size;
     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
     offset += uint64_t_size;
 
@@ -838,3 +875,13 @@
 } // namespace profiling
 
 } // namespace armnn
+
+namespace std
+{
+
+bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
+{
+    return std::memcmp(left.data(), &right, left.size()) == 0;
+}
+
+} // namespace std
diff --git a/src/profiling/ProfilingUtils.hpp b/src/profiling/ProfilingUtils.hpp
index 78ac43e..3e0a8a2 100644
--- a/src/profiling/ProfilingUtils.hpp
+++ b/src/profiling/ProfilingUtils.hpp
@@ -16,6 +16,7 @@
 #include <memory>
 #include <string>
 #include <vector>
+#include <thread>
 
 namespace armnn
 {
@@ -92,18 +93,24 @@
 
 std::vector<uint16_t> GetNextCounterUids(uint16_t cores);
 
+void WriteBytes(const IPacketBuffer& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize);
+
 void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value);
 
 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value);
 
 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value);
 
+void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize);
+
 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
 
 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value);
 
 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value);
 
+void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
+
 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset);
 
 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset);
@@ -112,6 +119,8 @@
 
 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset);
 
+void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
+
 uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset);
 
 uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset);
@@ -176,7 +185,7 @@
                                                          unsigned int& numberOfBytesWritten);
 
 TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
-                                                    uint32_t threadId,
+                                                    std::thread::id threadId,
                                                     uint64_t profilingGuid,
                                                     unsigned char* buffer,
                                                     unsigned int bufferSize,
@@ -190,3 +199,10 @@
 } // namespace profiling
 
 } // namespace armnn
+
+namespace std
+{
+
+bool operator==(const std::vector<uint8_t>& left, std::thread::id right);
+
+} // namespace std
diff --git a/src/profiling/SendTimelinePacket.cpp b/src/profiling/SendTimelinePacket.cpp
index a0fbf5c..707bfba 100644
--- a/src/profiling/SendTimelinePacket.cpp
+++ b/src/profiling/SendTimelinePacket.cpp
@@ -79,7 +79,9 @@
                               m_BufferSize);
 }
 
-void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp, uint32_t threadId, uint64_t profilingGuid)
+void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp,
+                                                       std::thread::id threadId,
+                                                       uint64_t profilingGuid)
 {
     FORWARD_WRITE_BINARY_FUNC(WriteTimelineEventBinaryPacket,
                               timestamp,
diff --git a/src/profiling/SendTimelinePacket.hpp b/src/profiling/SendTimelinePacket.hpp
index 2f9892f..77268a4 100644
--- a/src/profiling/SendTimelinePacket.hpp
+++ b/src/profiling/SendTimelinePacket.hpp
@@ -34,7 +34,7 @@
     void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override;
 
     /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
-    void SendTimelineEventBinaryPacket(uint64_t timestamp, uint32_t threadId, uint64_t profilingGuid) override;
+    void SendTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid) override;
 
     /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
     void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) override;
diff --git a/src/profiling/test/SendTimelinePacketTests.cpp b/src/profiling/test/SendTimelinePacketTests.cpp
index 53e52b5..60cda9a 100644
--- a/src/profiling/test/SendTimelinePacketTests.cpp
+++ b/src/profiling/test/SendTimelinePacketTests.cpp
@@ -207,6 +207,7 @@
 {
     unsigned int uint32_t_size = sizeof(uint32_t);
     unsigned int uint64_t_size = sizeof(uint64_t);
+    unsigned int threadId_size = sizeof(std::thread::id);
 
     MockBufferManager bufferManager(512);
     TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);
@@ -303,7 +304,7 @@
 
     // Send TimelineEventBinaryPacket
     const uint64_t timestamp = 456789u;
-    const uint32_t threadId = 654321u;
+    const std::thread::id threadId = std::this_thread::get_id();
     const uint64_t eventProfilingGuid = 123456u;
     sendTimelinePacket->SendTimelineEventBinaryPacket(timestamp, threadId, eventProfilingGuid);
 
@@ -333,7 +334,7 @@
     uint32_t eventBinaryPacketSequenceNumbered = (eventBinaryPacketHeaderWord1 >> 24) & 0x00000001;
     uint32_t eventBinaryPacketDataLength       = (eventBinaryPacketHeaderWord1 >>  0) & 0x00FFFFFF;
     BOOST_CHECK(eventBinaryPacketSequenceNumbered == 0);
-    BOOST_CHECK(eventBinaryPacketDataLength       == 24);
+    BOOST_CHECK(eventBinaryPacketDataLength       == 28);
 
     // Check the decl_id
     offset += uint32_t_size;
@@ -347,11 +348,12 @@
 
     // Check the thread id
     offset += uint64_t_size;
-    uint32_t readThreadId = ReadUint32(packetBuffer, offset);
+    std::vector<uint8_t> readThreadId(threadId_size, 0);
+    ReadBytes(packetBuffer, offset, threadId_size, readThreadId.data());
     BOOST_CHECK(readThreadId == threadId);
 
     // Check the profiling GUID
-    offset += uint32_t_size;
+    offset += threadId_size;
     readProfilingGuid = ReadUint64(packetBuffer, offset);
     BOOST_CHECK(readProfilingGuid == eventProfilingGuid);
 }
diff --git a/src/profiling/test/TimelinePacketTests.cpp b/src/profiling/test/TimelinePacketTests.cpp
index a9ba516..6a8aa85 100644
--- a/src/profiling/test/TimelinePacketTests.cpp
+++ b/src/profiling/test/TimelinePacketTests.cpp
@@ -790,7 +790,7 @@
 BOOST_AUTO_TEST_CASE(TimelineEventPacketTest1)
 {
     const uint64_t timestamp = 456789u;
-    const uint32_t threadId = 654321u;
+    const std::thread::id threadId = std::this_thread::get_id();
     const uint64_t profilingGuid = 123456u;
     unsigned int numberOfBytesWritten = 789u;
     TimelinePacketStatus result = WriteTimelineEventBinaryPacket(timestamp,
@@ -808,7 +808,7 @@
     std::vector<unsigned char> buffer(512, 0);
 
     const uint64_t timestamp = 456789u;
-    const uint32_t threadId = 654321u;
+    const std::thread::id threadId = std::this_thread::get_id();
     const uint64_t profilingGuid = 123456u;
     unsigned int numberOfBytesWritten = 789u;
     TimelinePacketStatus result = WriteTimelineEventBinaryPacket(timestamp,
@@ -826,7 +826,7 @@
     std::vector<unsigned char> buffer(10, 0);
 
     const uint64_t timestamp = 456789u;
-    const uint32_t threadId = 654321u;
+    const std::thread::id threadId = std::this_thread::get_id();
     const uint64_t profilingGuid = 123456u;
     unsigned int numberOfBytesWritten = 789u;
     TimelinePacketStatus result = WriteTimelineEventBinaryPacket(timestamp,
@@ -844,7 +844,7 @@
     std::vector<unsigned char> buffer(512, 0);
 
     const uint64_t timestamp = 456789u;
-    const uint32_t threadId = 654321u;
+    const std::thread::id threadId = std::this_thread::get_id();
     const uint64_t profilingGuid = 123456u;
     unsigned int numberOfBytesWritten = 789u;
     TimelinePacketStatus result = WriteTimelineEventBinaryPacket(timestamp,
@@ -854,10 +854,11 @@
                                                                  boost::numeric_cast<unsigned int>(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);
+    unsigned int threadId_size = sizeof(std::thread::id);
 
     // Check the packet header
     unsigned int offset = 0;
@@ -876,7 +877,7 @@
     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 the decl_id
     offset += uint32_t_size;
@@ -890,11 +891,12 @@
 
     // Check the thread id
     offset += uint64_t_size;
-    uint32_t readThreadId = ReadUint32(buffer.data(), offset);
+    std::vector<uint8_t> readThreadId(threadId_size, 0);
+    ReadBytes(buffer.data(), offset, threadId_size, readThreadId.data());
     BOOST_CHECK(readThreadId == threadId);
 
     // Check the profiling GUID
-    offset += uint32_t_size;
+    offset += threadId_size;
     uint64_t readProfilingGuid = ReadUint64(buffer.data(), offset);
     BOOST_CHECK(readProfilingGuid == profilingGuid);
 }