ArmNN
 21.11
GatordMockService Class Reference

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

#include <GatordMockService.hpp>

Public Member Functions

 GatordMockService (std::unique_ptr< arm::pipe::BasePipeServer > clientConnection, bool echoPackets)
 
 GatordMockService (const GatordMockService &)=delete
 
GatordMockServiceoperator= (const GatordMockService &)=delete
 
 GatordMockService (GatordMockService &&)=delete
 
GatordMockServiceoperator= (GatordMockService &&)=delete
 
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...
 
void SendActivateTimelinePacket ()
 Send a activate timeline packet back to the client. More...
 
void SendDeactivateTimelinePacket ()
 Send a deactivate timeline 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...
 
profiling::DirectoryCaptureCommandHandlerGetDirectoryCaptureCommandHandler ()
 
arm::pipe::TimelineDecoder & GetTimelineDecoder ()
 
arm::pipe::TimelineDirectoryCaptureCommandHandler & GetTimelineDirectoryCaptureCommandHandler ()
 

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 38 of file GatordMockService.hpp.

Constructor & Destructor Documentation

◆ GatordMockService() [1/3]

GatordMockService ( std::unique_ptr< arm::pipe::BasePipeServer >  clientConnection,
bool  echoPackets 
)
inline
Parameters
registryreference to a command handler registry.
echoPacketsif true the raw packets will be printed to stdout.

Definition at line 43 of file GatordMockService.hpp.

References GatordMockService::LaunchReceivingThread(), GatordMockService::operator=(), GatordMockService::SendActivateTimelinePacket(), GatordMockService::SendConnectionAck(), GatordMockService::SendDeactivateTimelinePacket(), GatordMockService::SendRequestCounterDir(), and GatordMockService::WaitForStreamMetaData().

44  : m_BasePipeServer(std::move(clientConnection))
45  , m_EchoPackets(echoPackets)
46  , m_CloseReceivingThread(false)
47  , m_PacketVersionResolver()
48  , m_HandlerRegistry()
49  , m_TimelineDecoder()
50  , m_CounterCaptureCommandHandler(
51  0, 4, m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), !echoPackets)
52  , m_StreamMetadataCommandHandler(
53  0, 0, m_PacketVersionResolver.ResolvePacketVersion(0, 0).GetEncodedValue(), !echoPackets)
54  // This stub lets us ignore any counter capture packets we receive without throwing an error
55  , m_StubCommandHandler(3, 0, m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue())
56  , m_DirectoryCaptureCommandHandler(
57  0, 2, m_PacketVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), !echoPackets)
58  , m_TimelineCaptureCommandHandler(
59  1, 1, m_PacketVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), m_TimelineDecoder)
60  , m_TimelineDirectoryCaptureCommandHandler(
61  1, 0, m_PacketVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(),
62  m_TimelineCaptureCommandHandler, !echoPackets)
63  {
64  m_TimelineDecoder.SetDefaultCallbacks();
65 
66  m_HandlerRegistry.RegisterFunctor(&m_CounterCaptureCommandHandler);
67  m_HandlerRegistry.RegisterFunctor(&m_StreamMetadataCommandHandler);
68  m_HandlerRegistry.RegisterFunctor(&m_StubCommandHandler);
69  m_HandlerRegistry.RegisterFunctor(&m_DirectoryCaptureCommandHandler);
70  m_HandlerRegistry.RegisterFunctor(&m_TimelineDirectoryCaptureCommandHandler);
71  m_HandlerRegistry.RegisterFunctor(&m_TimelineCaptureCommandHandler);
72  }

◆ GatordMockService() [2/3]

GatordMockService ( const GatordMockService )
delete

◆ GatordMockService() [3/3]

Member Function Documentation

◆ GetDirectoryCaptureCommandHandler()

profiling::DirectoryCaptureCommandHandler& GetDirectoryCaptureCommandHandler ( )
inline

Definition at line 123 of file GatordMockService.hpp.

124  {
125  return m_DirectoryCaptureCommandHandler;
126  }

◆ 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 102 of file GatordMockService.hpp.

References GatordMockService::WaitForReceivingThread().

103  {
104  return m_PacketsReceivedCount.load(std::memory_order_acquire);
105  }

◆ GetTimelineDecoder()

arm::pipe::TimelineDecoder& GetTimelineDecoder ( )
inline

Definition at line 128 of file GatordMockService.hpp.

129  {
130  return m_TimelineDecoder;
131  }

◆ GetTimelineDirectoryCaptureCommandHandler()

arm::pipe::TimelineDirectoryCaptureCommandHandler& GetTimelineDirectoryCaptureCommandHandler ( )
inline

Definition at line 133 of file GatordMockService.hpp.

134  {
135  return m_TimelineDirectoryCaptureCommandHandler;
136  }

◆ LaunchReceivingThread()

bool LaunchReceivingThread ( )

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

Definition at line 64 of file GatordMockService.cpp.

Referenced by CreateMockService(), and GatordMockService::GatordMockService().

65 {
66  if (m_EchoPackets)
67  {
68  std::cout << "Launching receiving thread." << std::endl;
69  }
70  // At this point we want to make the socket non blocking.
71  if (!m_BasePipeServer.get()->SetNonBlocking())
72  {
73  m_BasePipeServer.get()->Close();
74  std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl;
75  return false;
76  }
77  m_ListeningThread = std::thread(&GatordMockService::ReceiveLoop, this);
78  return true;
79 }

◆ operator=() [1/2]

GatordMockService& operator= ( const GatordMockService )
delete

◆ operator=() [2/2]

GatordMockService& operator= ( GatordMockService &&  )
delete

◆ ReceiveThreadRunning()

bool ReceiveThreadRunning ( )
inline

Definition at line 112 of file GatordMockService.hpp.

References GatordMockService::SendPeriodicCounterSelectionList(), and GatordMockService::WaitCommand().

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

113  {
114  return !m_CloseReceivingThread.load();
115  }

◆ SendActivateTimelinePacket()

void SendActivateTimelinePacket ( )

Send a activate timeline packet back to the client.

Definition at line 44 of file GatordMockService.cpp.

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

45 {
46  if (m_EchoPackets)
47  {
48  std::cout << "Sending activate timeline packet." << std::endl;
49  }
50  // The activate timeline packet is an empty data packet with packetId == 6.
51  m_BasePipeServer.get()->SendPacket(0, 6, nullptr, 0);
52 }

◆ SendConnectionAck()

void SendConnectionAck ( )

Send a connection acknowledged packet back to the client.

Definition at line 24 of file GatordMockService.cpp.

Referenced by CreateMockService(), and GatordMockService::GatordMockService().

25 {
26  if (m_EchoPackets)
27  {
28  std::cout << "Sending connection acknowledgement." << std::endl;
29  }
30  // The connection ack packet is an empty data packet with packetId == 1.
31  m_BasePipeServer.get()->SendPacket(0, 1, nullptr, 0);
32 }

◆ SendDeactivateTimelinePacket()

void SendDeactivateTimelinePacket ( )

Send a deactivate timeline packet back to the client.

Definition at line 54 of file GatordMockService.cpp.

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

55 {
56  if (m_EchoPackets)
57  {
58  std::cout << "Sending deactivate timeline packet." << std::endl;
59  }
60  // The deactivate timeline packet is an empty data packet with packetId == 7.
61  m_BasePipeServer.get()->SendPacket(0, 7, nullptr, 0);
62 }

◆ SendPeriodicCounterSelectionList()

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

Send the counter list to ArmNN.

Definition at line 106 of file GatordMockService.cpp.

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

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

107 {
108  // The packet body consists of a UINT32 representing the period following by zero or more
109  // UINT16's representing counter UID's. If the list is empty it implies all counters are to
110  // be disabled.
111 
112  if (m_EchoPackets)
113  {
114  std::cout << "SendPeriodicCounterSelectionList: Period=" << std::dec << period << "uSec" << std::endl;
115  std::cout << "List length=" << counters.size() << std::endl;
116  }
117  // Start by calculating the length of the packet body in bytes. This will be at least 4.
118  uint32_t dataLength = static_cast<uint32_t>(4 + (counters.size() * 2));
119 
120  std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength);
121  unsigned char* data = reinterpret_cast<unsigned char*>(uniqueData.get());
122 
123  uint32_t offset = 0;
124  arm::pipe::WriteUint32(data, offset, period);
125  offset += 4;
126  for (std::vector<uint16_t>::iterator it = counters.begin(); it != counters.end(); ++it)
127  {
128  arm::pipe::WriteUint16(data, offset, *it);
129  offset += 2;
130  }
131 
132  // Send the packet.
133  m_BasePipeServer.get()->SendPacket(0, 4, data, dataLength);
134  // There will be an echo response packet sitting in the receive thread. PeriodicCounterSelectionResponseHandler
135  // should deal with it.
136 }
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 34 of file GatordMockService.cpp.

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

35 {
36  if (m_EchoPackets)
37  {
38  std::cout << "Sending connection acknowledgement." << std::endl;
39  }
40  // The request counter directory packet is an empty data packet with packetId == 3.
41  m_BasePipeServer.get()->SendPacket(0, 3, nullptr, 0);
42 }

◆ WaitCommand()

void WaitCommand ( uint32_t  timeout)

Execute the WAIT command from the comamnd file.

Definition at line 138 of file GatordMockService.cpp.

References GatordMockService::ReceiveThreadRunning().

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

139 {
140  // Wait for a maximum of timeout microseconds or if the receive thread has closed.
141  // There is a certain level of rounding involved in this timing.
142  uint32_t iterations = timeout / 1000;
143  std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations = " << iterations << std::endl;
144  uint32_t count = 0;
145  while ((this->ReceiveThreadRunning() && (count < iterations)))
146  {
147  std::this_thread::sleep_for(std::chrono::microseconds(1000));
148  ++count;
149  }
150  if (m_EchoPackets)
151  {
152  std::cout << std::dec << "Wait command with timeout of " << timeout << " microseconds completed. " << std::endl;
153  }
154 }

◆ 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 81 of file GatordMockService.cpp.

Referenced by CreateMockService(), and GatordMockService::GetPacketsReceivedCount().

82 {
83  // The receiving thread may already have died.
84  if (m_CloseReceivingThread != true)
85  {
86  m_CloseReceivingThread.store(true);
87  }
88  // Check that the receiving thread is running
89  if (m_ListeningThread.joinable())
90  {
91  // Wait for the receiving thread to complete operations
92  m_ListeningThread.join();
93  }
94 
95  if(m_EchoPackets)
96  {
97  m_TimelineDecoder.print();
98  }
99 }

◆ 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 101 of file GatordMockService.cpp.

Referenced by CreateMockService(), and GatordMockService::GatordMockService().

102 {
103  return m_BasePipeServer->WaitForStreamMetaData();
104 }

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