2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "../ProfilingConnectionDumpToFileDecorator.hpp"
7 #include <Filesystem.hpp>
9 #include <armnn/utility/IgnoreUnused.hpp>
10 #include <armnn/utility/NumericCast.hpp>
15 #include <boost/test/unit_test.hpp>
17 using namespace armnn::profiling;
22 const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' };
23 const uint32_t g_DataLength = armnn::numeric_cast<uint32_t>(g_Data.size());
24 const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data());
26 class DummyProfilingConnection : public IProfilingConnection
29 DummyProfilingConnection()
31 , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength))
33 // populate packet data and construct packet
34 std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength);
35 m_Packet = std::make_unique<arm::pipe::Packet>(0u, g_DataLength, m_PacketData);
38 ~DummyProfilingConnection() = default;
40 bool IsOpen() const override
50 bool WritePacket(const unsigned char* buffer, uint32_t length) override
52 armnn::IgnoreUnused(buffer);
53 armnn::IgnoreUnused(length);
57 arm::pipe::Packet ReadPacket(uint32_t timeout) override
59 armnn::IgnoreUnused(timeout);
60 return std::move(*m_Packet);
65 std::unique_ptr<unsigned char[]> m_PacketData;
66 std::unique_ptr<arm::pipe::Packet> m_Packet;
69 std::vector<char> ReadDumpFile(const std::string& dumpFileName)
71 std::ifstream input(dumpFileName, std::ios::binary);
72 return std::vector<char>(std::istreambuf_iterator<char>(input), {});
75 } // anonymous namespace
77 BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests)
79 BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile)
81 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
82 options.m_IncomingCaptureFile = "/";
83 options.m_OutgoingCaptureFile = "";
84 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
85 BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException);
88 BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors)
90 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
91 options.m_IncomingCaptureFile = "/";
92 options.m_OutgoingCaptureFile = "";
93 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
94 BOOST_CHECK_NO_THROW(decorator.ReadPacket(0));
97 BOOST_AUTO_TEST_CASE(DumpIncomingValidFile)
99 fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpIncomingValidFileTest-TempFile");
101 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
102 options.m_IncomingCaptureFile = fileName.string();
103 options.m_OutgoingCaptureFile = "";
105 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
107 // NOTE: unique_ptr is needed here because operator=() is deleted for Packet
108 std::unique_ptr<arm::pipe::Packet> packet;
109 BOOST_CHECK_NO_THROW(packet = std::make_unique<arm::pipe::Packet>(decorator.ReadPacket(0)));
113 std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile);
114 const char* packetData = reinterpret_cast<const char*>(packet->GetData());
116 // check if the data read back from the dump file matches the original
117 constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length
118 int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength);
119 BOOST_CHECK(diff == 0);
120 fs::remove(fileName);
123 BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile)
125 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
126 options.m_IncomingCaptureFile = "";
127 options.m_OutgoingCaptureFile = "/";
128 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
129 BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException);
132 BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors)
134 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
135 options.m_IncomingCaptureFile = "";
136 options.m_OutgoingCaptureFile = "/";
138 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
139 BOOST_CHECK_NO_THROW(decorator.WritePacket(g_DataPtr, g_DataLength));
141 bool success = decorator.WritePacket(g_DataPtr, g_DataLength);
142 BOOST_CHECK(!success);
145 BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile)
147 fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpOutgoingValidFileTest-TempFile");
149 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
150 options.m_IncomingCaptureFile = "";
151 options.m_OutgoingCaptureFile = fileName.string();
153 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
155 bool success = false;
156 BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
157 BOOST_CHECK(success);
161 std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile);
163 // check if the data read back from the dump file matches the original
164 int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength);
165 BOOST_CHECK(diff == 0);
166 fs::remove(fileName);
169 BOOST_AUTO_TEST_SUITE_END()