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" |
| 7 | |
| 8 | #include <armnn/Exceptions.hpp> |
| 9 | |
| 10 | #include <fstream> |
| 11 | |
| 12 | namespace armnn |
| 13 | { |
| 14 | |
| 15 | namespace profiling |
| 16 | { |
| 17 | |
| 18 | ProfilingConnectionDumpToFileDecorator::ProfilingConnectionDumpToFileDecorator( |
| 19 | std::unique_ptr<IProfilingConnection> connection, |
| 20 | const Settings& settings) |
| 21 | : m_Connection(std::move(connection)) |
| 22 | , m_Settings(settings) |
| 23 | { |
| 24 | if (!m_Connection) |
| 25 | { |
| 26 | throw InvalidArgumentException("Connection cannot be nullptr"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | ProfilingConnectionDumpToFileDecorator::~ProfilingConnectionDumpToFileDecorator() |
| 31 | { |
| 32 | Close(); |
| 33 | } |
| 34 | |
| 35 | bool ProfilingConnectionDumpToFileDecorator::IsOpen() |
| 36 | { |
| 37 | return m_Connection->IsOpen(); |
| 38 | } |
| 39 | |
| 40 | void ProfilingConnectionDumpToFileDecorator::Close() |
| 41 | { |
| 42 | m_IncomingDumpFileStream.close(); |
| 43 | m_OutgoingDumpFileStream.close(); |
| 44 | m_Connection->Close(); |
| 45 | } |
| 46 | |
| 47 | bool ProfilingConnectionDumpToFileDecorator::WritePacket(const unsigned char* buffer, uint32_t length) |
| 48 | { |
| 49 | bool success = true; |
| 50 | if (m_Settings.m_DumpOutgoing) |
| 51 | { |
| 52 | success &= DumpOutgoingToFile(reinterpret_cast<const char*>(buffer), length); |
| 53 | } |
| 54 | success &= m_Connection->WritePacket(buffer, length); |
| 55 | return success; |
| 56 | } |
| 57 | |
| 58 | Packet ProfilingConnectionDumpToFileDecorator::ReadPacket(uint32_t timeout) |
| 59 | { |
| 60 | Packet packet = m_Connection->ReadPacket(timeout); |
| 61 | if (m_Settings.m_DumpIncoming) |
| 62 | { |
| 63 | DumpIncomingToFile(packet); |
| 64 | } |
| 65 | return packet; |
| 66 | } |
| 67 | |
| 68 | bool ProfilingConnectionDumpToFileDecorator::OpenIncomingDumpFile() |
| 69 | { |
| 70 | m_IncomingDumpFileStream.open(m_Settings.m_IncomingDumpFileName, std::ios::out | std::ios::binary); |
| 71 | return m_IncomingDumpFileStream.is_open(); |
| 72 | } |
| 73 | |
| 74 | bool ProfilingConnectionDumpToFileDecorator::OpenOutgoingDumpFile() |
| 75 | { |
| 76 | m_OutgoingDumpFileStream.open(m_Settings.m_OutgoingDumpFileName, std::ios::out | std::ios::binary); |
| 77 | return m_OutgoingDumpFileStream.is_open(); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /// Dumps incoming data into the file specified by m_Settings.m_IncomingDumpFileName. |
| 82 | /// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored, |
| 83 | /// i.e. the method will not throw an exception if it encounters an error while trying |
| 84 | /// to write the data into the specified file. |
| 85 | /// @param packet data packet to write |
| 86 | /// @return nothing |
| 87 | void ProfilingConnectionDumpToFileDecorator::DumpIncomingToFile(const Packet& packet) |
| 88 | { |
| 89 | bool success = true; |
| 90 | if (!m_IncomingDumpFileStream.is_open()) |
| 91 | { |
| 92 | // attempt to open dump file |
| 93 | success &= OpenIncomingDumpFile(); |
| 94 | if (!(success || m_Settings.m_IgnoreFileErrors)) |
| 95 | { |
| 96 | Fail("Failed to open \"" + m_Settings.m_IncomingDumpFileName + "\" for writing"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // attempt to write binary data from packet |
| 101 | const unsigned int header = packet.GetHeader(); |
| 102 | const unsigned int packetLength = packet.GetLength(); |
| 103 | |
| 104 | m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&header), sizeof header); |
| 105 | m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&packetLength), sizeof packetLength); |
| 106 | m_IncomingDumpFileStream.write(packet.GetData(), packetLength); |
| 107 | |
| 108 | success &= m_IncomingDumpFileStream.good(); |
| 109 | if (!(success || m_Settings.m_IgnoreFileErrors)) |
| 110 | { |
| 111 | Fail("Error writing incoming packet of " + std::to_string(packetLength) + " bytes"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Dumps outgoing data into the file specified by m_Settings.m_OutgoingDumpFileName. |
| 116 | /// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored, |
| 117 | /// i.e. the method will not throw an exception if it encounters an error while trying |
| 118 | /// to write the data into the specified file. However, the return value will still |
| 119 | /// signal if the write has not been completed succesfully. |
| 120 | /// @param buffer pointer to data to write |
| 121 | /// @param length number of bytes to write |
| 122 | /// @return true if write successful, false otherwise |
| 123 | bool ProfilingConnectionDumpToFileDecorator::DumpOutgoingToFile(const char* buffer, uint32_t length) |
| 124 | { |
| 125 | bool success = true; |
| 126 | if (!m_OutgoingDumpFileStream.is_open()) |
| 127 | { |
| 128 | // attempt to open dump file |
| 129 | success &= OpenOutgoingDumpFile(); |
| 130 | if (!(success || m_Settings.m_IgnoreFileErrors)) |
| 131 | { |
| 132 | Fail("Failed to open \"" + m_Settings.m_OutgoingDumpFileName + "\" for writing"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // attempt to write binary data |
| 137 | m_OutgoingDumpFileStream.write(buffer, length); |
| 138 | success &= m_OutgoingDumpFileStream.good(); |
| 139 | if (!(success || m_Settings.m_IgnoreFileErrors)) |
| 140 | { |
| 141 | Fail("Error writing outgoing packet of " + std::to_string(length) + " bytes"); |
| 142 | } |
| 143 | |
| 144 | return success; |
| 145 | } |
| 146 | |
| 147 | void ProfilingConnectionDumpToFileDecorator::Fail(const std::string& errorMessage) |
| 148 | { |
| 149 | Close(); |
| 150 | throw RuntimeException(errorMessage); |
| 151 | } |
| 152 | |
| 153 | } // namespace profiling |
| 154 | |
| 155 | } // namespace armnn |