From 1b941728caa2cd7e2148d0872a6b7dda4947b641 Mon Sep 17 00:00:00 2001 From: Ferran Balaguer Date: Wed, 28 Aug 2019 16:57:18 +0100 Subject: IVGCVSW-3436 Create the Periodic Counter Selection Command Handler Change-Id: Ia6fe19db5aebe82bb00dcbab17e16633befda0a5 Signed-off-by: Ferran Balaguer --- src/profiling/Packet.cpp | 2 +- src/profiling/Packet.hpp | 2 +- .../PeriodicCounterSelectionCommandHandler.cpp | 70 +++++++++++++ .../PeriodicCounterSelectionCommandHandler.hpp | 50 ++++++++++ src/profiling/test/ProfilingTests.cpp | 108 +++++++++++++++++++++ src/profiling/test/SendCounterPacketTests.cpp | 90 +---------------- src/profiling/test/SendCounterPacketTests.hpp | 101 +++++++++++++++++++ 7 files changed, 334 insertions(+), 89 deletions(-) create mode 100644 src/profiling/PeriodicCounterSelectionCommandHandler.cpp create mode 100644 src/profiling/PeriodicCounterSelectionCommandHandler.hpp create mode 100644 src/profiling/test/SendCounterPacketTests.hpp (limited to 'src/profiling') diff --git a/src/profiling/Packet.cpp b/src/profiling/Packet.cpp index d0650a2aff..44d5ac19e9 100644 --- a/src/profiling/Packet.cpp +++ b/src/profiling/Packet.cpp @@ -31,7 +31,7 @@ std::uint32_t Packet::GetLength() const return m_Length; } -const char* Packet::GetData() +const char* Packet::GetData() const { return m_Data; } diff --git a/src/profiling/Packet.hpp b/src/profiling/Packet.hpp index b350f7c2ca..c5e7f3c029 100644 --- a/src/profiling/Packet.hpp +++ b/src/profiling/Packet.hpp @@ -35,7 +35,7 @@ public: uint32_t GetPacketFamily() const; uint32_t GetPacketId() const; uint32_t GetLength() const; - const char* GetData(); + const char* GetData() const; uint32_t GetPacketClass() const; uint32_t GetPacketType() const; diff --git a/src/profiling/PeriodicCounterSelectionCommandHandler.cpp b/src/profiling/PeriodicCounterSelectionCommandHandler.cpp new file mode 100644 index 0000000000..9be37fcfd2 --- /dev/null +++ b/src/profiling/PeriodicCounterSelectionCommandHandler.cpp @@ -0,0 +1,70 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "PeriodicCounterSelectionCommandHandler.hpp" +#include "ProfilingUtils.hpp" + +#include + +namespace armnn +{ + +namespace profiling +{ + +using namespace std; +using boost::numeric_cast; + +void PeriodicCounterSelectionCommandHandler::ParseData(const Packet& packet, CaptureData& captureData) +{ + std::vector counterIds; + uint32_t sizeOfUint32 = numeric_cast(sizeof(uint32_t)); + uint32_t sizeOfUint16 = numeric_cast(sizeof(uint16_t)); + uint32_t offset = 0; + + if (packet.GetLength() > 0) + { + if (packet.GetLength() >= 4) + { + captureData.SetCapturePeriod(ReadUint32(reinterpret_cast(packet.GetData()), offset)); + + unsigned int counters = (packet.GetLength() - 4) / 2; + + if (counters > 0) + { + counterIds.reserve(counters); + offset += sizeOfUint32; + for(unsigned int pos = 0; pos < counters; ++pos) + { + counterIds.emplace_back(ReadUint16(reinterpret_cast(packet.GetData()), + offset)); + offset += sizeOfUint16; + } + } + + captureData.SetCounterIds(counterIds); + } + } +} + +void PeriodicCounterSelectionCommandHandler::operator()(const Packet& packet) +{ + CaptureData captureData; + + ParseData(packet, captureData); + + vector counterIds = captureData.GetCounterIds(); + + m_CaptureDataHolder.SetCaptureData(captureData.GetCapturePeriod(), counterIds); + + m_CaptureThread.Start(); + + // Write packet to Counter Stream Buffer + m_SendCounterPacket.SendPeriodicCounterSelectionPacket(captureData.GetCapturePeriod(), captureData.GetCounterIds()); +} + +} // namespace profiling + +} // namespace armnn \ No newline at end of file diff --git a/src/profiling/PeriodicCounterSelectionCommandHandler.hpp b/src/profiling/PeriodicCounterSelectionCommandHandler.hpp new file mode 100644 index 0000000000..e247e7773f --- /dev/null +++ b/src/profiling/PeriodicCounterSelectionCommandHandler.hpp @@ -0,0 +1,50 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "Packet.hpp" +#include "CommandHandlerFunctor.hpp" +#include "Holder.hpp" +#include "SendCounterPacket.hpp" +#include "IPeriodicCounterCapture.hpp" + +#include +#include +#include + +namespace armnn +{ + +namespace profiling +{ + +class PeriodicCounterSelectionCommandHandler : public CommandHandlerFunctor +{ + +public: + PeriodicCounterSelectionCommandHandler(uint32_t packetId, uint32_t version, Holder& captureDataHolder, + IPeriodicCounterCapture& captureThread, + ISendCounterPacket& sendCounterPacket) + : CommandHandlerFunctor(packetId, version), + m_CaptureDataHolder(captureDataHolder), + m_CaptureThread(captureThread), + m_SendCounterPacket(sendCounterPacket) + {} + + void operator()(const Packet& packet) override; + + +private: + Holder& m_CaptureDataHolder; + IPeriodicCounterCapture& m_CaptureThread; + ISendCounterPacket& m_SendCounterPacket; + void ParseData(const Packet& packet, CaptureData& captureData); +}; + +} // namespace profiling + +} // namespace armnn + diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp index 25fae6c478..7f7fe1c0d2 100644 --- a/src/profiling/test/ProfilingTests.cpp +++ b/src/profiling/test/ProfilingTests.cpp @@ -12,12 +12,17 @@ #include "../PacketVersionResolver.hpp" #include "../ProfilingService.hpp" #include "../ProfilingStateMachine.hpp" +#include "../PeriodicCounterSelectionCommandHandler.hpp" #include "../ProfilingUtils.hpp" #include "../SocketProfilingConnection.hpp" +#include "../IPeriodicCounterCapture.hpp" +#include "SendCounterPacketTests.hpp" #include + #include +#include #include #include @@ -531,6 +536,109 @@ BOOST_AUTO_TEST_CASE(GetNextUidTest) BOOST_TEST(uid1 != uid2); } +BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData) +{ + using boost::numeric_cast; + + class TestCaptureThread : public IPeriodicCounterCapture + { + void Start() override {}; + }; + + const uint32_t packetId = 0x40000; + + uint32_t version = 1; + Holder holder; + TestCaptureThread captureThread; + MockBuffer mockBuffer(512); + SendCounterPacket sendCounterPacket(mockBuffer); + + uint32_t sizeOfUint32 = numeric_cast(sizeof(uint32_t)); + uint32_t sizeOfUint16 = numeric_cast(sizeof(uint16_t)); + + // Data with period and counters + uint32_t period1 = 10; + uint32_t dataLength1 = 8; + unsigned char data1[dataLength1]; + uint32_t offset = 0; + + WriteUint32(data1, offset, period1); + offset += sizeOfUint32; + WriteUint16(data1, offset, 4000); + offset += sizeOfUint16; + WriteUint16(data1, offset, 5000); + + Packet packetA(packetId, dataLength1, reinterpret_cast(data1)); + + PeriodicCounterSelectionCommandHandler commandHandler(packetId, version, holder, captureThread, + sendCounterPacket); + commandHandler(packetA); + + std::vector counterIds = holder.GetCaptureData().GetCounterIds(); + + BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1); + BOOST_TEST(counterIds.size() == 2); + BOOST_TEST(counterIds[0] == 4000); + BOOST_TEST(counterIds[1] == 5000); + + unsigned int size = 0; + + const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size); + + offset = 0; + + uint32_t headerWord0 = ReadUint32(readBuffer, offset); + offset += sizeOfUint32; + uint32_t headerWord1 = ReadUint32(readBuffer, offset); + offset += sizeOfUint32; + uint32_t period = ReadUint32(readBuffer, offset); + + BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family + BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id + BOOST_TEST(headerWord1 == 8); // data lenght + BOOST_TEST(period == 10); // capture period + + uint16_t counterId = 0; + offset += sizeOfUint32; + counterId = ReadUint16(readBuffer, offset); + BOOST_TEST(counterId == 4000); + offset += sizeOfUint16; + counterId = ReadUint16(readBuffer, offset); + BOOST_TEST(counterId == 5000); + + // Data with period only + uint32_t period2 = 11; + uint32_t dataLength2 = 4; + unsigned char data2[dataLength2]; + + WriteUint32(data2, 0, period2); + + Packet packetB(packetId, dataLength2, reinterpret_cast(data2)); + + commandHandler(packetB); + + counterIds = holder.GetCaptureData().GetCounterIds(); + + BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2); + BOOST_TEST(counterIds.size() == 0); + + readBuffer = mockBuffer.GetReadBuffer(size); + + offset = 0; + + headerWord0 = ReadUint32(readBuffer, offset); + offset += sizeOfUint32; + headerWord1 = ReadUint32(readBuffer, offset); + offset += sizeOfUint32; + period = ReadUint32(readBuffer, offset); + + BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family + BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id + BOOST_TEST(headerWord1 == 4); // data lenght + BOOST_TEST(period == 11); // capture period + +} + BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection) { // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist. diff --git a/src/profiling/test/SendCounterPacketTests.cpp b/src/profiling/test/SendCounterPacketTests.cpp index 626a5a6589..4c4620ffca 100644 --- a/src/profiling/test/SendCounterPacketTests.cpp +++ b/src/profiling/test/SendCounterPacketTests.cpp @@ -1,11 +1,12 @@ // -// Copyright © 2017 Arm Ltd. All rights reserved. +// Copyright © 2019 Arm Ltd. All rights reserved. // SPDX-License-Identifier: MIT // -#include "../SendCounterPacket.hpp" #include "../ProfilingUtils.hpp" #include "../EncodeVersion.hpp" +#include "../SendCounterPacket.hpp" +#include "SendCounterPacketTests.hpp" #include @@ -17,91 +18,6 @@ BOOST_AUTO_TEST_SUITE(SendCounterPacketTests) -using namespace armnn::profiling; - -class MockBuffer : public IBufferWrapper -{ -public: - MockBuffer(unsigned int size) - : m_BufferSize(size), - m_Buffer(std::make_unique(size)) {} - - unsigned char* Reserve(unsigned int requestedSize, unsigned int& reservedSize) override - { - if (requestedSize > m_BufferSize) - { - reservedSize = m_BufferSize; - } - else - { - reservedSize = requestedSize; - } - - return m_Buffer.get(); - } - - void Commit(unsigned int size) override {} - - const unsigned char* GetReadBuffer(unsigned int& size) override - { - size = static_cast(strlen(reinterpret_cast(m_Buffer.get())) + 1); - return m_Buffer.get(); - } - - void Release( unsigned int size) override {} - -private: - unsigned int m_BufferSize; - std::unique_ptr m_Buffer; -}; - -class MockSendCounterPacket : public ISendCounterPacket -{ -public: - MockSendCounterPacket(IBufferWrapper& sendBuffer) : m_Buffer(sendBuffer) {} - - void SendStreamMetaDataPacket() override - { - std::string message("SendStreamMetaDataPacket"); - unsigned int reserved = 0; - unsigned char* buffer = m_Buffer.Reserve(1024, reserved); - memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); - } - - void SendCounterDirectoryPacket(const CounterDirectory& counterDirectory) override - { - std::string message("SendCounterDirectoryPacket"); - unsigned int reserved = 0; - unsigned char* buffer = m_Buffer.Reserve(1024, reserved); - memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); - } - - void SendPeriodicCounterCapturePacket(uint64_t timestamp, - const std::vector>& values) override - { - std::string message("SendPeriodicCounterCapturePacket"); - unsigned int reserved = 0; - unsigned char* buffer = m_Buffer.Reserve(1024, reserved); - memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); - } - - void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, - const std::vector& selectedCounterIds) override - { - std::string message("SendPeriodicCounterSelectionPacket"); - unsigned int reserved = 0; - unsigned char* buffer = m_Buffer.Reserve(1024, reserved); - memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); - m_Buffer.Commit(reserved); - } - - void SetReadyToRead() override - {} - -private: - IBufferWrapper& m_Buffer; -}; - BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest) { unsigned int size = 0; diff --git a/src/profiling/test/SendCounterPacketTests.hpp b/src/profiling/test/SendCounterPacketTests.hpp new file mode 100644 index 0000000000..a22d02bd63 --- /dev/null +++ b/src/profiling/test/SendCounterPacketTests.hpp @@ -0,0 +1,101 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "../SendCounterPacket.hpp" +#include "../ProfilingUtils.hpp" + +#include + +#include + +#include +#include + +using namespace armnn::profiling; + +class MockBuffer : public IBufferWrapper +{ +public: + MockBuffer(unsigned int size) + : m_BufferSize(size), + m_Buffer(std::make_unique(size)) {} + + unsigned char* Reserve(unsigned int requestedSize, unsigned int& reservedSize) override + { + if (requestedSize > m_BufferSize) + { + reservedSize = m_BufferSize; + } + else + { + reservedSize = requestedSize; + } + + return m_Buffer.get(); + } + + void Commit(unsigned int size) override {} + + const unsigned char* GetReadBuffer(unsigned int& size) override + { + size = static_cast(strlen(reinterpret_cast(m_Buffer.get())) + 1); + return m_Buffer.get(); + } + + void Release( unsigned int size) override {} + +private: + unsigned int m_BufferSize; + std::unique_ptr m_Buffer; +}; + +class MockSendCounterPacket : public ISendCounterPacket +{ +public: + MockSendCounterPacket(IBufferWrapper& sendBuffer) : m_Buffer(sendBuffer) {} + + void SendStreamMetaDataPacket() override + { + std::string message("SendStreamMetaDataPacket"); + unsigned int reserved = 0; + unsigned char* buffer = m_Buffer.Reserve(1024, reserved); + memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); + } + + void SendCounterDirectoryPacket(const CounterDirectory& counterDirectory) override + { + std::string message("SendCounterDirectoryPacket"); + unsigned int reserved = 0; + unsigned char* buffer = m_Buffer.Reserve(1024, reserved); + memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); + } + + void SendPeriodicCounterCapturePacket(uint64_t timestamp, + const std::vector>& values) override + { + std::string message("SendPeriodicCounterCapturePacket"); + unsigned int reserved = 0; + unsigned char* buffer = m_Buffer.Reserve(1024, reserved); + memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); + } + + void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, + const std::vector& selectedCounterIds) override + { + std::string message("SendPeriodicCounterSelectionPacket"); + unsigned int reserved = 0; + unsigned char* buffer = m_Buffer.Reserve(1024, reserved); + memcpy(buffer, message.c_str(), static_cast(message.size()) + 1); + m_Buffer.Commit(reserved); + } + + void SetReadyToRead() override + {} + +private: + IBufferWrapper& m_Buffer; +}; -- cgit v1.2.1