1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "../ProfilingConnectionDumpToFileDecorator.hpp"
7 #include <Filesystem.hpp>
8 #include <Runtime.hpp>
9 #include <armnn/utility/IgnoreUnused.hpp>
10 #include <armnn/utility/NumericCast.hpp>
11
12 #include <fstream>
13 #include <sstream>
14
15 #include <boost/test/unit_test.hpp>
16
17 using namespace armnn::profiling;
18
19 namespace
20 {
21
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());
25
26 class DummyProfilingConnection : public IProfilingConnection
27 {
28 public:
DummyProfilingConnection()29 DummyProfilingConnection()
30 : m_Open(true)
31 , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength))
32 {
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);
36 }
37
38 ~DummyProfilingConnection() = default;
39
IsOpen() const40 bool IsOpen() const override
41 {
42 return m_Open;
43 }
44
Close()45 void Close() override
46 {
47 m_Open = false;
48 }
49
WritePacket(const unsigned char * buffer,uint32_t length)50 bool WritePacket(const unsigned char* buffer, uint32_t length) override
51 {
52 armnn::IgnoreUnused(buffer);
53 armnn::IgnoreUnused(length);
54 return true;
55 }
56
ReadPacket(uint32_t timeout)57 arm::pipe::Packet ReadPacket(uint32_t timeout) override
58 {
59 armnn::IgnoreUnused(timeout);
60 return std::move(*m_Packet);
61 }
62
63 private:
64 bool m_Open;
65 std::unique_ptr<unsigned char[]> m_PacketData;
66 std::unique_ptr<arm::pipe::Packet> m_Packet;
67 };
68
ReadDumpFile(const std::string & dumpFileName)69 std::vector<char> ReadDumpFile(const std::string& dumpFileName)
70 {
71 std::ifstream input(dumpFileName, std::ios::binary);
72 return std::vector<char>(std::istreambuf_iterator<char>(input), {});
73 }
74
75 } // anonymous namespace
76
77 BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests)
78
BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile)79 BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile)
80 {
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);
86 }
87
BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors)88 BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors)
89 {
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));
95 }
96
BOOST_AUTO_TEST_CASE(DumpIncomingValidFile)97 BOOST_AUTO_TEST_CASE(DumpIncomingValidFile)
98 {
99 fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpIncomingValidFileTest-TempFile");
100
101 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
102 options.m_IncomingCaptureFile = fileName.string();
103 options.m_OutgoingCaptureFile = "";
104
105 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
106
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)));
110
111 decorator.Close();
112
113 std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile);
114 const char* packetData = reinterpret_cast<const char*>(packet->GetData());
115
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);
121 }
122
BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile)123 BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile)
124 {
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);
130 }
131
BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors)132 BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors)
133 {
134 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
135 options.m_IncomingCaptureFile = "";
136 options.m_OutgoingCaptureFile = "/";
137
138 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
139 BOOST_CHECK_NO_THROW(decorator.WritePacket(g_DataPtr, g_DataLength));
140
141 bool success = decorator.WritePacket(g_DataPtr, g_DataLength);
142 BOOST_CHECK(!success);
143 }
144
BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile)145 BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile)
146 {
147 fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpOutgoingValidFileTest-TempFile");
148
149 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
150 options.m_IncomingCaptureFile = "";
151 options.m_OutgoingCaptureFile = fileName.string();
152
153 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
154
155 bool success = false;
156 BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
157 BOOST_CHECK(success);
158
159 decorator.Close();
160
161 std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile);
162
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);
167 }
168
169 BOOST_AUTO_TEST_SUITE_END()
170