aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAron Virginas-Tar <Aron.Virginas-Tar@arm.com>2019-09-24 18:24:47 +0100
committerÁron Virginás-Tar <aron.virginas-tar@arm.com>2019-09-27 15:48:18 +0000
commitdfa1477f8144c8ae933f949f0cc6ab70b6ba372d (patch)
tree261804170b87e5d37633c276b7709f69868ff8e7 /src
parent3a7d3a70d99cbe22f5e4711d5dbbea2a245da7ed (diff)
downloadarmnn-dfa1477f8144c8ae933f949f0cc6ab70b6ba372d.tar.gz
IVGCVSW-3442 Add ProfilingConnectionDumpToFileDecorator
Signed-off-by: Aron Virginas-Tar <Aron.Virginas-Tar@arm.com> Change-Id: I3a8313287c69268ae02b6a65103363e9fbba6b4a
Diffstat (limited to 'src')
-rw-r--r--src/profiling/ProfilingConnectionDumpToFileDecorator.cpp155
-rw-r--r--src/profiling/ProfilingConnectionDumpToFileDecorator.hpp81
-rw-r--r--src/profiling/test/ProfilingConnectionDumpToFileDecoratorTests.cpp177
3 files changed, 413 insertions, 0 deletions
diff --git a/src/profiling/ProfilingConnectionDumpToFileDecorator.cpp b/src/profiling/ProfilingConnectionDumpToFileDecorator.cpp
new file mode 100644
index 0000000000..b5400f2a2e
--- /dev/null
+++ b/src/profiling/ProfilingConnectionDumpToFileDecorator.cpp
@@ -0,0 +1,155 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "ProfilingConnectionDumpToFileDecorator.hpp"
+
+#include <armnn/Exceptions.hpp>
+
+#include <fstream>
+
+namespace armnn
+{
+
+namespace profiling
+{
+
+ProfilingConnectionDumpToFileDecorator::ProfilingConnectionDumpToFileDecorator(
+ std::unique_ptr<IProfilingConnection> connection,
+ const Settings& settings)
+ : m_Connection(std::move(connection))
+ , m_Settings(settings)
+{
+ if (!m_Connection)
+ {
+ throw InvalidArgumentException("Connection cannot be nullptr");
+ }
+}
+
+ProfilingConnectionDumpToFileDecorator::~ProfilingConnectionDumpToFileDecorator()
+{
+ Close();
+}
+
+bool ProfilingConnectionDumpToFileDecorator::IsOpen()
+{
+ return m_Connection->IsOpen();
+}
+
+void ProfilingConnectionDumpToFileDecorator::Close()
+{
+ m_IncomingDumpFileStream.close();
+ m_OutgoingDumpFileStream.close();
+ m_Connection->Close();
+}
+
+bool ProfilingConnectionDumpToFileDecorator::WritePacket(const unsigned char* buffer, uint32_t length)
+{
+ bool success = true;
+ if (m_Settings.m_DumpOutgoing)
+ {
+ success &= DumpOutgoingToFile(reinterpret_cast<const char*>(buffer), length);
+ }
+ success &= m_Connection->WritePacket(buffer, length);
+ return success;
+}
+
+Packet ProfilingConnectionDumpToFileDecorator::ReadPacket(uint32_t timeout)
+{
+ Packet packet = m_Connection->ReadPacket(timeout);
+ if (m_Settings.m_DumpIncoming)
+ {
+ DumpIncomingToFile(packet);
+ }
+ return packet;
+}
+
+bool ProfilingConnectionDumpToFileDecorator::OpenIncomingDumpFile()
+{
+ m_IncomingDumpFileStream.open(m_Settings.m_IncomingDumpFileName, std::ios::out | std::ios::binary);
+ return m_IncomingDumpFileStream.is_open();
+}
+
+bool ProfilingConnectionDumpToFileDecorator::OpenOutgoingDumpFile()
+{
+ m_OutgoingDumpFileStream.open(m_Settings.m_OutgoingDumpFileName, std::ios::out | std::ios::binary);
+ return m_OutgoingDumpFileStream.is_open();
+}
+
+
+/// Dumps incoming data into the file specified by m_Settings.m_IncomingDumpFileName.
+/// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored,
+/// i.e. the method will not throw an exception if it encounters an error while trying
+/// to write the data into the specified file.
+/// @param packet data packet to write
+/// @return nothing
+void ProfilingConnectionDumpToFileDecorator::DumpIncomingToFile(const Packet& packet)
+{
+ bool success = true;
+ if (!m_IncomingDumpFileStream.is_open())
+ {
+ // attempt to open dump file
+ success &= OpenIncomingDumpFile();
+ if (!(success || m_Settings.m_IgnoreFileErrors))
+ {
+ Fail("Failed to open \"" + m_Settings.m_IncomingDumpFileName + "\" for writing");
+ }
+ }
+
+ // attempt to write binary data from packet
+ const unsigned int header = packet.GetHeader();
+ const unsigned int packetLength = packet.GetLength();
+
+ m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&header), sizeof header);
+ m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&packetLength), sizeof packetLength);
+ m_IncomingDumpFileStream.write(packet.GetData(), packetLength);
+
+ success &= m_IncomingDumpFileStream.good();
+ if (!(success || m_Settings.m_IgnoreFileErrors))
+ {
+ Fail("Error writing incoming packet of " + std::to_string(packetLength) + " bytes");
+ }
+}
+
+/// Dumps outgoing data into the file specified by m_Settings.m_OutgoingDumpFileName.
+/// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored,
+/// i.e. the method will not throw an exception if it encounters an error while trying
+/// to write the data into the specified file. However, the return value will still
+/// signal if the write has not been completed succesfully.
+/// @param buffer pointer to data to write
+/// @param length number of bytes to write
+/// @return true if write successful, false otherwise
+bool ProfilingConnectionDumpToFileDecorator::DumpOutgoingToFile(const char* buffer, uint32_t length)
+{
+ bool success = true;
+ if (!m_OutgoingDumpFileStream.is_open())
+ {
+ // attempt to open dump file
+ success &= OpenOutgoingDumpFile();
+ if (!(success || m_Settings.m_IgnoreFileErrors))
+ {
+ Fail("Failed to open \"" + m_Settings.m_OutgoingDumpFileName + "\" for writing");
+ }
+ }
+
+ // attempt to write binary data
+ m_OutgoingDumpFileStream.write(buffer, length);
+ success &= m_OutgoingDumpFileStream.good();
+ if (!(success || m_Settings.m_IgnoreFileErrors))
+ {
+ Fail("Error writing outgoing packet of " + std::to_string(length) + " bytes");
+ }
+
+ return success;
+}
+
+void ProfilingConnectionDumpToFileDecorator::Fail(const std::string& errorMessage)
+{
+ Close();
+ throw RuntimeException(errorMessage);
+}
+
+} // namespace profiling
+
+} // namespace armnn
diff --git a/src/profiling/ProfilingConnectionDumpToFileDecorator.hpp b/src/profiling/ProfilingConnectionDumpToFileDecorator.hpp
new file mode 100644
index 0000000000..95dbe55641
--- /dev/null
+++ b/src/profiling/ProfilingConnectionDumpToFileDecorator.hpp
@@ -0,0 +1,81 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include "IProfilingConnection.hpp"
+
+#include <armnn/Optional.hpp>
+
+#include <fstream>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace armnn
+{
+
+namespace profiling
+{
+
+class ProfilingConnectionDumpToFileDecorator : public IProfilingConnection
+{
+public:
+ struct Settings
+ {
+ Settings(const std::string& incomingDumpFileName = "",
+ const std::string& outgoingDumpFileName = "",
+ bool ignoreFileErrors = true)
+ : m_IncomingDumpFileName(incomingDumpFileName)
+ , m_OutgoingDumpFileName(outgoingDumpFileName)
+ , m_DumpIncoming(!incomingDumpFileName.empty())
+ , m_DumpOutgoing(!outgoingDumpFileName.empty())
+ , m_IgnoreFileErrors(ignoreFileErrors)
+ {}
+
+ ~Settings() = default;
+
+ std::string m_IncomingDumpFileName;
+ std::string m_OutgoingDumpFileName;
+ bool m_DumpIncoming;
+ bool m_DumpOutgoing;
+ bool m_IgnoreFileErrors;
+ };
+
+ ProfilingConnectionDumpToFileDecorator(std::unique_ptr<IProfilingConnection> connection,
+ const Settings& settings);
+
+ ~ProfilingConnectionDumpToFileDecorator();
+
+ bool IsOpen() override;
+
+ void Close() override;
+
+ bool WritePacket(const unsigned char* buffer, uint32_t length) override;
+
+ Packet ReadPacket(uint32_t timeout) override;
+
+private:
+ bool OpenIncomingDumpFile();
+
+ bool OpenOutgoingDumpFile();
+
+ void DumpIncomingToFile(const Packet& packet);
+
+ bool DumpOutgoingToFile(const char* buffer, uint32_t length);
+
+ void Fail(const std::string& errorMessage);
+
+ std::unique_ptr<IProfilingConnection> m_Connection;
+ Settings m_Settings;
+ std::ofstream m_IncomingDumpFileStream;
+ std::ofstream m_OutgoingDumpFileStream;
+};
+
+using ProfilingConnectionDumpToFileDecoratorSettings = ProfilingConnectionDumpToFileDecorator::Settings;
+
+} // namespace profiling
+
+} // namespace armnn
diff --git a/src/profiling/test/ProfilingConnectionDumpToFileDecoratorTests.cpp b/src/profiling/test/ProfilingConnectionDumpToFileDecoratorTests.cpp
new file mode 100644
index 0000000000..3e06cb353b
--- /dev/null
+++ b/src/profiling/test/ProfilingConnectionDumpToFileDecoratorTests.cpp
@@ -0,0 +1,177 @@
+//
+// Copyright © 2019 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "../ProfilingConnectionDumpToFileDecorator.hpp"
+
+#include <fstream>
+#include <sstream>
+
+#include <boost/core/ignore_unused.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+#include <boost/test/unit_test.hpp>
+
+#if defined(__ANDROID__)
+#define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/data/local/tmp"
+#else
+#define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/tmp"
+#endif
+
+using namespace armnn::profiling;
+
+namespace
+{
+
+const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' };
+const uint32_t g_DataLength = boost::numeric_cast<uint32_t>(g_Data.size());
+const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data());
+
+class DummyProfilingConnection : public IProfilingConnection
+{
+public:
+ DummyProfilingConnection()
+ : m_Open(true)
+ , m_PacketData(std::make_unique<char[]>(g_DataLength))
+ {
+ // populate packet data and construct packet
+ std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength);
+ m_Packet = std::make_unique<Packet>(0u, g_DataLength, m_PacketData);
+ }
+
+ ~DummyProfilingConnection() = default;
+
+ bool IsOpen() override
+ {
+ return m_Open;
+ }
+
+ void Close() override
+ {
+ m_Open = false;
+ }
+
+ bool WritePacket(const unsigned char* buffer, uint32_t length) override
+ {
+ boost::ignore_unused(buffer);
+ boost::ignore_unused(length);
+ return true;
+ }
+
+ Packet ReadPacket(uint32_t timeout) override
+ {
+ boost::ignore_unused(timeout);
+ return std::move(*m_Packet);
+ }
+
+private:
+ bool m_Open;
+ std::unique_ptr<char[]> m_PacketData;
+ std::unique_ptr<Packet> m_Packet;
+};
+
+std::vector<char> ReadDumpFile(const std::string& dumpFileName)
+{
+ std::ifstream input(dumpFileName, std::ios::binary);
+ return std::vector<char>(std::istreambuf_iterator<char>(input), {});
+}
+
+} // anonymous namespace
+
+BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests)
+
+BOOST_AUTO_TEST_CASE(CheckSettings)
+{
+ ProfilingConnectionDumpToFileDecoratorSettings settings0("", "");
+ BOOST_CHECK(settings0.m_DumpIncoming == false);
+ BOOST_CHECK(settings0.m_DumpOutgoing == false);
+
+ ProfilingConnectionDumpToFileDecoratorSettings settings1("incomingDumpFile.dat", "");
+ BOOST_CHECK(settings1.m_DumpIncoming == true);
+ BOOST_CHECK(settings1.m_DumpOutgoing == false);
+
+ ProfilingConnectionDumpToFileDecoratorSettings settings2("", "outgoingDumpFile.dat");
+ BOOST_CHECK(settings2.m_DumpIncoming == false);
+ BOOST_CHECK(settings2.m_DumpOutgoing == true);
+
+ ProfilingConnectionDumpToFileDecoratorSettings settings3("incomingDumpFile.dat", "outgoingDumpFile.dat");
+ BOOST_CHECK(settings3.m_DumpIncoming == true);
+ BOOST_CHECK(settings3.m_DumpOutgoing == true);
+}
+
+BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile)
+{
+ ProfilingConnectionDumpToFileDecoratorSettings settings("/", "", false);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+ BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException);
+}
+
+BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors)
+{
+ ProfilingConnectionDumpToFileDecoratorSettings settings("/", "", true);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+ BOOST_CHECK_NO_THROW(decorator.ReadPacket(0));
+}
+
+BOOST_AUTO_TEST_CASE(DumpIncomingValidFile)
+{
+ std::stringstream fileName;
+ fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file_incoming.dat";
+
+ ProfilingConnectionDumpToFileDecoratorSettings settings(fileName.str(), "", false);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+
+ // NOTE: unique_ptr is needed here because operator=() is deleted for Packet
+ std::unique_ptr<Packet> packet;
+ BOOST_CHECK_NO_THROW(packet = std::make_unique<Packet>(decorator.ReadPacket(0)));
+
+ decorator.Close();
+
+ std::vector<char> data = ReadDumpFile(settings.m_IncomingDumpFileName);
+ const char* packetData = packet->GetData();
+
+ // check if the data read back from the dump file matches the original
+ constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length
+ int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength);
+ BOOST_CHECK(diff == 0);
+}
+
+BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile)
+{
+ ProfilingConnectionDumpToFileDecoratorSettings settings("", "/", false);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+ BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException);
+}
+
+BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors)
+{
+ ProfilingConnectionDumpToFileDecoratorSettings settings("", "/", true);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+ BOOST_CHECK_NO_THROW(decorator.WritePacket(g_DataPtr, g_DataLength));
+
+ bool success = decorator.WritePacket(g_DataPtr, g_DataLength);
+ BOOST_CHECK(!success);
+}
+
+BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile)
+{
+ std::stringstream fileName;
+ fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file.dat";
+
+ ProfilingConnectionDumpToFileDecoratorSettings settings("", fileName.str(), false);
+ ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings);
+
+ bool success = false;
+ BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
+ BOOST_CHECK(success);
+
+ decorator.Close();
+
+ std::vector<char> data = ReadDumpFile(settings.m_OutgoingDumpFileName);
+
+ // check if the data read back from the dump file matches the original
+ int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength);
+ BOOST_CHECK(diff == 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()