2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ProfilingConnectionDumpToFileDecorator.hpp"
8 #include <armnn/Exceptions.hpp>
9 #include <armnn/utility/NumericCast.hpp>
19 ProfilingConnectionDumpToFileDecorator::ProfilingConnectionDumpToFileDecorator(
20 std::unique_ptr<IProfilingConnection> connection,
21 const Runtime::CreationOptions::ExternalProfilingOptions& options,
23 : m_Connection(std::move(connection))
25 , m_IgnoreFileErrors(ignoreFailures)
29 throw InvalidArgumentException("Connection cannot be nullptr");
33 ProfilingConnectionDumpToFileDecorator::~ProfilingConnectionDumpToFileDecorator()
38 bool ProfilingConnectionDumpToFileDecorator::IsOpen() const
40 return m_Connection->IsOpen();
43 void ProfilingConnectionDumpToFileDecorator::Close()
45 m_IncomingDumpFileStream.flush();
46 m_IncomingDumpFileStream.close();
47 m_OutgoingDumpFileStream.flush();
48 m_OutgoingDumpFileStream.close();
49 m_Connection->Close();
52 bool ProfilingConnectionDumpToFileDecorator::WritePacket(const unsigned char* buffer, uint32_t length)
55 if (!m_Options.m_OutgoingCaptureFile.empty())
57 success &= DumpOutgoingToFile(buffer, length);
59 success &= m_Connection->WritePacket(buffer, length);
63 arm::pipe::Packet ProfilingConnectionDumpToFileDecorator::ReadPacket(uint32_t timeout)
65 arm::pipe::Packet packet = m_Connection->ReadPacket(timeout);
66 if (!m_Options.m_IncomingCaptureFile.empty())
68 DumpIncomingToFile(packet);
73 bool ProfilingConnectionDumpToFileDecorator::OpenIncomingDumpFile()
75 m_IncomingDumpFileStream.open(m_Options.m_IncomingCaptureFile, std::ios::out | std::ios::binary);
76 return m_IncomingDumpFileStream.is_open();
79 bool ProfilingConnectionDumpToFileDecorator::OpenOutgoingDumpFile()
81 m_OutgoingDumpFileStream.open(m_Options.m_OutgoingCaptureFile, std::ios::out | std::ios::binary);
82 return m_OutgoingDumpFileStream.is_open();
86 /// Dumps incoming data into the file specified by m_Settings.m_IncomingDumpFileName.
87 /// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored,
88 /// i.e. the method will not throw an exception if it encounters an error while trying
89 /// to write the data into the specified file.
90 /// @param packet data packet to write
92 void ProfilingConnectionDumpToFileDecorator::DumpIncomingToFile(const arm::pipe::Packet& packet)
95 if (!m_IncomingDumpFileStream.is_open())
97 // attempt to open dump file
98 success &= OpenIncomingDumpFile();
99 if (!(success || m_IgnoreFileErrors))
101 Fail("Failed to open \"" + m_Options.m_IncomingCaptureFile + "\" for writing");
105 // attempt to write binary data from packet
106 const unsigned int header = packet.GetHeader();
107 const unsigned int packetLength = packet.GetLength();
109 m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&header), sizeof header);
110 m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(&packetLength), sizeof packetLength);
111 m_IncomingDumpFileStream.write(reinterpret_cast<const char*>(packet.GetData()),
112 armnn::numeric_cast<std::streamsize>(packetLength));
114 success &= m_IncomingDumpFileStream.good();
115 if (!(success || m_IgnoreFileErrors))
117 Fail("Error writing incoming packet of " + std::to_string(packetLength) + " bytes");
121 /// Dumps outgoing data into the file specified by m_Settings.m_OutgoingDumpFileName.
122 /// If m_IgnoreFileErrors is set to true in m_Settings, write errors will be ignored,
123 /// i.e. the method will not throw an exception if it encounters an error while trying
124 /// to write the data into the specified file. However, the return value will still
125 /// signal if the write has not been completed succesfully.
126 /// @param buffer pointer to data to write
127 /// @param length number of bytes to write
128 /// @return true if write successful, false otherwise
129 bool ProfilingConnectionDumpToFileDecorator::DumpOutgoingToFile(const unsigned char* buffer, uint32_t length)
132 if (!m_OutgoingDumpFileStream.is_open())
134 // attempt to open dump file
135 success &= OpenOutgoingDumpFile();
136 if (!(success || m_IgnoreFileErrors))
138 Fail("Failed to open \"" + m_Options.m_OutgoingCaptureFile + "\" for writing");
142 // attempt to write binary data
143 m_OutgoingDumpFileStream.write(reinterpret_cast<const char*>(buffer),
144 armnn::numeric_cast<std::streamsize>(length));
145 success &= m_OutgoingDumpFileStream.good();
146 if (!(success || m_IgnoreFileErrors))
148 Fail("Error writing outgoing packet of " + std::to_string(length) + " bytes");
154 void ProfilingConnectionDumpToFileDecorator::Fail(const std::string& errorMessage)
157 throw RuntimeException(errorMessage);
160 } // namespace profiling