blob: c393ec9b07bec57a3c018b31f595c350c0af5d52 [file] [log] [blame]
Aron Virginas-Tardfa14772019-09-24 18:24:47 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "../ProfilingConnectionDumpToFileDecorator.hpp"
Keith Davisb10e0812019-10-17 09:52:50 +01007#include <Runtime.hpp>
Aron Virginas-Tardfa14772019-09-24 18:24:47 +01008
9#include <fstream>
10#include <sstream>
11
12#include <boost/core/ignore_unused.hpp>
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +000013#include <boost/filesystem.hpp>
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010014#include <boost/numeric/conversion/cast.hpp>
15#include <boost/test/unit_test.hpp>
16
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010017using namespace armnn::profiling;
18
19namespace
20{
21
22const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' };
23const uint32_t g_DataLength = boost::numeric_cast<uint32_t>(g_Data.size());
24const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data());
25
26class DummyProfilingConnection : public IProfilingConnection
27{
28public:
29 DummyProfilingConnection()
30 : m_Open(true)
Matteo Martincigh67ef2a52019-10-10 13:29:02 +010031 , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength))
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010032 {
33 // populate packet data and construct packet
34 std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength);
35 m_Packet = std::make_unique<Packet>(0u, g_DataLength, m_PacketData);
36 }
37
38 ~DummyProfilingConnection() = default;
39
Matteo Martincigh54fb9572019-10-02 12:50:57 +010040 bool IsOpen() const override
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010041 {
42 return m_Open;
43 }
44
45 void Close() override
46 {
47 m_Open = false;
48 }
49
50 bool WritePacket(const unsigned char* buffer, uint32_t length) override
51 {
52 boost::ignore_unused(buffer);
53 boost::ignore_unused(length);
54 return true;
55 }
56
57 Packet ReadPacket(uint32_t timeout) override
58 {
59 boost::ignore_unused(timeout);
60 return std::move(*m_Packet);
61 }
62
63private:
Matteo Martincigh67ef2a52019-10-10 13:29:02 +010064 bool m_Open;
65 std::unique_ptr<unsigned char[]> m_PacketData;
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010066 std::unique_ptr<Packet> m_Packet;
67};
68
69std::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
77BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests)
78
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010079BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile)
80{
Keith Davisb10e0812019-10-17 09:52:50 +010081 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
82 options.m_IncomingCaptureFile = "/";
83 options.m_OutgoingCaptureFile = "";
84 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010085 BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException);
86}
87
88BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors)
89{
Keith Davisb10e0812019-10-17 09:52:50 +010090 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
91 options.m_IncomingCaptureFile = "/";
92 options.m_OutgoingCaptureFile = "";
93 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +010094 BOOST_CHECK_NO_THROW(decorator.ReadPacket(0));
95}
96
97BOOST_AUTO_TEST_CASE(DumpIncomingValidFile)
98{
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +000099 boost::filesystem::path fileName =
100 boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
101
Keith Davisb10e0812019-10-17 09:52:50 +0100102 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +0000103 options.m_IncomingCaptureFile = fileName.c_str();
Keith Davisb10e0812019-10-17 09:52:50 +0100104 options.m_OutgoingCaptureFile = "";
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100105
Keith Davisb10e0812019-10-17 09:52:50 +0100106 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100107
108 // NOTE: unique_ptr is needed here because operator=() is deleted for Packet
109 std::unique_ptr<Packet> packet;
110 BOOST_CHECK_NO_THROW(packet = std::make_unique<Packet>(decorator.ReadPacket(0)));
111
112 decorator.Close();
113
Keith Davisb10e0812019-10-17 09:52:50 +0100114 std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile);
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100115 const char* packetData = reinterpret_cast<const char*>(packet->GetData());
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100116
117 // check if the data read back from the dump file matches the original
118 constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length
119 int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength);
120 BOOST_CHECK(diff == 0);
121}
122
123BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile)
124{
Keith Davisb10e0812019-10-17 09:52:50 +0100125 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
126 options.m_IncomingCaptureFile = "";
127 options.m_OutgoingCaptureFile = "/";
128 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100129 BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException);
130}
131
132BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors)
133{
Keith Davisb10e0812019-10-17 09:52:50 +0100134 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
135 options.m_IncomingCaptureFile = "";
136 options.m_OutgoingCaptureFile = "/";
137
138 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100139 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
145BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile)
146{
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +0000147 boost::filesystem::path fileName =
148 boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100149
Keith Davisb10e0812019-10-17 09:52:50 +0100150 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
151 options.m_IncomingCaptureFile = "";
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +0000152 options.m_OutgoingCaptureFile = fileName.c_str();
Keith Davisb10e0812019-10-17 09:52:50 +0100153
154 ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100155
156 bool success = false;
157 BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
158 BOOST_CHECK(success);
159
160 decorator.Close();
161
Keith Davisb10e0812019-10-17 09:52:50 +0100162 std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile);
Aron Virginas-Tardfa14772019-09-24 18:24:47 +0100163
164 // check if the data read back from the dump file matches the original
165 int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength);
166 BOOST_CHECK(diff == 0);
167}
168
169BOOST_AUTO_TEST_SUITE_END()