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 | |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <sys/un.h> |
| 11 | #include <cerrno> |
| 12 | #include <string> |
| 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | namespace profiling |
| 17 | { |
| 18 | |
| 19 | SocketProfilingConnection::SocketProfilingConnection() |
| 20 | { |
| 21 | memset(m_Socket, 0, sizeof(m_Socket)); |
| 22 | // Note: we're using Linux specific SOCK_CLOEXEC flag. |
| 23 | m_Socket[0].fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 24 | if (m_Socket[0].fd == -1) |
| 25 | { |
| 26 | throw armnn::Exception(std::string(": Socket construction failed: ") + strerror(errno)); |
| 27 | } |
| 28 | |
| 29 | // Connect to the named unix domain socket. |
| 30 | struct sockaddr_un server{}; |
| 31 | memset(&server, 0, sizeof(sockaddr_un)); |
| 32 | // As m_GatorNamespace begins with a null character we need to ignore that when getting its length. |
| 33 | memcpy(server.sun_path, m_GatorNamespace, strlen(m_GatorNamespace + 1) + 1); |
| 34 | server.sun_family = AF_UNIX; |
| 35 | if (0 != connect(m_Socket[0].fd, reinterpret_cast<const sockaddr*>(&server), sizeof(sockaddr_un))) |
| 36 | { |
| 37 | close(m_Socket[0].fd); |
| 38 | throw armnn::Exception(std::string(": Cannot connect to stream socket: ") + strerror(errno)); |
| 39 | } |
| 40 | |
| 41 | // Our socket will only be interested in polling reads. |
| 42 | m_Socket[0].events = POLLIN; |
| 43 | |
| 44 | // Make the socket non blocking. |
| 45 | const int currentFlags = fcntl(m_Socket[0].fd, F_GETFL); |
| 46 | if (0 != fcntl(m_Socket[0].fd, F_SETFL, currentFlags | O_NONBLOCK)) |
| 47 | { |
| 48 | close(m_Socket[0].fd); |
| 49 | throw armnn::Exception(std::string(": Failed to set socket as non blocking: ") + strerror(errno)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | bool SocketProfilingConnection::IsOpen() |
| 54 | { |
| 55 | // Dummy return value, function not implemented |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | void SocketProfilingConnection::Close() |
| 60 | { |
| 61 | // Function not implemented |
| 62 | } |
| 63 | |
| 64 | bool SocketProfilingConnection::WritePacket(const char* buffer, uint32_t length) |
| 65 | { |
| 66 | // Dummy return value, function not implemented |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | Packet SocketProfilingConnection::ReadPacket(uint32_t timeout) |
| 71 | { |
| 72 | // Dummy return value, function not implemented |
| 73 | return {472580096, 0, nullptr}; |
| 74 | } |
| 75 | |
| 76 | } // namespace profiling |
| 77 | } // namespace armnn |