2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "SocketProfilingConnection.hpp"
8 #include "common/include/SocketConnectionException.hpp"
14 using namespace armnnUtils;
21 SocketProfilingConnection::SocketProfilingConnection()
23 Sockets::Initialize();
24 memset(m_Socket, 0, sizeof(m_Socket));
25 // Note: we're using Linux specific SOCK_CLOEXEC flag.
26 m_Socket[0].fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
27 if (m_Socket[0].fd == -1)
29 throw armnnProfiling::SocketConnectionException(
30 std::string("SocketProfilingConnection: Socket construction failed: ") + strerror(errno),
35 // Connect to the named unix domain socket.
37 memset(&server, 0, sizeof(sockaddr_un));
38 // As m_GatorNamespace begins with a null character we need to ignore that when getting its length.
39 memcpy(server.sun_path, m_GatorNamespace, strlen(m_GatorNamespace + 1) + 1);
40 server.sun_family = AF_UNIX;
41 if (0 != connect(m_Socket[0].fd, reinterpret_cast<const sockaddr*>(&server), sizeof(sockaddr_un)))
44 throw armnnProfiling::SocketConnectionException(
45 std::string("SocketProfilingConnection: Cannot connect to stream socket: ") + strerror(errno),
50 // Our socket will only be interested in polling reads.
51 m_Socket[0].events = POLLIN;
53 // Make the socket non blocking.
54 if (!Sockets::SetNonBlocking(m_Socket[0].fd))
57 throw armnnProfiling::SocketConnectionException(
58 std::string("SocketProfilingConnection: Failed to set socket as non blocking: ") + strerror(errno),
64 bool SocketProfilingConnection::IsOpen() const
66 return m_Socket[0].fd > 0;
69 void SocketProfilingConnection::Close()
71 if (Sockets::Close(m_Socket[0].fd) != 0)
73 throw armnnProfiling::SocketConnectionException(
74 std::string("SocketProfilingConnection: Cannot close stream socket: ") + strerror(errno),
79 memset(m_Socket, 0, sizeof(m_Socket));
82 bool SocketProfilingConnection::WritePacket(const unsigned char* buffer, uint32_t length)
84 if (buffer == nullptr || length == 0)
89 return Sockets::Write(m_Socket[0].fd, buffer, length) != -1;
92 Packet SocketProfilingConnection::ReadPacket(uint32_t timeout)
94 // Is there currently at least a header worth of data waiting to be read?
95 int bytes_available = 0;
96 Sockets::Ioctl(m_Socket[0].fd, FIONREAD, &bytes_available);
97 if (bytes_available >= 8)
99 // Yes there is. Read it:
100 return ReceivePacket();
103 // Poll for data on the socket or until timeout occurs
104 int pollResult = Sockets::Poll(&m_Socket[0], 1, static_cast<int>(timeout));
109 throw armnnProfiling::SocketConnectionException(
110 std::string("SocketProfilingConnection: Error occured while reading from socket: ") + strerror(errno),
115 throw TimeoutException("SocketProfilingConnection: Timeout while reading from socket");
117 default: // Normal poll return but it could still contain an error signal
118 // Check if the socket reported an error
119 if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP))
121 if (m_Socket[0].revents == POLLNVAL)
123 // This is an unrecoverable error.
125 throw armnnProfiling::SocketConnectionException(
126 std::string("SocketProfilingConnection: Error occured while polling receiving socket: POLLNVAL."),
129 if (m_Socket[0].revents == POLLERR)
131 throw armnnProfiling::SocketConnectionException(
133 "SocketProfilingConnection: Error occured while polling receiving socket: POLLERR: ")
138 if (m_Socket[0].revents == POLLHUP)
140 // This is an unrecoverable error.
142 throw armnnProfiling::SocketConnectionException(
143 std::string("SocketProfilingConnection: Connection closed by remote client: POLLHUP."),
148 // Check if there is data to read
149 if (!(m_Socket[0].revents & (POLLIN)))
151 // This is a corner case. The socket as been woken up but not with any data.
152 // We'll throw a timeout exception to loop around again.
153 throw armnn::TimeoutException(
154 "SocketProfilingConnection: File descriptor was polled but no data was available to receive.");
157 return ReceivePacket();
161 Packet SocketProfilingConnection::ReceivePacket()
164 long receiveResult = Sockets::Read(m_Socket[0].fd, &header, sizeof(header));
165 // We expect 8 as the result here. 0 means EOF, socket is closed. -1 means there been some other kind of error.
166 switch( receiveResult )
169 // Socket has closed.
171 throw armnnProfiling::SocketConnectionException(
172 std::string("SocketProfilingConnection: Remote socket has closed the connection."),
175 // There's been a socket error. We will presume it's unrecoverable.
177 throw armnnProfiling::SocketConnectionException(
178 std::string("SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno),
182 if (receiveResult < 8)
184 throw armnnProfiling::SocketConnectionException(
186 "SocketProfilingConnection: The received packet did not contains a valid PIPE header."),
192 // stream_metadata_identifier is the first 4 bytes
193 uint32_t metadataIdentifier = 0;
194 std::memcpy(&metadataIdentifier, header, sizeof(metadataIdentifier));
196 // data_length is the next 4 bytes
197 uint32_t dataLength = 0;
198 std::memcpy(&dataLength, header + 4u, sizeof(dataLength));
200 std::unique_ptr<unsigned char[]> packetData;
203 packetData = std::make_unique<unsigned char[]>(dataLength);
204 long receivedLength = Sockets::Read(m_Socket[0].fd, packetData.get(), dataLength);
205 if (receivedLength < 0)
207 throw armnnProfiling::SocketConnectionException(
208 std::string("SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno),
212 if (dataLength != static_cast<uint32_t>(receivedLength))
214 // What do we do here if we can't read in a full packet?
215 throw armnnProfiling::SocketConnectionException(
216 std::string("SocketProfilingConnection: Invalid PIPE packet."),
221 return Packet(metadataIdentifier, dataLength, packetData);
224 } // namespace profiling