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> |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 10 | #include <sys/ioctl.h> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 13 | #include <string> |
| 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | namespace profiling |
| 18 | { |
| 19 | |
| 20 | SocketProfilingConnection::SocketProfilingConnection() |
| 21 | { |
| 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. |
| 31 | struct sockaddr_un server{}; |
| 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 | { |
| 38 | close(m_Socket[0].fd); |
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. |
| 46 | const int currentFlags = fcntl(m_Socket[0].fd, F_GETFL); |
| 47 | if (0 != fcntl(m_Socket[0].fd, F_SETFL, currentFlags | O_NONBLOCK)) |
| 48 | { |
| 49 | close(m_Socket[0].fd); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 50 | 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] | 51 | } |
| 52 | } |
| 53 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 54 | bool SocketProfilingConnection::IsOpen() const |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 55 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 56 | return m_Socket[0].fd > 0; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void SocketProfilingConnection::Close() |
| 60 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 61 | if (close(m_Socket[0].fd) != 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 62 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 63 | throw armnn::RuntimeException(std::string("Cannot close stream socket: ") + strerror(errno)); |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 64 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 65 | |
| 66 | memset(m_Socket, 0, sizeof(m_Socket)); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 69 | bool SocketProfilingConnection::WritePacket(const unsigned char* buffer, uint32_t length) |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 70 | { |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 71 | if (buffer == nullptr || length == 0) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 72 | { |
| 73 | return false; |
| 74 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 75 | |
| 76 | return write(m_Socket[0].fd, buffer, length) != -1; |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Packet SocketProfilingConnection::ReadPacket(uint32_t timeout) |
| 80 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 81 | // Is there currently at least a header worth of data waiting to be read? |
| 82 | int bytes_available = 0; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 83 | ioctl(m_Socket[0].fd, FIONREAD, &bytes_available); |
| 84 | if (bytes_available >= 8) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 85 | { |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 86 | // Yes there is. Read it: |
| 87 | return ReceivePacket(); |
| 88 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 89 | |
| 90 | // Poll for data on the socket or until timeout occurs |
| 91 | int pollResult = poll(m_Socket, 1, static_cast<int>(timeout)); |
| 92 | |
| 93 | switch (pollResult) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 94 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 95 | case -1: // Error |
| 96 | throw armnn::RuntimeException(std::string("Read failure from socket: ") + strerror(errno)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 97 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 98 | case 0: // Timeout |
| 99 | throw TimeoutException("Timeout while reading from socket"); |
| 100 | |
| 101 | default: // Normal poll return but it could still contain an error signal |
| 102 | |
| 103 | // Check if the socket reported an error |
| 104 | if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP)) |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 105 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 106 | throw armnn::RuntimeException(std::string("Socket reported an error: ") + strerror(errno)); |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 107 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 108 | |
| 109 | // Check if there is data to read |
| 110 | if (!(m_Socket[0].revents & (POLLIN))) |
| 111 | { |
| 112 | // This is a very odd case. The file descriptor was woken up but no data was written. |
| 113 | throw armnn::RuntimeException("Poll resulted in : no data to read"); |
| 114 | } |
| 115 | |
| 116 | return ReceivePacket(); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 119 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 120 | Packet SocketProfilingConnection::ReceivePacket() |
| 121 | { |
| 122 | char header[8] = {}; |
| 123 | if (8 != recv(m_Socket[0].fd, &header, sizeof(header), 0)) |
| 124 | { |
| 125 | // What do we do here if there's not a valid 8 byte header to read? |
| 126 | throw armnn::RuntimeException("The received packet did not contains a valid MIPE header"); |
| 127 | } |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 128 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 129 | // stream_metadata_identifier is the first 4 bytes |
| 130 | uint32_t metadataIdentifier = 0; |
| 131 | std::memcpy(&metadataIdentifier, header, sizeof(metadataIdentifier)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 132 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 133 | // data_length is the next 4 bytes |
| 134 | uint32_t dataLength = 0; |
| 135 | std::memcpy(&dataLength, header + 4u, sizeof(dataLength)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 136 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 137 | std::unique_ptr<unsigned char[]> packetData; |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 138 | if (dataLength > 0) |
| 139 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame^] | 140 | packetData = std::make_unique<unsigned char[]>(dataLength); |
Jim Flynn | e11ff89 | 2019-10-04 04:25:43 -0700 | [diff] [blame] | 141 | ssize_t receivedLength = recv(m_Socket[0].fd, packetData.get(), dataLength, 0); |
| 142 | if (receivedLength < 0) |
| 143 | { |
| 144 | throw armnn::RuntimeException(std::string("Error occured on recv: ") + strerror(errno)); |
| 145 | } |
| 146 | if (dataLength != static_cast<uint32_t>(receivedLength)) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 147 | { |
| 148 | // 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] | 149 | throw armnn::RuntimeException("Invalid MIPE packet"); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 150 | } |
FinnWilliamsArm | a0c7871 | 2019-09-16 12:06:47 +0100 | [diff] [blame] | 151 | } |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 152 | |
| 153 | return Packet(metadataIdentifier, dataLength, packetData); |
Teresa Charlin | 9bab496 | 2019-09-06 12:28:35 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace profiling |
| 157 | } // namespace armnn |