Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "SocketProfilingConnection.hpp" |
| 7 | |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 8 | #include "common/include/SocketConnectionException.hpp" |
| 9 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 10 | #include <cerrno> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 11 | #include <fcntl.h> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 14 | using namespace armnnUtils; |
| 15 | |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | namespace profiling |
| 19 | { |
| 20 | |
| 21 | SocketProfilingConnection::SocketProfilingConnection() |
| 22 | { |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 23 | Sockets::Initialize(); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 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) |
| 28 | { |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 29 | throw armnnProfiling::SocketConnectionException( |
| 30 | std::string("SocketProfilingConnection: Socket construction failed: ") + strerror(errno), |
| 31 | m_Socket[0].fd, |
| 32 | errno); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Connect to the named unix domain socket. |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 36 | sockaddr_un server{}; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 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))) |
| 42 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 43 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 44 | throw armnnProfiling::SocketConnectionException( |
| 45 | std::string("SocketProfilingConnection: Cannot connect to stream socket: ") + strerror(errno), |
| 46 | m_Socket[0].fd, |
| 47 | errno); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Our socket will only be interested in polling reads. |
| 51 | m_Socket[0].events = POLLIN; |
| 52 | |
| 53 | // Make the socket non blocking. |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 54 | if (!Sockets::SetNonBlocking(m_Socket[0].fd)) |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 55 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 56 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 57 | throw armnnProfiling::SocketConnectionException( |
| 58 | std::string("SocketProfilingConnection: Failed to set socket as non blocking: ") + strerror(errno), |
| 59 | m_Socket[0].fd, |
| 60 | errno); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 64 | bool SocketProfilingConnection::IsOpen() const |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 65 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 66 | return m_Socket[0].fd > 0; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void SocketProfilingConnection::Close() |
| 70 | { |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 71 | if (Sockets::Close(m_Socket[0].fd) != 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 72 | { |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 73 | throw armnnProfiling::SocketConnectionException( |
| 74 | std::string("SocketProfilingConnection: Cannot close stream socket: ") + strerror(errno), |
| 75 | m_Socket[0].fd, |
| 76 | errno); |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 77 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 78 | |
| 79 | memset(m_Socket, 0, sizeof(m_Socket)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 82 | bool SocketProfilingConnection::WritePacket(const unsigned char* buffer, uint32_t length) |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 83 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 84 | if (buffer == nullptr || length == 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 85 | { |
| 86 | return false; |
| 87 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 88 | |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 89 | return Sockets::Write(m_Socket[0].fd, buffer, length) != -1; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | Packet SocketProfilingConnection::ReadPacket(uint32_t timeout) |
| 93 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 94 | // Is there currently at least a header worth of data waiting to be read? |
| 95 | int bytes_available = 0; |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 96 | Sockets::Ioctl(m_Socket[0].fd, FIONREAD, &bytes_available); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 97 | if (bytes_available >= 8) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 98 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 99 | // Yes there is. Read it: |
| 100 | return ReceivePacket(); |
| 101 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 102 | |
| 103 | // Poll for data on the socket or until timeout occurs |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 104 | int pollResult = Sockets::Poll(&m_Socket[0], 1, static_cast<int>(timeout)); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 105 | |
| 106 | switch (pollResult) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 107 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 108 | case -1: // Error |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 109 | throw armnnProfiling::SocketConnectionException( |
| 110 | std::string("SocketProfilingConnection: Error occured while reading from socket: ") + strerror(errno), |
| 111 | m_Socket[0].fd, |
| 112 | errno); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 113 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 114 | case 0: // Timeout |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 115 | throw TimeoutException("SocketProfilingConnection: Timeout while reading from socket"); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 116 | |
| 117 | default: // Normal poll return but it could still contain an error signal |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 118 | // Check if the socket reported an error |
| 119 | if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP)) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 120 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 121 | if (m_Socket[0].revents == POLLNVAL) |
| 122 | { |
| 123 | // This is an unrecoverable error. |
| 124 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 125 | throw armnnProfiling::SocketConnectionException( |
| 126 | std::string("SocketProfilingConnection: Error occured while polling receiving socket: POLLNVAL."), |
| 127 | m_Socket[0].fd); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 128 | } |
| 129 | if (m_Socket[0].revents == POLLERR) |
| 130 | { |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 131 | throw armnnProfiling::SocketConnectionException( |
| 132 | std::string( |
| 133 | "SocketProfilingConnection: Error occured while polling receiving socket: POLLERR: ") |
| 134 | + strerror(errno), |
| 135 | m_Socket[0].fd, |
| 136 | errno); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 137 | } |
| 138 | if (m_Socket[0].revents == POLLHUP) |
| 139 | { |
| 140 | // This is an unrecoverable error. |
| 141 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 142 | throw armnnProfiling::SocketConnectionException( |
| 143 | std::string("SocketProfilingConnection: Connection closed by remote client: POLLHUP."), |
| 144 | m_Socket[0].fd); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 145 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 146 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 147 | |
| 148 | // Check if there is data to read |
| 149 | if (!(m_Socket[0].revents & (POLLIN))) |
| 150 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 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. |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 153 | throw armnn::TimeoutException( |
| 154 | "SocketProfilingConnection: File descriptor was polled but no data was available to receive."); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return ReceivePacket(); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 160 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 161 | Packet SocketProfilingConnection::ReceivePacket() |
| 162 | { |
| 163 | char header[8] = {}; |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 164 | long receiveResult = Sockets::Read(m_Socket[0].fd, &header, sizeof(header)); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 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 ) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 167 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 168 | case 0: |
| 169 | // Socket has closed. |
| 170 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 171 | throw armnnProfiling::SocketConnectionException( |
| 172 | std::string("SocketProfilingConnection: Remote socket has closed the connection."), |
| 173 | m_Socket[0].fd); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 174 | case -1: |
| 175 | // There's been a socket error. We will presume it's unrecoverable. |
| 176 | Close(); |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 177 | throw armnnProfiling::SocketConnectionException( |
| 178 | std::string("SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno), |
| 179 | m_Socket[0].fd, |
| 180 | errno); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 181 | default: |
| 182 | if (receiveResult < 8) |
| 183 | { |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 184 | throw armnnProfiling::SocketConnectionException( |
| 185 | std::string( |
| 186 | "SocketProfilingConnection: The received packet did not contains a valid PIPE header."), |
| 187 | m_Socket[0].fd); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 188 | } |
| 189 | break; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 190 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 191 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 192 | // stream_metadata_identifier is the first 4 bytes |
| 193 | uint32_t metadataIdentifier = 0; |
| 194 | std::memcpy(&metadataIdentifier, header, sizeof(metadataIdentifier)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 195 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 196 | // data_length is the next 4 bytes |
| 197 | uint32_t dataLength = 0; |
| 198 | std::memcpy(&dataLength, header + 4u, sizeof(dataLength)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 199 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 200 | std::unique_ptr<unsigned char[]> packetData; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 201 | if (dataLength > 0) |
| 202 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 203 | packetData = std::make_unique<unsigned char[]>(dataLength); |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 204 | long receivedLength = Sockets::Read(m_Socket[0].fd, packetData.get(), dataLength); |
Jim Flynn | e11ff89 | 2019-10-04 04:25:43 -0700 | [diff] [blame] | 205 | if (receivedLength < 0) |
| 206 | { |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 207 | throw armnnProfiling::SocketConnectionException( |
| 208 | std::string("SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno), |
| 209 | m_Socket[0].fd, |
| 210 | errno); |
Jim Flynn | e11ff89 | 2019-10-04 04:25:43 -0700 | [diff] [blame] | 211 | } |
| 212 | if (dataLength != static_cast<uint32_t>(receivedLength)) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 213 | { |
| 214 | // What do we do here if we can't read in a full packet? |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 215 | throw armnnProfiling::SocketConnectionException( |
| 216 | std::string("SocketProfilingConnection: Invalid PIPE packet."), |
| 217 | m_Socket[0].fd); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 218 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 219 | } |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 220 | |
| 221 | return Packet(metadataIdentifier, dataLength, packetData); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } // namespace profiling |
| 225 | } // namespace armnn |