ArmNN  NotReleased
SocketProfilingConnection Class Reference

#include <SocketProfilingConnection.hpp>

Inheritance diagram for SocketProfilingConnection:
IProfilingConnection

Public Member Functions

 SocketProfilingConnection ()
 
bool IsOpen () const final
 
void Close () final
 
bool WritePacket (const unsigned char *buffer, uint32_t length) final
 
Packet ReadPacket (uint32_t timeout) final
 
- Public Member Functions inherited from IProfilingConnection
virtual ~IProfilingConnection ()
 

Detailed Description

Definition at line 18 of file SocketProfilingConnection.hpp.

Constructor & Destructor Documentation

◆ SocketProfilingConnection()

Definition at line 19 of file SocketProfilingConnection.cpp.

References SocketProfilingConnection::Close(), armnnUtils::Sockets::Initialize(), and armnnUtils::Sockets::SetNonBlocking().

20 {
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  {
27  throw armnn::RuntimeException(std::string("Socket construction failed: ") + strerror(errno));
28  }
29 
30  // Connect to the named unix domain socket.
31  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();
39  throw armnn::RuntimeException(std::string("Cannot connect to stream socket: ") + strerror(errno));
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  if (!Sockets::SetNonBlocking(m_Socket[0].fd))
47  {
48  Close();
49  throw armnn::RuntimeException(std::string("Failed to set socket as non blocking: ") + strerror(errno));
50  }
51 }
bool Initialize()
Performs any required one-time setup.
bool SetNonBlocking(Socket s)

Member Function Documentation

◆ Close()

void Close ( )
finalvirtual

Implements IProfilingConnection.

Definition at line 58 of file SocketProfilingConnection.cpp.

References armnnUtils::Sockets::Close().

Referenced by SocketProfilingConnection::ReadPacket(), and SocketProfilingConnection::SocketProfilingConnection().

59 {
60  if (Sockets::Close(m_Socket[0].fd) != 0)
61  {
62  throw armnn::RuntimeException(std::string("Cannot close stream socket: ") + strerror(errno));
63  }
64 
65  memset(m_Socket, 0, sizeof(m_Socket));
66 }

◆ IsOpen()

bool IsOpen ( ) const
finalvirtual

Implements IProfilingConnection.

Definition at line 53 of file SocketProfilingConnection.cpp.

54 {
55  return m_Socket[0].fd > 0;
56 }

◆ ReadPacket()

Packet ReadPacket ( uint32_t  timeout)
finalvirtual

Implements IProfilingConnection.

Definition at line 78 of file SocketProfilingConnection.cpp.

References SocketProfilingConnection::Close(), armnnUtils::Sockets::Ioctl(), armnnUtils::Sockets::Poll(), and armnnUtils::Sockets::Read().

79 {
80  // Is there currently at least a header worth of data waiting to be read?
81  int bytes_available = 0;
82  Sockets::Ioctl(m_Socket[0].fd, FIONREAD, &bytes_available);
83  if (bytes_available >= 8)
84  {
85  // Yes there is. Read it:
86  return ReceivePacket();
87  }
88 
89  // Poll for data on the socket or until timeout occurs
90  int pollResult = Sockets::Poll(&m_Socket[0], 1, static_cast<int>(timeout));
91 
92  switch (pollResult)
93  {
94  case -1: // Error
95  throw armnn::RuntimeException(std::string("Read failure from socket: ") + strerror(errno));
96 
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
101  // Check if the socket reported an error
102  if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP))
103  {
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  }
121  }
122 
123  // Check if there is data to read
124  if (!(m_Socket[0].revents & (POLLIN)))
125  {
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.");
129  }
130 
131  return ReceivePacket();
132  }
133 }
int Poll(PollFd *fds, nfds_t numFds, int timeout)
int Ioctl(Socket s, unsigned long int cmd, void *arg)

◆ WritePacket()

bool WritePacket ( const unsigned char *  buffer,
uint32_t  length 
)
finalvirtual

Implements IProfilingConnection.

Definition at line 68 of file SocketProfilingConnection.cpp.

References armnnUtils::Sockets::Write().

69 {
70  if (buffer == nullptr || length == 0)
71  {
72  return false;
73  }
74 
75  return Sockets::Write(m_Socket[0].fd, buffer, length) != -1;
76 }
long Write(Socket s, const void *buf, size_t len)

The documentation for this class was generated from the following files: