ArmNN
 20.02
GatordMockService.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 #include <Packet.hpp>
10 #include <NetworkSockets.hpp>
11 
12 #include <atomic>
13 #include <string>
14 #include <thread>
15 
16 namespace armnn
17 {
18 
19 namespace gatordmock
20 {
21 
22 enum class TargetEndianness
23 {
24  BeWire,
25  LeWire
26 };
27 
28 enum class PacketDirection
29 {
30  Sending,
33 };
34 
35 /// A class that implements a Mock Gatord server. It will listen on a specified Unix domain socket (UDS)
36 /// namespace for client connections. It will then allow opertaions to manage coutners while receiving counter data.
38 {
39 public:
40  /// @param registry reference to a command handler registry.
41  /// @param echoPackets if true the raw packets will be printed to stdout.
43  : m_HandlerRegistry(registry)
44  , m_EchoPackets(echoPackets)
45  , m_CloseReceivingThread(false)
46  {
47  m_PacketsReceivedCount.store(0, std::memory_order_relaxed);
48  }
49 
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  }
56 
57  /// Establish the Unix domain socket and set it to listen for connections.
58  /// @param udsNamespace the namespace (socket address) associated with the listener.
59  /// @return true only if the socket has been correctly setup.
60  bool OpenListeningSocket(std::string udsNamespace);
61 
62  /// Block waiting to accept one client to connect to the UDS.
63  /// @return the file descriptor of the client connection.
64  armnnUtils::Sockets::Socket BlockForOneClient();
65 
66  /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
67  /// packet differs from others as we need to determine endianness.
68  /// @return true only if a valid stream met data packet has been received.
69  bool WaitForStreamMetaData();
70 
71  /// Send a connection acknowledged packet back to the client.
72  void SendConnectionAck();
73 
74  /// Send a request counter directory packet back to the client.
75  void SendRequestCounterDir();
76 
77  /// Start the thread that will receive all packets and print them nicely to stdout.
78  bool LaunchReceivingThread();
79 
80  /// Return the total number of periodic counter capture packets received since the receive thread started.
81  /// @return number of periodic counter capture packets received.
83  {
84  return m_PacketsReceivedCount.load(std::memory_order_acquire);
85  }
86 
87  /// This is a placeholder method to prevent main exiting. It can be removed once the
88  /// command handling code is added.
89  void WaitForReceivingThread();
90 
91  // @return true only if the receive thread is closed or closing.
93  {
94  return !m_CloseReceivingThread.load();
95  }
96 
97  /// Send the counter list to ArmNN.
98  void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
99 
100  /// Execute the WAIT command from the comamnd file.
101  void WaitCommand(uint32_t timeout);
102 
104  {
105  return m_StreamMetaDataVersion;
106  }
107 
109  {
110  return m_StreamMetaDataMaxDataLen;
111  }
112 
114  {
115  return m_StreamMetaDataPid;
116  }
117 
118 private:
119  void ReceiveLoop(GatordMockService& mockService);
120 
121  /// Block on the client connection until a complete packet has been received. This is a placeholder function to
122  /// enable early testing of the tool.
123  /// @return true if a valid packet has been received.
124  armnn::profiling::Packet WaitForPacket(uint32_t timeoutMs);
125 
126  armnn::profiling::Packet ReceivePacket();
127 
128  bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength);
129 
130  void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes);
131 
132  bool ReadHeader(uint32_t headerAsWords[2]);
133 
134  bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength);
135 
136  uint32_t ToUint32(uint8_t* data, TargetEndianness endianness);
137 
138  void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness);
139 
140  static const uint32_t PIPE_MAGIC = 0x45495434;
141 
142  std::atomic<uint32_t> m_PacketsReceivedCount;
143  TargetEndianness m_Endianness;
144  uint32_t m_StreamMetaDataVersion;
145  uint32_t m_StreamMetaDataMaxDataLen;
146  uint32_t m_StreamMetaDataPid;
147 
148  armnn::profiling::CommandHandlerRegistry& m_HandlerRegistry;
149 
150  bool m_EchoPackets;
151  armnnUtils::Sockets::Socket m_ListeningSocket;
152  armnnUtils::Sockets::Socket m_ClientConnection;
153  std::thread m_ListeningThread;
154  std::atomic<bool> m_CloseReceivingThread;
155 };
156 } // namespace gatordmock
157 
158 } // namespace armnn
Copyright (c) 2020 ARM Limited.
DataLayout::NHWC false
GatordMockService(armnn::profiling::CommandHandlerRegistry &registry, bool echoPackets)
A class that implements a Mock Gatord server.
uint32_t GetPacketsReceivedCount()
Return the total number of periodic counter capture packets received since the receive thread started...