IVGCVSW-3927 Create the Timeline Label Binary Packet

 * Added a new utility function to create a Timeline Label
   Binary Packet and write it to a given buffer
 * Added new enumeration to be reused for subsequent utility
   functions
 * Added unit tests

Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Change-Id: Icbabefb9050f3f3b1a30082eabf875593378001f
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 94da6bf..7c0c53b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -600,6 +600,7 @@
         src/profiling/test/ProfilingConnectionDumpToFileDecoratorTests.cpp
         src/profiling/test/ProfilingTests.cpp
         src/profiling/test/SendCounterPacketTests.cpp
+        src/profiling/test/TimelinePacketTests.cpp
         )
 
     if(ARMNNREF)
diff --git a/src/profiling/ProfilingUtils.cpp b/src/profiling/ProfilingUtils.cpp
index 588fcc1..23386f8 100644
--- a/src/profiling/ProfilingUtils.cpp
+++ b/src/profiling/ProfilingUtils.cpp
@@ -239,6 +239,98 @@
     return name;
 }
 
+TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
+                                                    const std::string& label,
+                                                    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);
+
+    // Convert the label into a SWTrace string
+    std::vector<uint32_t> swTraceLabel;
+    bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
+    if (!result)
+    {
+        return TimelinePacketStatus::Error;
+    }
+
+    // Calculate the size of the SWTrace string label (in bytes)
+    unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
+
+    // Calculate the length of the data (in bytes)
+    unsigned int timelineLabelPacketDataLength = 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
+
+    // Check whether the timeline binary packet fits in the given buffer
+    if (timelineLabelPacketSize > bufferSize)
+    {
+        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<uint32_t>(timelineLabelPacketDataLength); // Profiling GUID + label
+    uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
+                                 ((dataLength       & 0x00FFFFFF) <<  0);
+
+    // 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);
+    offset += uint32_t_size;
+    WriteUint32(buffer, offset, packetHeaderWord1);
+    offset += uint32_t_size;
+
+    // Write the timeline binary packet payload to the buffer
+    WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
+    offset += uint64_t_size;
+    for (uint32_t swTraceLabelWord : swTraceLabel)
+    {
+        WriteUint32(buffer, offset, swTraceLabelWord); // Label
+        offset += uint32_t_size;
+    }
+
+    // Update the number of bytes written
+    numberOfBytesWritten = timelineLabelPacketSize;
+
+    return TimelinePacketStatus::Ok;
+}
+
 } // namespace profiling
 
 } // namespace armnn
diff --git a/src/profiling/ProfilingUtils.hpp b/src/profiling/ProfilingUtils.hpp
index 09b04f1..5afe6d8 100644
--- a/src/profiling/ProfilingUtils.hpp
+++ b/src/profiling/ProfilingUtils.hpp
@@ -119,6 +119,19 @@
 
 std::string GetProcessName();
 
+enum class TimelinePacketStatus
+{
+    Ok,
+    Error,
+    BufferExhaustion
+};
+
+TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
+                                                    const std::string& label,
+                                                    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
new file mode 100644
index 0000000..82d4dca
--- /dev/null
+++ b/src/profiling/test/TimelinePacketTests.cpp
@@ -0,0 +1,133 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include <ProfilingUtils.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+
+using namespace armnn::profiling;
+
+BOOST_AUTO_TEST_SUITE(TimelinePacketTests)
+
+BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest1)
+{
+    const uint64_t profilingGuid = 123456u;
+    const std::string label = "some label";
+    unsigned int numberOfBytesWritten = 789u;
+    TimelinePacketStatus result = WriteTimelineLabelBinaryPacket(profilingGuid,
+                                                                 label,
+                                                                 nullptr,
+                                                                 512u,
+                                                                 numberOfBytesWritten);
+    BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion);
+    BOOST_CHECK(numberOfBytesWritten == 0);
+}
+
+BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest2)
+{
+    std::vector<unsigned char> buffer(512, 0);
+
+    const uint64_t profilingGuid = 123456u;
+    const std::string label = "some label";
+    unsigned int numberOfBytesWritten = 789u;
+    TimelinePacketStatus result = WriteTimelineLabelBinaryPacket(profilingGuid,
+                                                                 label,
+                                                                 buffer.data(),
+                                                                 0,
+                                                                 numberOfBytesWritten);
+    BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion);
+    BOOST_CHECK(numberOfBytesWritten == 0);
+}
+
+BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest3)
+{
+    std::vector<unsigned char> buffer(10, 0);
+
+    const uint64_t profilingGuid = 123456u;
+    const std::string label = "some label";
+    unsigned int numberOfBytesWritten = 789u;
+    TimelinePacketStatus result = WriteTimelineLabelBinaryPacket(profilingGuid,
+                                                                 label,
+                                                                 buffer.data(),
+                                                                 boost::numeric_cast<unsigned int>(buffer.size()),
+                                                                 numberOfBytesWritten);
+    BOOST_CHECK(result == TimelinePacketStatus::BufferExhaustion);
+    BOOST_CHECK(numberOfBytesWritten == 0);
+}
+
+BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest4)
+{
+    std::vector<unsigned char> buffer(512, 0);
+
+    const uint64_t profilingGuid = 123456u;
+    const std::string label = "s0m€ l@b€l";
+    unsigned int numberOfBytesWritten = 789u;
+    TimelinePacketStatus result = WriteTimelineLabelBinaryPacket(profilingGuid,
+                                                                 label,
+                                                                 buffer.data(),
+                                                                 boost::numeric_cast<unsigned int>(buffer.size()),
+                                                                 numberOfBytesWritten);
+    BOOST_CHECK(result == TimelinePacketStatus::Error);
+    BOOST_CHECK(numberOfBytesWritten == 0);
+}
+
+BOOST_AUTO_TEST_CASE(TimelineLabelPacketTest5)
+{
+    std::vector<unsigned char> buffer(512, 0);
+
+    const uint64_t profilingGuid = 123456u;
+    const std::string label = "some label";
+    unsigned int numberOfBytesWritten = 789u;
+    TimelinePacketStatus result = WriteTimelineLabelBinaryPacket(profilingGuid,
+                                                                 label,
+                                                                 buffer.data(),
+                                                                 boost::numeric_cast<unsigned int>(buffer.size()),
+                                                                 numberOfBytesWritten);
+    BOOST_CHECK(result == TimelinePacketStatus::Ok);
+    BOOST_CHECK(numberOfBytesWritten == 32);
+
+    unsigned int uint32_t_size = sizeof(uint32_t);
+    unsigned int uint64_t_size = sizeof(uint64_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       == 24);
+
+    // Check the profiling GUID
+    offset += uint32_t_size;
+    uint64_t readProfilingGuid = ReadUint64(buffer.data(), offset);
+    BOOST_CHECK(readProfilingGuid == profilingGuid);
+
+    // Check the SWTrace label
+    offset += uint64_t_size;
+    uint32_t swTraceLabelLength = ReadUint32(buffer.data(), offset);
+    BOOST_CHECK(swTraceLabelLength == 11); // Label length including the null-terminator
+
+    offset += uint32_t_size;
+    BOOST_CHECK(std::memcmp(buffer.data() + offset,        // Offset to the label in the buffer
+                            label.data(),                  // The original label
+                            swTraceLabelLength - 1) == 0); // The length of the label
+
+    offset += swTraceLabelLength * uint32_t_size;
+    BOOST_CHECK(buffer[offset] == '\0'); // The null-terminator at the end of the SWTrace label
+}
+
+BOOST_AUTO_TEST_SUITE_END()