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 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 8 | #include <cerrno> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 9 | #include <fcntl.h> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 12 | using namespace armnnUtils; |
| 13 | |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | namespace profiling |
| 17 | { |
| 18 | |
| 19 | SocketProfilingConnection::SocketProfilingConnection() |
| 20 | { |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 21 | Sockets::Initialize(); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 22 | memset(m_Socket, 0, sizeof(m_Socket)); |
| 23 | // Note: we're using Linux specific SOCK_CLOEXEC flag. |
| 24 | m_Socket[0].fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 25 | if (m_Socket[0].fd == -1) |
| 26 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 27 | throw armnn::RuntimeException(std::string("Socket construction failed: ") + strerror(errno)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // Connect to the named unix domain socket. |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 31 | sockaddr_un server{}; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 32 | memset(&server, 0, sizeof(sockaddr_un)); |
| 33 | // As m_GatorNamespace begins with a null character we need to ignore that when getting its length. |
| 34 | memcpy(server.sun_path, m_GatorNamespace, strlen(m_GatorNamespace + 1) + 1); |
| 35 | server.sun_family = AF_UNIX; |
| 36 | if (0 != connect(m_Socket[0].fd, reinterpret_cast<const sockaddr*>(&server), sizeof(sockaddr_un))) |
| 37 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 38 | Close(); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 39 | throw armnn::RuntimeException(std::string("Cannot connect to stream socket: ") + strerror(errno)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Our socket will only be interested in polling reads. |
| 43 | m_Socket[0].events = POLLIN; |
| 44 | |
| 45 | // Make the socket non blocking. |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 46 | if (!Sockets::SetNonBlocking(m_Socket[0].fd)) |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 47 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 48 | Close(); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 49 | throw armnn::RuntimeException(std::string("Failed to set socket as non blocking: ") + strerror(errno)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 53 | bool SocketProfilingConnection::IsOpen() const |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 54 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 55 | return m_Socket[0].fd > 0; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void SocketProfilingConnection::Close() |
| 59 | { |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 60 | if (Sockets::Close(m_Socket[0].fd) != 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 61 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 62 | throw armnn::RuntimeException(std::string("Cannot close stream socket: ") + strerror(errno)); |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 63 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 64 | |
| 65 | memset(m_Socket, 0, sizeof(m_Socket)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 68 | bool SocketProfilingConnection::WritePacket(const unsigned char* buffer, uint32_t length) |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 69 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 70 | if (buffer == nullptr || length == 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 71 | { |
| 72 | return false; |
| 73 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 74 | |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 75 | return Sockets::Write(m_Socket[0].fd, buffer, length) != -1; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Packet SocketProfilingConnection::ReadPacket(uint32_t timeout) |
| 79 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 80 | // Is there currently at least a header worth of data waiting to be read? |
| 81 | int bytes_available = 0; |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 82 | Sockets::Ioctl(m_Socket[0].fd, FIONREAD, &bytes_available); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 83 | if (bytes_available >= 8) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 84 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 85 | // Yes there is. Read it: |
| 86 | return ReceivePacket(); |
| 87 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 88 | |
| 89 | // Poll for data on the socket or until timeout occurs |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 90 | int pollResult = Sockets::Poll(&m_Socket[0], 1, static_cast<int>(timeout)); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 91 | |
| 92 | switch (pollResult) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 93 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 94 | case -1: // Error |
| 95 | throw armnn::RuntimeException(std::string("Read failure from socket: ") + strerror(errno)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 96 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 97 | case 0: // Timeout |
| 98 | throw TimeoutException("Timeout while reading from socket"); |
| 99 | |
| 100 | default: // Normal poll return but it could still contain an error signal |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 101 | // Check if the socket reported an error |
| 102 | if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP)) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 103 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 104 | if (m_Socket[0].revents == POLLNVAL) |
| 105 | { |
| 106 | // This is an unrecoverable error. |
| 107 | Close(); |
| 108 | throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLNVAL")); |
| 109 | } |
| 110 | if (m_Socket[0].revents == POLLERR) |
| 111 | { |
| 112 | throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLERR: ") + |
| 113 | strerror(errno)); |
| 114 | } |
| 115 | if (m_Socket[0].revents == POLLHUP) |
| 116 | { |
| 117 | // This is an unrecoverable error. |
| 118 | Close(); |
| 119 | throw armnn::RuntimeException(std::string("Connection closed by remote client: POLLHUP")); |
| 120 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 121 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 122 | |
| 123 | // Check if there is data to read |
| 124 | if (!(m_Socket[0].revents & (POLLIN))) |
| 125 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 126 | // This is a corner case. The socket as been woken up but not with any data. |
| 127 | // We'll throw a timeout exception to loop around again. |
| 128 | throw armnn::TimeoutException("File descriptor was polled but no data was available to receive."); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return ReceivePacket(); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 134 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 135 | Packet SocketProfilingConnection::ReceivePacket() |
| 136 | { |
| 137 | char header[8] = {}; |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 138 | long receiveResult = Sockets::Read(m_Socket[0].fd, &header, sizeof(header)); |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 139 | // We expect 8 as the result here. 0 means EOF, socket is closed. -1 means there been some other kind of error. |
| 140 | switch( receiveResult ) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 141 | { |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 142 | case 0: |
| 143 | // Socket has closed. |
| 144 | Close(); |
| 145 | throw armnn::RuntimeException("Remote socket has closed the connection."); |
| 146 | case -1: |
| 147 | // There's been a socket error. We will presume it's unrecoverable. |
| 148 | Close(); |
| 149 | throw armnn::RuntimeException(std::string("Error occured on recv: ") + strerror(errno)); |
| 150 | default: |
| 151 | if (receiveResult < 8) |
| 152 | { |
| 153 | throw armnn::RuntimeException("The received packet did not contains a valid MIPE header"); |
| 154 | } |
| 155 | break; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 156 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 157 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 158 | // stream_metadata_identifier is the first 4 bytes |
| 159 | uint32_t metadataIdentifier = 0; |
| 160 | std::memcpy(&metadataIdentifier, header, sizeof(metadataIdentifier)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 161 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 162 | // data_length is the next 4 bytes |
| 163 | uint32_t dataLength = 0; |
| 164 | std::memcpy(&dataLength, header + 4u, sizeof(dataLength)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 165 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 166 | std::unique_ptr<unsigned char[]> packetData; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 167 | if (dataLength > 0) |
| 168 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 169 | packetData = std::make_unique<unsigned char[]>(dataLength); |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 170 | long receivedLength = Sockets::Read(m_Socket[0].fd, packetData.get(), dataLength); |
Jim Flynn | e11ff89 | 2019-10-04 04:25:43 -0700 | [diff] [blame] | 171 | if (receivedLength < 0) |
| 172 | { |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame^] | 173 | throw armnn::RuntimeException(std::string("Error occurred on recv: ") + strerror(errno)); |
Jim Flynn | e11ff89 | 2019-10-04 04:25:43 -0700 | [diff] [blame] | 174 | } |
| 175 | if (dataLength != static_cast<uint32_t>(receivedLength)) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 176 | { |
| 177 | // What do we do here if we can't read in a full packet? |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 178 | throw armnn::RuntimeException("Invalid MIPE packet"); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 179 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 180 | } |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 181 | |
| 182 | return Packet(metadataIdentifier, dataLength, packetData); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace profiling |
| 186 | } // namespace armnn |