From d034e087d9b48a7541aa588b5cad1373693a8e95 Mon Sep 17 00:00:00 2001 From: Narumol Prangnawarat Date: Wed, 30 Oct 2019 12:48:31 +0000 Subject: [PATCH] IVGCVSW-4036 Add CreateNamedTypedEntity function * Add CreateNamedTypedEntity function * Add NameEntity function * Add TypeEntity function * Unit tests Change-Id: I3cc27157a0b41c8709b0a468f93b58b63a1ad8d1 Signed-off-by: Narumol Prangnawarat --- src/profiling/TimelineUtilityMethods.cpp | 39 ++++++ src/profiling/TimelineUtilityMethods.hpp | 9 +- src/profiling/test/TimelineUtilityMethodsTests.cpp | 141 +++++++++++++++++++++ 3 files changed, 188 insertions(+), 1 deletion(-) diff --git a/src/profiling/TimelineUtilityMethods.cpp b/src/profiling/TimelineUtilityMethods.cpp index b34b70f..429967f 100644 --- a/src/profiling/TimelineUtilityMethods.cpp +++ b/src/profiling/TimelineUtilityMethods.cpp @@ -34,6 +34,35 @@ void TimelineUtilityMethods::SendWellKnownLabelsAndEventClasses() m_SendTimelinePacket.SendTimelineEventClassBinaryPacket(LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS); } +ProfilingDynamicGuid TimelineUtilityMethods::CreateNamedTypedEntity(const std::string& name, const std::string& type) +{ + // Check that the entity name is valid + if (name.empty()) + { + throw InvalidArgumentException("Invalid entity name, the entity name cannot be empty"); + } + + // Check that the entity type is valid + if (type.empty()) + { + throw InvalidArgumentException("Invalid entity type, the entity type cannot be empty"); + } + + // Generate dynamic GUID of the entity + ProfilingDynamicGuid entityGuid = ProfilingService::Instance().NextGuid(); + + // Send Entity Binary Packet of the entity to the external profiling service + m_SendTimelinePacket.SendTimelineEntityBinaryPacket(entityGuid); + + // Create name entity and send the relationship of the entity with the given name + NameEntity(entityGuid, name); + + // Create type entity and send the relationship of the entity with the given type + TypeEntity(entityGuid, type); + + return entityGuid; +} + ProfilingStaticGuid TimelineUtilityMethods::DeclareLabel(const std::string& labelName) { // Check that the label name is valid @@ -85,6 +114,16 @@ void TimelineUtilityMethods::CreateTypedLabel(ProfilingGuid entityGuid, labelTypeGuid); } +void TimelineUtilityMethods::NameEntity(ProfilingGuid entityGuid, const std::string& name) +{ + CreateTypedLabel(entityGuid, name, LabelsAndEventClasses::NAME_GUID); +} + +void TimelineUtilityMethods::TypeEntity(ProfilingGuid entityGuid, const std::string& type) +{ + CreateTypedLabel(entityGuid, type, LabelsAndEventClasses::TYPE_GUID); +} + } // namespace profiling } // namespace armnn diff --git a/src/profiling/TimelineUtilityMethods.hpp b/src/profiling/TimelineUtilityMethods.hpp index 63e5c40..7d029e7 100644 --- a/src/profiling/TimelineUtilityMethods.hpp +++ b/src/profiling/TimelineUtilityMethods.hpp @@ -24,9 +24,16 @@ public: void SendWellKnownLabelsAndEventClasses(); - ProfilingStaticGuid DeclareLabel(const std::string& labelName); + ProfilingDynamicGuid CreateNamedTypedEntity(const std::string& name, const std::string& type); + void CreateTypedLabel(ProfilingGuid entityGuid, const std::string& entityName, ProfilingStaticGuid labelTypeGuid); + ProfilingStaticGuid DeclareLabel(const std::string& labelName); + + void NameEntity(ProfilingGuid entityGuid, const std::string& name); + + void TypeEntity(ProfilingGuid entityGuid, const std::string& type); + private: ISendTimelinePacket& m_SendTimelinePacket; }; diff --git a/src/profiling/test/TimelineUtilityMethodsTests.cpp b/src/profiling/test/TimelineUtilityMethodsTests.cpp index c98ed83..6c5ce63 100644 --- a/src/profiling/test/TimelineUtilityMethodsTests.cpp +++ b/src/profiling/test/TimelineUtilityMethodsTests.cpp @@ -231,6 +231,57 @@ void VerifyTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationsh offset += uint64_t_size; } +void VerifyTimelineEntityPacket(Optional guid, + const unsigned char* readableData, + unsigned int& offset) +{ + BOOST_ASSERT(readableData); + + // Utils + unsigned int uint32_t_size = sizeof(uint32_t); + unsigned int uint64_t_size = sizeof(uint64_t); + + // Reading TimelineEntityClassBinaryPacket + uint32_t entityBinaryPacketHeaderWord0 = ReadUint32(readableData, offset); + uint32_t entityBinaryPacketFamily = (entityBinaryPacketHeaderWord0 >> 26) & 0x0000003F; + uint32_t entityBinaryPacketClass = (entityBinaryPacketHeaderWord0 >> 19) & 0x0000007F; + uint32_t entityBinaryPacketType = (entityBinaryPacketHeaderWord0 >> 16) & 0x00000007; + uint32_t entityBinaryPacketStreamId = (entityBinaryPacketHeaderWord0 >> 0) & 0x00000007; + + BOOST_CHECK(entityBinaryPacketFamily == 1); + BOOST_CHECK(entityBinaryPacketClass == 0); + BOOST_CHECK(entityBinaryPacketType == 1); + BOOST_CHECK(entityBinaryPacketStreamId == 0); + + offset += uint32_t_size; + uint32_t entityBinaryPacketHeaderWord1 = ReadUint32(readableData, offset); + uint32_t entityBinaryPacketSequenceNumbered = (entityBinaryPacketHeaderWord1 >> 24) & 0x00000001; + uint32_t entityBinaryPacketDataLength = (entityBinaryPacketHeaderWord1 >> 0) & 0x00FFFFFF; + BOOST_CHECK(entityBinaryPacketSequenceNumbered == 0); + BOOST_CHECK(entityBinaryPacketDataLength == 8); + + // Check the decl_id + offset += uint32_t_size; + uint32_t entitytDecId = ReadUint32(readableData, offset); + + BOOST_CHECK(entitytDecId == uint32_t(1)); + + // Check the profiling GUID + offset += uint32_t_size; + uint64_t readProfilingGuid = ReadUint64(readableData, offset); + + if (guid.has_value()) + { + BOOST_CHECK(readProfilingGuid == guid.value()); + } + else + { + BOOST_CHECK(readProfilingGuid != ProfilingGuid(0)); + } + + offset += uint64_t_size; +} + } // Anonymous namespace BOOST_AUTO_TEST_SUITE(TimelineUtilityMethodsTests) @@ -365,4 +416,94 @@ BOOST_AUTO_TEST_CASE(DeclareLabelTest) //BOOST_CHECK(newLabelGuid == labelGuid); } +BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest) +{ + MockBufferManager mockBufferManager(1024); + SendTimelinePacket sendTimelinePacket(mockBufferManager); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + + // Invalid name + BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("", "Type"), InvalidArgumentException); + + // Invalid type + BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("Name", ""), InvalidArgumentException); + +} + +BOOST_AUTO_TEST_CASE(CreateNameTypeEntitylTest) +{ + MockBufferManager mockBufferManager(1024); + SendTimelinePacket sendTimelinePacket(mockBufferManager); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + + const std::string entityName = "Entity0"; + const std::string entityType = "Type0"; + + // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. + ProfilingService::Instance().NextGuid(); + + ProfilingDynamicGuid guid = timelineUtilityMethods.CreateNamedTypedEntity(entityName, entityType); + BOOST_CHECK(guid != ProfilingGuid(0)); + + // Commit all packets at once + sendTimelinePacket.Commit(); + + // Get the readable buffer + auto readableBuffer = mockBufferManager.GetReadableBuffer(); + BOOST_CHECK(readableBuffer != nullptr); + unsigned int size = readableBuffer->GetSize(); + BOOST_CHECK(size == 244); + const unsigned char* readableData = readableBuffer->GetReadableData(); + BOOST_CHECK(readableData != nullptr); + + // Utils + unsigned int offset = 0; + + // First packet sent: TimelineEntityBinaryPacket + VerifyTimelineEntityPacket(guid, readableData, offset); + + // Packets for Name Entity + // First packet sent: TimelineLabelBinaryPacket + VerifyTimelineLabelBinaryPacket(EmptyOptional(), entityName, readableData, offset); + + // Second packet sent: TimelineRelationshipBinaryPacket + VerifyTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, + EmptyOptional(), + EmptyOptional(), + EmptyOptional(), + readableData, + offset); + + // Third packet sent: TimelineRelationshipBinaryPacket + VerifyTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, + EmptyOptional(), + EmptyOptional(), + LabelsAndEventClasses::NAME_GUID, + readableData, + offset); + + // Packets for Type Entity + // First packet sent: TimelineLabelBinaryPacket + VerifyTimelineLabelBinaryPacket(EmptyOptional(), entityType, readableData, offset); + + // Second packet sent: TimelineRelationshipBinaryPacket + VerifyTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, + EmptyOptional(), + EmptyOptional(), + EmptyOptional(), + readableData, + offset); + + // Third packet sent: TimelineRelationshipBinaryPacket + VerifyTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, + EmptyOptional(), + EmptyOptional(), + LabelsAndEventClasses::TYPE_GUID, + readableData, + offset); + + // Mark the buffer as read + mockBufferManager.MarkRead(readableBuffer); +} + BOOST_AUTO_TEST_SUITE_END() -- 2.7.4