2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "GatordMockService.hpp"
8 #include <CommandHandlerRegistry.hpp>
9 #include <PacketVersionResolver.hpp>
10 #include <ProfilingUtils.hpp>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
29 bool GatordMockService::OpenListeningSocket(std::string udsNamespace)
31 m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
32 if (-1 == m_ListeningSocket)
34 std::cerr << ": Socket construction failed: " << strerror(errno) << std::endl;
38 sockaddr_un udsAddress;
39 memset(&udsAddress, 0, sizeof(sockaddr_un));
40 // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it.
41 memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str()));
42 udsAddress.sun_family = AF_UNIX;
44 // Bind the socket to the UDS namespace.
45 if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un)))
47 std::cerr << ": Binding on socket failed: " << strerror(errno) << std::endl;
50 // Listen for 1 connection.
51 if (-1 == listen(m_ListeningSocket, 1))
53 std::cerr << ": Listen call on socket failed: " << strerror(errno) << std::endl;
59 int GatordMockService::BlockForOneClient()
61 m_ClientConnection = accept4(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC);
62 if (-1 == m_ClientConnection)
64 std::cerr << ": Failure when waiting for a client connection: " << strerror(errno) << std::endl;
67 return m_ClientConnection;
70 bool GatordMockService::WaitForStreamMetaData()
74 std::cout << "Waiting for stream meta data..." << std::endl;
76 // The start of the stream metadata is 2x32bit words, 0 and packet length.
78 if (!ReadFromSocket(header, 8))
82 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
83 // The first word, stream_metadata_identifer, should always be 0.
84 if (ToUint32(&header[0], TargetEndianness::BeWire) != 0)
86 std::cerr << ": Protocol error. The stream_metadata_identifer was not 0." << std::endl;
91 if (!ReadFromSocket(pipeMagic, 4))
95 EchoPacket(PacketDirection::ReceivedData, pipeMagic, 4);
97 // Before we interpret the length we need to read the pipe_magic word to determine endianness.
98 if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == PIPE_MAGIC)
100 m_Endianness = TargetEndianness::BeWire;
102 else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == PIPE_MAGIC)
104 m_Endianness = TargetEndianness::LeWire;
108 std::cerr << ": Protocol read error. Unable to read PIPE_MAGIC value." << std::endl;
111 // Now we know the endianness we can get the length from the header.
112 // Remember we already read the pipe magic 4 bytes.
113 uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4;
114 // Read the entire packet.
115 uint8_t packetData[metaDataLength];
116 if (metaDataLength != boost::numeric_cast<uint32_t>(read(m_ClientConnection, &packetData, metaDataLength)))
118 std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
121 EchoPacket(PacketDirection::ReceivedData, packetData, 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);
129 void GatordMockService::SendConnectionAck()
133 std::cout << "Sending connection acknowledgement." << std::endl;
135 // The connection ack packet is an empty data packet with packetId == 1.
136 SendPacket(0, 1, nullptr, 0);
139 void GatordMockService::SendRequestCounterDir()
143 std::cout << "Sending connection acknowledgement." << std::endl;
145 // The connection ack packet is an empty data packet with packetId == 1.
146 SendPacket(0, 3, nullptr, 0);
149 bool GatordMockService::LaunchReceivingThread()
153 std::cout << "Launching receiving thread." << std::endl;
155 // At this point we want to make the socket non blocking.
156 const int currentFlags = fcntl(m_ClientConnection, F_GETFL);
157 if (0 != fcntl(m_ClientConnection, F_SETFL, currentFlags | O_NONBLOCK))
159 close(m_ClientConnection);
160 std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl;
163 m_ListeningThread = std::thread(&GatordMockService::ReceiveLoop, this, std::ref(*this));
167 void GatordMockService::WaitForReceivingThread()
169 // The receiving thread may already have died.
170 if (m_CloseReceivingThread != true)
172 m_CloseReceivingThread.store(true);
174 // Check that the receiving thread is running
175 if (m_ListeningThread.joinable())
177 // Wait for the receiving thread to complete operations
178 m_ListeningThread.join();
182 void GatordMockService::SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters)
184 // The packet body consists of a UINT32 representing the period following by zero or more
185 // UINT16's representing counter UID's. If the list is empty it implies all counters are to
190 std::cout << "SendPeriodicCounterSelectionList: Period=" << std::dec << period << "uSec" << std::endl;
191 std::cout << "List length=" << counters.size() << std::endl;
194 // Start by calculating the length of the packet body in bytes. This will be at least 4.
195 uint32_t dataLength = static_cast<uint32_t>(4 + (counters.size() * 2));
197 std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength);
198 unsigned char* data = reinterpret_cast<unsigned char*>(uniqueData.get());
201 profiling::WriteUint32(data, offset, period);
203 for (std::vector<uint16_t>::iterator it = counters.begin(); it != counters.end(); ++it)
205 profiling::WriteUint16(data, offset, *it);
210 SendPacket(0, 4, data, dataLength);
211 // There will be an echo response packet sitting in the receive thread. PeriodicCounterSelectionResponseHandler
212 // should deal with it.
215 void GatordMockService::WaitCommand(uint timeout)
217 // Wait for a maximum of timeout microseconds or if the receive thread has closed.
218 // There is a certain level of rounding involved in this timing.
219 uint iterations = timeout / 1000;
220 std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations = " << iterations << std::endl;
222 while ((this->ReceiveThreadRunning() && (count < iterations)))
224 std::this_thread::sleep_for(std::chrono::microseconds(1000));
229 std::cout << std::dec << "Wait command with timeout of " << timeout << " microseconds completed. " << std::endl;
233 void GatordMockService::ReceiveLoop(GatordMockService& mockService)
235 m_CloseReceivingThread.store(false);
236 while (!m_CloseReceivingThread.load())
240 armnn::profiling::Packet packet = mockService.WaitForPacket(500);
242 catch (const armnn::TimeoutException&)
244 // In this case we ignore timeouts and and keep trying to receive.
246 catch (const armnn::InvalidArgumentException& e)
248 // We couldn't find a functor to handle the packet?
249 std::cerr << "Packet received that could not be processed: " << e.what() << std::endl;
251 catch (const armnn::RuntimeException& e)
253 // A runtime exception occurred which means we must exit the loop.
254 std::cerr << "Receive thread closing: " << e.what() << std::endl;
255 m_CloseReceivingThread.store(true);
260 armnn::profiling::Packet GatordMockService::WaitForPacket(uint32_t timeoutMs)
262 // Is there currently more than a headers worth of data waiting to be read?
264 ioctl(m_ClientConnection, FIONREAD, &bytes_available);
265 if (bytes_available > 8)
267 // Yes there is. Read it:
268 return ReceivePacket();
272 // No there's not. Poll for more data.
273 struct pollfd pollingFd[1]{};
274 pollingFd[0].fd = m_ClientConnection;
275 int pollResult = poll(pollingFd, 1, static_cast<int>(timeoutMs));
281 throw armnn::RuntimeException(std::string("File descriptor reported an error during polling: ") +
286 throw armnn::TimeoutException("Timeout while waiting to receive packet.");
288 // Normal poll return. It could still contain an error signal
290 // Check if the socket reported an error
291 if (pollingFd[0].revents & (POLLNVAL | POLLERR | POLLHUP))
293 if (pollingFd[0].revents == POLLNVAL)
295 throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLNVAL"));
297 if (pollingFd[0].revents == POLLERR)
299 throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLERR: ") +
302 if (pollingFd[0].revents == POLLHUP)
304 throw armnn::RuntimeException(std::string("Connection closed by remote client: POLLHUP"));
308 // Check if there is data to read
309 if (!(pollingFd[0].revents & (POLLIN)))
311 // This is a corner case. The socket as been woken up but not with any data.
312 // We'll throw a timeout exception to loop around again.
313 throw armnn::TimeoutException("File descriptor was polled but no data was available to receive.");
315 return ReceivePacket();
320 armnn::profiling::Packet GatordMockService::ReceivePacket()
323 if (!ReadHeader(header))
325 return armnn::profiling::Packet();
327 // Read data_length bytes from the socket.
328 std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(header[1]);
329 unsigned char* packetData = reinterpret_cast<unsigned char*>(uniquePacketData.get());
331 if (!ReadFromSocket(packetData, header[1]))
333 return armnn::profiling::Packet();
336 EchoPacket(PacketDirection::ReceivedData, packetData, header[1]);
338 // Construct received packet
339 armnn::profiling::PacketVersionResolver packetVersionResolver;
340 armnn::profiling::Packet packetRx = armnn::profiling::Packet(header[0], header[1], uniquePacketData);
343 std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength()
347 profiling::Version version =
348 packetVersionResolver.ResolvePacketVersion(packetRx.GetPacketFamily(), packetRx.GetPacketId());
350 profiling::CommandHandlerFunctor* commandHandlerFunctor =
351 m_HandlerRegistry.GetFunctor(packetRx.GetPacketFamily(), packetRx.GetPacketId(), version.GetEncodedValue());
352 BOOST_ASSERT(commandHandlerFunctor);
353 commandHandlerFunctor->operator()(packetRx);
357 bool GatordMockService::SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength)
359 // Construct a packet from the id and data given and send it to the client.
360 // Encode the header.
362 header[0] = packetFamily << 26 | packetId << 16;
363 header[1] = dataLength;
364 // Add the header to the packet.
365 uint8_t packet[8 + dataLength];
366 InsertU32(header[0], packet, m_Endianness);
367 InsertU32(header[1], packet + 4, m_Endianness);
368 // And the rest of the data if there is any.
371 memcpy((packet + 8), data, dataLength);
373 EchoPacket(PacketDirection::Sending, packet, sizeof(packet));
374 if (-1 == write(m_ClientConnection, packet, sizeof(packet)))
376 std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl;
382 bool GatordMockService::ReadHeader(uint32_t headerAsWords[2])
384 // The header will always be 2x32bit words.
386 if (!ReadFromSocket(header, 8))
390 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
391 headerAsWords[0] = ToUint32(&header[0], m_Endianness);
392 headerAsWords[1] = ToUint32(&header[4], m_Endianness);
396 bool GatordMockService::ReadFromSocket(uint8_t* packetData, uint32_t expectedLength)
398 // This is a blocking read until either expectedLength has been received or an error is detected.
399 ssize_t totalBytesRead = 0;
400 while (boost::numeric_cast<uint32_t>(totalBytesRead) < expectedLength)
402 ssize_t bytesRead = recv(m_ClientConnection, packetData, expectedLength, 0);
405 std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl;
410 std::cerr << ": EOF while reading from client socket." << std::endl;
413 totalBytesRead += bytesRead;
418 void GatordMockService::EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes)
420 // If enabled print the contents of the data packet to the console.
423 if (direction == PacketDirection::Sending)
425 std::cout << "TX " << std::dec << lengthInBytes << " bytes : ";
427 else if (direction == PacketDirection::ReceivedHeader)
429 std::cout << "RX Header " << std::dec << lengthInBytes << " bytes : ";
433 std::cout << "RX Data " << std::dec << lengthInBytes << " bytes : ";
435 for (unsigned int i = 0; i < lengthInBytes; i++)
439 std::cout << std::endl;
441 std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(packet[i])
444 std::cout << std::endl;
448 uint32_t GatordMockService::ToUint32(uint8_t* data, TargetEndianness endianness)
450 // Extract the first 4 bytes starting at data and push them into a 32bit integer based on the
451 // specified endianness.
452 if (endianness == TargetEndianness::BeWire)
454 return static_cast<uint32_t>(data[0]) << 24 | static_cast<uint32_t>(data[1]) << 16 |
455 static_cast<uint32_t>(data[2]) << 8 | static_cast<uint32_t>(data[3]);
459 return static_cast<uint32_t>(data[3]) << 24 | static_cast<uint32_t>(data[2]) << 16 |
460 static_cast<uint32_t>(data[1]) << 8 | static_cast<uint32_t>(data[0]);
464 void GatordMockService::InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness)
466 // Take the bytes of a 32bit integer and copy them into char array starting at data considering
467 // the endianness value.
468 if (endianness == TargetEndianness::BeWire)
470 *data = static_cast<uint8_t>((value >> 24) & 0xFF);
471 *(data + 1) = static_cast<uint8_t>((value >> 16) & 0xFF);
472 *(data + 2) = static_cast<uint8_t>((value >> 8) & 0xFF);
473 *(data + 3) = static_cast<uint8_t>(value & 0xFF);
477 *(data + 3) = static_cast<uint8_t>((value >> 24) & 0xFF);
478 *(data + 2) = static_cast<uint8_t>((value >> 16) & 0xFF);
479 *(data + 1) = static_cast<uint8_t>((value >> 8) & 0xFF);
480 *data = static_cast<uint8_t>(value & 0xFF);
484 } // namespace gatordmock