ArmNN  NotReleased
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 
38 {
39 public:
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 
60  bool OpenListeningSocket(std::string udsNamespace);
61 
64  armnnUtils::Sockets::Socket BlockForOneClient();
65 
69  bool WaitForStreamMetaData();
70 
72  void SendConnectionAck();
73 
75  void SendRequestCounterDir();
76 
78  bool LaunchReceivingThread();
79 
83  {
84  return m_PacketsReceivedCount.load(std::memory_order_acquire);
85  }
86 
89  void WaitForReceivingThread();
90 
91  // @return true only if the receive thread is closed or closing.
93  {
94  return !m_CloseReceivingThread.load();
95  }
96 
98  void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
99 
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 
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
DataLayout::NHWC false
GatordMockService(armnn::profiling::CommandHandlerRegistry &registry, bool echoPackets)