ArmNN
 20.02
GatordMockService Class Reference

A class that implements a Mock Gatord server. More...

#include <GatordMockService.hpp>

Public Member Functions

 GatordMockService (armnn::profiling::CommandHandlerRegistry &registry, bool echoPackets)
 
 ~GatordMockService ()
 
bool OpenListeningSocket (std::string udsNamespace)
 Establish the Unix domain socket and set it to listen for connections. More...
 
armnnUtils::Sockets::Socket BlockForOneClient ()
 Block waiting to accept one client to connect to the UDS. More...
 
bool WaitForStreamMetaData ()
 Once the connection is open wait to receive the stream meta data packet from the client. More...
 
void SendConnectionAck ()
 Send a connection acknowledged packet back to the client. More...
 
void SendRequestCounterDir ()
 Send a request counter directory packet back to the client. More...
 
bool LaunchReceivingThread ()
 Start the thread that will receive all packets and print them nicely to stdout. More...
 
uint32_t GetPacketsReceivedCount ()
 Return the total number of periodic counter capture packets received since the receive thread started. More...
 
void WaitForReceivingThread ()
 This is a placeholder method to prevent main exiting. More...
 
bool ReceiveThreadRunning ()
 
void SendPeriodicCounterSelectionList (uint32_t period, std::vector< uint16_t > counters)
 Send the counter list to ArmNN. More...
 
void WaitCommand (uint32_t timeout)
 Execute the WAIT command from the comamnd file. More...
 
uint32_t GetStreamMetadataVersion ()
 
uint32_t GetStreamMetadataMaxDataLen ()
 
uint32_t GetStreamMetadataPid ()
 

Detailed Description

A class that implements a Mock Gatord server.

It will listen on a specified Unix domain socket (UDS) namespace for client connections. It will then allow opertaions to manage coutners while receiving counter data.

Definition at line 37 of file GatordMockService.hpp.

Constructor & Destructor Documentation

◆ GatordMockService()

GatordMockService ( armnn::profiling::CommandHandlerRegistry registry,
bool  echoPackets 
)
inline
Parameters
registryreference to a command handler registry.
echoPacketsif true the raw packets will be printed to stdout.

Definition at line 42 of file GatordMockService.hpp.

43  : m_HandlerRegistry(registry)
44  , m_EchoPackets(echoPackets)
45  , m_CloseReceivingThread(false)
46  {
47  m_PacketsReceivedCount.store(0, std::memory_order_relaxed);
48  }

◆ ~GatordMockService()

~GatordMockService ( )
inline

Definition at line 50 of file GatordMockService.hpp.

References armnnUtils::Sockets::Close().

51  {
52  // We have set SOCK_CLOEXEC on these sockets but we'll close them to be good citizens.
53  armnnUtils::Sockets::Close(m_ClientConnection);
54  armnnUtils::Sockets::Close(m_ListeningSocket);
55  }

Member Function Documentation

◆ BlockForOneClient()

Sockets::Socket BlockForOneClient ( )

Block waiting to accept one client to connect to the UDS.

Returns
the file descriptor of the client connection.

Definition at line 58 of file GatordMockService.cpp.

References armnnUtils::Sockets::Accept().

Referenced by BOOST_AUTO_TEST_CASE().

59 {
60  m_ClientConnection = Sockets::Accept(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC);
61  if (-1 == m_ClientConnection)
62  {
63  std::cerr << ": Failure when waiting for a client connection: " << strerror(errno) << std::endl;
64  return -1;
65  }
66  return m_ClientConnection;
67 }
armnnUtils::Sockets::Socket Accept(Socket s, sockaddr *addr, socklen_t *addrlen, int flags)

◆ GetPacketsReceivedCount()

uint32_t GetPacketsReceivedCount ( )
inline

Return the total number of periodic counter capture packets received since the receive thread started.

Returns
number of periodic counter capture packets received.

Definition at line 82 of file GatordMockService.hpp.

83  {
84  return m_PacketsReceivedCount.load(std::memory_order_acquire);
85  }

◆ GetStreamMetadataMaxDataLen()

uint32_t GetStreamMetadataMaxDataLen ( )
inline

Definition at line 108 of file GatordMockService.hpp.

109  {
110  return m_StreamMetaDataMaxDataLen;
111  }

◆ GetStreamMetadataPid()

uint32_t GetStreamMetadataPid ( )
inline

Definition at line 113 of file GatordMockService.hpp.

114  {
115  return m_StreamMetaDataPid;
116  }

◆ GetStreamMetadataVersion()

uint32_t GetStreamMetadataVersion ( )
inline

Definition at line 103 of file GatordMockService.hpp.

104  {
105  return m_StreamMetaDataVersion;
106  }

◆ LaunchReceivingThread()

bool LaunchReceivingThread ( )

Start the thread that will receive all packets and print them nicely to stdout.

Definition at line 149 of file GatordMockService.cpp.

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

Referenced by BOOST_AUTO_TEST_CASE().

150 {
151  if (m_EchoPackets)
152  {
153  std::cout << "Launching receiving thread." << std::endl;
154  }
155  // At this point we want to make the socket non blocking.
156  if (!Sockets::SetNonBlocking(m_ClientConnection))
157  {
158  Sockets::Close(m_ClientConnection);
159  std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl;
160  return false;
161  }
162  m_ListeningThread = std::thread(&GatordMockService::ReceiveLoop, this, std::ref(*this));
163  return true;
164 }
bool SetNonBlocking(Socket s)

◆ OpenListeningSocket()

bool OpenListeningSocket ( std::string  udsNamespace)

Establish the Unix domain socket and set it to listen for connections.

Parameters
udsNamespacethe namespace (socket address) associated with the listener.
Returns
true only if the socket has been correctly setup.

Definition at line 27 of file GatordMockService.cpp.

References armnnUtils::Sockets::Initialize().

Referenced by BOOST_AUTO_TEST_CASE().

28 {
30  m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
31  if (-1 == m_ListeningSocket)
32  {
33  std::cerr << ": Socket construction failed: " << strerror(errno) << std::endl;
34  return false;
35  }
36 
37  sockaddr_un udsAddress;
38  memset(&udsAddress, 0, sizeof(sockaddr_un));
39  // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it.
40  memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str()));
41  udsAddress.sun_family = AF_UNIX;
42 
43  // Bind the socket to the UDS namespace.
44  if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un)))
45  {
46  std::cerr << ": Binding on socket failed: " << strerror(errno) << std::endl;
47  return false;
48  }
49  // Listen for 1 connection.
50  if (-1 == listen(m_ListeningSocket, 1))
51  {
52  std::cerr << ": Listen call on socket failed: " << strerror(errno) << std::endl;
53  return false;
54  }
55  return true;
56 }
bool Initialize()
Performs any required one-time setup.

◆ ReceiveThreadRunning()

bool ReceiveThreadRunning ( )
inline

Definition at line 92 of file GatordMockService.hpp.

Referenced by CommandFileParser::ParseFile(), and GatordMockService::WaitCommand().

93  {
94  return !m_CloseReceivingThread.load();
95  }

◆ SendConnectionAck()

void SendConnectionAck ( )

Send a connection acknowledged packet back to the client.

Definition at line 129 of file GatordMockService.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

130 {
131  if (m_EchoPackets)
132  {
133  std::cout << "Sending connection acknowledgement." << std::endl;
134  }
135  // The connection ack packet is an empty data packet with packetId == 1.
136  SendPacket(0, 1, nullptr, 0);
137 }

◆ SendPeriodicCounterSelectionList()

void SendPeriodicCounterSelectionList ( uint32_t  period,
std::vector< uint16_t >  counters 
)

Send the counter list to ArmNN.

Definition at line 181 of file GatordMockService.cpp.

References armnn::profiling::WriteUint16(), and armnn::profiling::WriteUint32().

Referenced by CommandFileParser::ParseFile().

182 {
183  // The packet body consists of a UINT32 representing the period following by zero or more
184  // UINT16's representing counter UID's. If the list is empty it implies all counters are to
185  // be disabled.
186 
187  if (m_EchoPackets)
188  {
189  std::cout << "SendPeriodicCounterSelectionList: Period=" << std::dec << period << "uSec" << std::endl;
190  std::cout << "List length=" << counters.size() << std::endl;
191  ;
192  }
193  // Start by calculating the length of the packet body in bytes. This will be at least 4.
194  uint32_t dataLength = static_cast<uint32_t>(4 + (counters.size() * 2));
195 
196  std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength);
197  unsigned char* data = reinterpret_cast<unsigned char*>(uniqueData.get());
198 
199  uint32_t offset = 0;
200  profiling::WriteUint32(data, offset, period);
201  offset += 4;
202  for (std::vector<uint16_t>::iterator it = counters.begin(); it != counters.end(); ++it)
203  {
204  profiling::WriteUint16(data, offset, *it);
205  offset += 2;
206  }
207 
208  // Send the packet.
209  SendPacket(0, 4, data, dataLength);
210  // There will be an echo response packet sitting in the receive thread. PeriodicCounterSelectionResponseHandler
211  // should deal with it.
212 }
void WriteUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint16_t value)
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)

◆ SendRequestCounterDir()

void SendRequestCounterDir ( )

Send a request counter directory packet back to the client.

Definition at line 139 of file GatordMockService.cpp.

Referenced by CommandFileParser::ParseFile().

140 {
141  if (m_EchoPackets)
142  {
143  std::cout << "Sending connection acknowledgement." << std::endl;
144  }
145  // The connection ack packet is an empty data packet with packetId == 1.
146  SendPacket(0, 3, nullptr, 0);
147 }

◆ WaitCommand()

void WaitCommand ( uint32_t  timeout)

Execute the WAIT command from the comamnd file.

Definition at line 214 of file GatordMockService.cpp.

References armnn::gatordmock::BeWire, Version::GetEncodedValue(), Packet::GetLength(), Packet::GetPacketFamily(), Packet::GetPacketId(), armnnUtils::Sockets::Ioctl(), armnnUtils::Sockets::Poll(), armnnUtils::Sockets::Read(), armnn::gatordmock::ReceivedData, armnn::gatordmock::ReceivedHeader, GatordMockService::ReceiveThreadRunning(), PacketVersionResolver::ResolvePacketVersion(), armnn::gatordmock::Sending, Exception::what(), and armnnUtils::Sockets::Write().

Referenced by CommandFileParser::ParseFile().

215 {
216  // Wait for a maximum of timeout microseconds or if the receive thread has closed.
217  // There is a certain level of rounding involved in this timing.
218  uint32_t iterations = timeout / 1000;
219  std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations = " << iterations << std::endl;
220  uint32_t count = 0;
221  while ((this->ReceiveThreadRunning() && (count < iterations)))
222  {
223  std::this_thread::sleep_for(std::chrono::microseconds(1000));
224  ++count;
225  }
226  if (m_EchoPackets)
227  {
228  std::cout << std::dec << "Wait command with timeout of " << timeout << " microseconds completed. " << std::endl;
229  }
230 }

◆ WaitForReceivingThread()

void WaitForReceivingThread ( )

This is a placeholder method to prevent main exiting.

It can be removed once the command handling code is added.

Definition at line 166 of file GatordMockService.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

167 {
168  // The receiving thread may already have died.
169  if (m_CloseReceivingThread != true)
170  {
171  m_CloseReceivingThread.store(true);
172  }
173  // Check that the receiving thread is running
174  if (m_ListeningThread.joinable())
175  {
176  // Wait for the receiving thread to complete operations
177  m_ListeningThread.join();
178  }
179 }

◆ WaitForStreamMetaData()

bool WaitForStreamMetaData ( )

Once the connection is open wait to receive the stream meta data packet from the client.

Reading this packet differs from others as we need to determine endianness.

Returns
true only if a valid stream met data packet has been received.

Definition at line 69 of file GatordMockService.cpp.

References armnn::gatordmock::BeWire, armnn::gatordmock::LeWire, armnnUtils::Sockets::Read(), armnn::gatordmock::ReceivedData, and armnn::gatordmock::ReceivedHeader.

Referenced by BOOST_AUTO_TEST_CASE().

70 {
71  if (m_EchoPackets)
72  {
73  std::cout << "Waiting for stream meta data..." << std::endl;
74  }
75  // The start of the stream metadata is 2x32bit words, 0 and packet length.
76  uint8_t header[8];
77  if (!ReadFromSocket(header, 8))
78  {
79  return false;
80  }
81  EchoPacket(PacketDirection::ReceivedHeader, header, 8);
82  // The first word, stream_metadata_identifer, should always be 0.
83  if (ToUint32(&header[0], TargetEndianness::BeWire) != 0)
84  {
85  std::cerr << ": Protocol error. The stream_metadata_identifer was not 0." << std::endl;
86  return false;
87  }
88 
89  uint8_t pipeMagic[4];
90  if (!ReadFromSocket(pipeMagic, 4))
91  {
92  return false;
93  }
94  EchoPacket(PacketDirection::ReceivedData, pipeMagic, 4);
95 
96  // Before we interpret the length we need to read the pipe_magic word to determine endianness.
97  if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == PIPE_MAGIC)
98  {
99  m_Endianness = TargetEndianness::BeWire;
100  }
101  else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == PIPE_MAGIC)
102  {
103  m_Endianness = TargetEndianness::LeWire;
104  }
105  else
106  {
107  std::cerr << ": Protocol read error. Unable to read PIPE_MAGIC value." << std::endl;
108  return false;
109  }
110  // Now we know the endianness we can get the length from the header.
111  // Remember we already read the pipe magic 4 bytes.
112  uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4;
113  // Read the entire packet.
114  std::vector<uint8_t> packetData(metaDataLength);
115  if (metaDataLength !=
116  boost::numeric_cast<uint32_t>(Sockets::Read(m_ClientConnection, packetData.data(), metaDataLength)))
117  {
118  std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
119  return false;
120  }
121  EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength);
122  m_StreamMetaDataVersion = ToUint32(&packetData[0], m_Endianness);
123  m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness);
124  m_StreamMetaDataPid = ToUint32(&packetData[8], m_Endianness);
125 
126  return true;
127 }
long Read(Socket s, void *buf, size_t len)

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