Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "../ProfilingConnectionDumpToFileDecorator.hpp" |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 7 | #include <Runtime.hpp> |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 8 | |
| 9 | #include <fstream> |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include <boost/core/ignore_unused.hpp> |
| 13 | #include <boost/numeric/conversion/cast.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | #if defined(__ANDROID__) |
| 17 | #define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/data/local/tmp" |
| 18 | #else |
| 19 | #define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/tmp" |
| 20 | #endif |
| 21 | |
| 22 | using namespace armnn::profiling; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | |
| 27 | const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' }; |
| 28 | const uint32_t g_DataLength = boost::numeric_cast<uint32_t>(g_Data.size()); |
| 29 | const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data()); |
| 30 | |
| 31 | class DummyProfilingConnection : public IProfilingConnection |
| 32 | { |
| 33 | public: |
| 34 | DummyProfilingConnection() |
| 35 | : m_Open(true) |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 36 | , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength)) |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 37 | { |
| 38 | // populate packet data and construct packet |
| 39 | std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength); |
| 40 | m_Packet = std::make_unique<Packet>(0u, g_DataLength, m_PacketData); |
| 41 | } |
| 42 | |
| 43 | ~DummyProfilingConnection() = default; |
| 44 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 45 | bool IsOpen() const override |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 46 | { |
| 47 | return m_Open; |
| 48 | } |
| 49 | |
| 50 | void Close() override |
| 51 | { |
| 52 | m_Open = false; |
| 53 | } |
| 54 | |
| 55 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 56 | { |
| 57 | boost::ignore_unused(buffer); |
| 58 | boost::ignore_unused(length); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | Packet ReadPacket(uint32_t timeout) override |
| 63 | { |
| 64 | boost::ignore_unused(timeout); |
| 65 | return std::move(*m_Packet); |
| 66 | } |
| 67 | |
| 68 | private: |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 69 | bool m_Open; |
| 70 | std::unique_ptr<unsigned char[]> m_PacketData; |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 71 | std::unique_ptr<Packet> m_Packet; |
| 72 | }; |
| 73 | |
| 74 | std::vector<char> ReadDumpFile(const std::string& dumpFileName) |
| 75 | { |
| 76 | std::ifstream input(dumpFileName, std::ios::binary); |
| 77 | return std::vector<char>(std::istreambuf_iterator<char>(input), {}); |
| 78 | } |
| 79 | |
| 80 | } // anonymous namespace |
| 81 | |
| 82 | BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests) |
| 83 | |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile) |
| 85 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 86 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 87 | options.m_IncomingCaptureFile = "/"; |
| 88 | options.m_OutgoingCaptureFile = ""; |
| 89 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 90 | BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException); |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors) |
| 94 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 95 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 96 | options.m_IncomingCaptureFile = "/"; |
| 97 | options.m_OutgoingCaptureFile = ""; |
| 98 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 99 | BOOST_CHECK_NO_THROW(decorator.ReadPacket(0)); |
| 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_CASE(DumpIncomingValidFile) |
| 103 | { |
| 104 | std::stringstream fileName; |
| 105 | fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file_incoming.dat"; |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 106 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 107 | options.m_IncomingCaptureFile = fileName.str(); |
| 108 | options.m_OutgoingCaptureFile = ""; |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 109 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 110 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 111 | |
| 112 | // NOTE: unique_ptr is needed here because operator=() is deleted for Packet |
| 113 | std::unique_ptr<Packet> packet; |
| 114 | BOOST_CHECK_NO_THROW(packet = std::make_unique<Packet>(decorator.ReadPacket(0))); |
| 115 | |
| 116 | decorator.Close(); |
| 117 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 118 | std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile); |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 119 | const char* packetData = reinterpret_cast<const char*>(packet->GetData()); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 120 | |
| 121 | // check if the data read back from the dump file matches the original |
| 122 | constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length |
| 123 | int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength); |
| 124 | BOOST_CHECK(diff == 0); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile) |
| 128 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 129 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 130 | options.m_IncomingCaptureFile = ""; |
| 131 | options.m_OutgoingCaptureFile = "/"; |
| 132 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 133 | BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors) |
| 137 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 138 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 139 | options.m_IncomingCaptureFile = ""; |
| 140 | options.m_OutgoingCaptureFile = "/"; |
| 141 | |
| 142 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 143 | BOOST_CHECK_NO_THROW(decorator.WritePacket(g_DataPtr, g_DataLength)); |
| 144 | |
| 145 | bool success = decorator.WritePacket(g_DataPtr, g_DataLength); |
| 146 | BOOST_CHECK(!success); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile) |
| 150 | { |
| 151 | std::stringstream fileName; |
| 152 | fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file.dat"; |
| 153 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 154 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 155 | options.m_IncomingCaptureFile = ""; |
| 156 | options.m_OutgoingCaptureFile = fileName.str(); |
| 157 | |
| 158 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 159 | |
| 160 | bool success = false; |
| 161 | BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength)); |
| 162 | BOOST_CHECK(success); |
| 163 | |
| 164 | decorator.Close(); |
| 165 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 166 | std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 167 | |
| 168 | // check if the data read back from the dump file matches the original |
| 169 | int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength); |
| 170 | BOOST_CHECK(diff == 0); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() |