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> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 8 | #include <armnn/utility/IgnoreUnused.hpp> |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 9 | |
| 10 | #include <fstream> |
| 11 | #include <sstream> |
| 12 | |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 13 | #include <boost/filesystem.hpp> |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 14 | #include <boost/numeric/conversion/cast.hpp> |
| 15 | #include <boost/test/unit_test.hpp> |
| 16 | |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 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 = boost::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: |
| 29 | DummyProfilingConnection() |
| 30 | : m_Open(true) |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 31 | , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength)) |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 32 | { |
| 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 Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 40 | bool IsOpen() const override |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 41 | { |
| 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 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 52 | armnn::IgnoreUnused(buffer); |
| 53 | armnn::IgnoreUnused(length); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | |
| 57 | Packet ReadPacket(uint32_t timeout) override |
| 58 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 59 | armnn::IgnoreUnused(timeout); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 60 | return std::move(*m_Packet); |
| 61 | } |
| 62 | |
| 63 | private: |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 64 | bool m_Open; |
| 65 | std::unique_ptr<unsigned char[]> m_PacketData; |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 66 | std::unique_ptr<Packet> m_Packet; |
| 67 | }; |
| 68 | |
| 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 | |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile) |
| 80 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 81 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 82 | options.m_IncomingCaptureFile = "/"; |
| 83 | options.m_OutgoingCaptureFile = ""; |
| 84 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 85 | BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors) |
| 89 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 90 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 91 | options.m_IncomingCaptureFile = "/"; |
| 92 | options.m_OutgoingCaptureFile = ""; |
| 93 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 94 | BOOST_CHECK_NO_THROW(decorator.ReadPacket(0)); |
| 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(DumpIncomingValidFile) |
| 98 | { |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 99 | boost::filesystem::path fileName = |
| 100 | boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); |
| 101 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 102 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
Rob Hughes | 270233f | 2019-11-13 11:53:48 +0000 | [diff] [blame] | 103 | options.m_IncomingCaptureFile = fileName.string(); |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 104 | options.m_OutgoingCaptureFile = ""; |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 105 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 106 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 107 | |
| 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 Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 114 | std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile); |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 115 | const char* packetData = reinterpret_cast<const char*>(packet->GetData()); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 116 | |
| 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 | |
| 123 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile) |
| 124 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 125 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 126 | options.m_IncomingCaptureFile = ""; |
| 127 | options.m_OutgoingCaptureFile = "/"; |
| 128 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 129 | BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors) |
| 133 | { |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 134 | 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-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 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 | |
| 145 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile) |
| 146 | { |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 147 | boost::filesystem::path fileName = |
| 148 | boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 149 | |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 150 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 151 | options.m_IncomingCaptureFile = ""; |
Rob Hughes | 270233f | 2019-11-13 11:53:48 +0000 | [diff] [blame] | 152 | options.m_OutgoingCaptureFile = fileName.string(); |
Keith Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 153 | |
| 154 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 155 | |
| 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 Davis | b10e081 | 2019-10-17 09:52:50 +0100 | [diff] [blame] | 162 | std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 163 | |
| 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 | |
| 169 | BOOST_AUTO_TEST_SUITE_END() |