From 11f99b4e72a92051329b23af7ded759463380086 Mon Sep 17 00:00:00 2001 From: Francis Murtagh Date: Fri, 16 Aug 2019 11:28:52 +0100 Subject: IVGCVSW-3425 Create the Command Handler Functor base class Change-Id: I59ac9b32ac594161bdc5e1de2cdee02d79fc1992 Signed-off-by: Francis Murtagh --- CMakeLists.txt | 2 + src/profiling/CommandHandlerFunctor.cpp | 16 +++++++ src/profiling/CommandHandlerFunctor.hpp | 25 +++++++++++ src/profiling/test/ProfilingTests.cpp | 78 +++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/profiling/CommandHandlerFunctor.cpp create mode 100644 src/profiling/CommandHandlerFunctor.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b098a0ac90..a77b112bb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -411,6 +411,8 @@ list(APPEND armnn_sources src/armnn/optimizations/OptimizeInversePermutes.hpp src/armnn/optimizations/PermuteAsReshape.hpp src/armnn/optimizations/SquashEqualSiblings.hpp + src/profiling/CommandHandlerFunctor.cpp + src/profiling/CommandHandlerFunctor.hpp src/profiling/CommandHandlerKey.cpp src/profiling/CommandHandlerKey.hpp src/profiling/Packet.cpp diff --git a/src/profiling/CommandHandlerFunctor.cpp b/src/profiling/CommandHandlerFunctor.cpp new file mode 100644 index 0000000000..7289221db8 --- /dev/null +++ b/src/profiling/CommandHandlerFunctor.cpp @@ -0,0 +1,16 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "CommandHandlerFunctor.hpp" + +uint32_t CommandHandlerFunctor::GetPacketId() const +{ + return m_PacketId; +} + +uint32_t CommandHandlerFunctor::GetVersion() const +{ + return m_Version; +} \ No newline at end of file diff --git a/src/profiling/CommandHandlerFunctor.hpp b/src/profiling/CommandHandlerFunctor.hpp new file mode 100644 index 0000000000..fce6e3f8bb --- /dev/null +++ b/src/profiling/CommandHandlerFunctor.hpp @@ -0,0 +1,25 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "Packet.hpp" + +#include + +#pragma once + +class CommandHandlerFunctor +{ +public: + CommandHandlerFunctor(uint32_t packetId, uint32_t version) : m_PacketId(packetId), m_Version(version) {}; + + uint32_t GetPacketId() const; + uint32_t GetVersion() const; + + virtual void operator()(const Packet& packet) {}; + +private: + uint32_t m_PacketId; + uint32_t m_Version; +}; \ No newline at end of file diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp index 1ea8ab96a2..26cbfd7183 100644 --- a/src/profiling/test/ProfilingTests.cpp +++ b/src/profiling/test/ProfilingTests.cpp @@ -4,11 +4,13 @@ // #include "../CommandHandlerKey.hpp" +#include "../CommandHandlerFunctor.hpp" #include "../Packet.hpp" #include #include #include +#include BOOST_AUTO_TEST_SUITE(ExternalProfiling) @@ -78,4 +80,80 @@ BOOST_AUTO_TEST_CASE(CheckPacketClass) BOOST_CHECK(packetTest1.GetPacketClass() == 5); } +BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor) +{ + // Create Derived Classes + class TestFunctorA : public CommandHandlerFunctor + { + public: + using CommandHandlerFunctor::CommandHandlerFunctor; + + int GetCount() { return m_Count; } + + void operator()(const Packet& packet) override + { + m_Count++; + } + + private: + int m_Count = 0; + }; + + class TestFunctorB : public TestFunctorA + { + using TestFunctorA::TestFunctorA; + }; + + class TestFunctorC : public TestFunctorA + { + using TestFunctorA::TestFunctorA; + }; + + // Hard code the version as it will be the same during a single profiling session + uint32_t version = 1; + + TestFunctorA testFunctorA(461, version); + TestFunctorB testFunctorB(963, version); + TestFunctorC testFunctorC(983, version); + + CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion()); + CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion()); + CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion()); + + // Create the unwrapped map to simulate the Command Handler Registry + std::map registry; + + registry.insert(std::make_pair(keyB, &testFunctorB)); + registry.insert(std::make_pair(keyA, &testFunctorA)); + registry.insert(std::make_pair(keyC, &testFunctorC)); + + // Check the order of the map is correct + auto it = registry.begin(); + BOOST_CHECK(it->first==keyA); + it++; + BOOST_CHECK(it->first==keyB); + it++; + BOOST_CHECK(it->first==keyC); + + Packet packetA(500000000, 0, nullptr); + Packet packetB(600000000, 0, nullptr); + Packet packetC(400000000, 0, nullptr); + + // Check the correct operator of derived class is called + registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA); + BOOST_CHECK(testFunctorA.GetCount() == 1); + BOOST_CHECK(testFunctorB.GetCount() == 0); + BOOST_CHECK(testFunctorC.GetCount() == 0); + + registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB); + BOOST_CHECK(testFunctorA.GetCount() == 1); + BOOST_CHECK(testFunctorB.GetCount() == 1); + BOOST_CHECK(testFunctorC.GetCount() == 0); + + registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC); + BOOST_CHECK(testFunctorA.GetCount() == 1); + BOOST_CHECK(testFunctorB.GetCount() == 1); + BOOST_CHECK(testFunctorC.GetCount() == 1); +} + BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1