ArmNN
 20.08
GatordMockService.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 // local includes
11 #include "StubCommandHandler.hpp"
12 
13 #include <common/include/CommandHandlerRegistry.hpp>
14 #include <common/include/Packet.hpp>
15 #include <common/include/PacketVersionResolver.hpp>
16 
17 #include <server/include/basePipeServer/BasePipeServer.hpp>
18 
19 #include <server/include/timelineDecoder/TimelineDecoder.hpp>
20 #include <server/include/timelineDecoder/TimelineCaptureCommandHandler.hpp>
21 #include <server/include/timelineDecoder/TimelineDirectoryCaptureCommandHandler.hpp>
22 
23 // src/profiling
25 
26 #include <atomic>
27 #include <string>
28 #include <thread>
29 
30 namespace armnn
31 {
32 
33 namespace gatordmock
34 {
35 
36 /// A class that implements a Mock Gatord server. It will listen on a specified Unix domain socket (UDS)
37 /// namespace for client connections. It will then allow opertaions to manage coutners while receiving counter data.
39 {
40 public:
41  /// @param registry reference to a command handler registry.
42  /// @param echoPackets if true the raw packets will be printed to stdout.
43  GatordMockService(std::unique_ptr<arm::pipe::BasePipeServer> clientConnection, bool echoPackets)
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  }
73 
74  GatordMockService(const GatordMockService&) = delete;
76 
79 
80  /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
81  /// packet differs from others as we need to determine endianness.
82  /// @return true only if a valid stream met data packet has been received.
83  bool WaitForStreamMetaData();
84 
85  /// Send a connection acknowledged packet back to the client.
86  void SendConnectionAck();
87 
88  /// Send a request counter directory packet back to the client.
89  void SendRequestCounterDir();
90 
91  /// Send a activate timeline packet back to the client.
93 
94  /// Send a deactivate timeline packet back to the client.
96 
97  /// Start the thread that will receive all packets and print them nicely to stdout.
98  bool LaunchReceivingThread();
99 
100  /// Return the total number of periodic counter capture packets received since the receive thread started.
101  /// @return number of periodic counter capture packets received.
103  {
104  return m_PacketsReceivedCount.load(std::memory_order_acquire);
105  }
106 
107  /// This is a placeholder method to prevent main exiting. It can be removed once the
108  /// command handling code is added.
109  void WaitForReceivingThread();
110 
111  // @return true only if the receive thread is closed or closing.
113  {
114  return !m_CloseReceivingThread.load();
115  }
116 
117  /// Send the counter list to ArmNN.
118  void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
119 
120  /// Execute the WAIT command from the comamnd file.
121  void WaitCommand(uint32_t timeout);
122 
124  {
125  return m_DirectoryCaptureCommandHandler;
126  }
127 
128  arm::pipe::TimelineDecoder& GetTimelineDecoder()
129  {
130  return m_TimelineDecoder;
131  }
132 
133  arm::pipe::TimelineDirectoryCaptureCommandHandler& GetTimelineDirectoryCaptureCommandHandler()
134  {
135  return m_TimelineDirectoryCaptureCommandHandler;
136  }
137 
138 private:
139  void ReceiveLoop();
140 
141  std::unique_ptr<arm::pipe::BasePipeServer> m_BasePipeServer;
142 
143  std::atomic<uint32_t> m_PacketsReceivedCount;
144 
145  bool m_EchoPackets;
146  std::thread m_ListeningThread;
147  std::atomic<bool> m_CloseReceivingThread;
148 
149  arm::pipe::PacketVersionResolver m_PacketVersionResolver;
150  arm::pipe::CommandHandlerRegistry m_HandlerRegistry;
151 
152  arm::pipe::TimelineDecoder m_TimelineDecoder;
153 
154  gatordmock::PeriodicCounterCaptureCommandHandler m_CounterCaptureCommandHandler;
155  gatordmock::StreamMetadataCommandHandler m_StreamMetadataCommandHandler;
156  gatordmock::StubCommandHandler m_StubCommandHandler;
157 
158  profiling::DirectoryCaptureCommandHandler m_DirectoryCaptureCommandHandler;
159 
160  arm::pipe::TimelineCaptureCommandHandler m_TimelineCaptureCommandHandler;
161  arm::pipe::TimelineDirectoryCaptureCommandHandler m_TimelineDirectoryCaptureCommandHandler;
162 };
163 } // namespace gatordmock
164 
165 } // namespace armnn
arm::pipe::TimelineDirectoryCaptureCommandHandler & GetTimelineDirectoryCaptureCommandHandler()
DataLayout::NHWC false
Copyright (c) 2020 ARM Limited.
void SendDeactivateTimelinePacket()
Send a deactivate timeline packet back to the client.
void SendActivateTimelinePacket()
Send a activate timeline packet back to the client.
bool WaitForStreamMetaData()
Once the connection is open wait to receive the stream meta data packet from the client.
void SendRequestCounterDir()
Send a request counter directory packet back to the client.
arm::pipe::TimelineDecoder & GetTimelineDecoder()
bool LaunchReceivingThread()
Start the thread that will receive all packets and print them nicely to stdout.
profiling::DirectoryCaptureCommandHandler & GetDirectoryCaptureCommandHandler()
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...
void WaitCommand(uint32_t timeout)
Execute the WAIT command from the comamnd file.
void SendPeriodicCounterSelectionList(uint32_t period, std::vector< uint16_t > counters)
Send the counter list to ArmNN.
void WaitForReceivingThread()
This is a placeholder method to prevent main exiting.
GatordMockService(std::unique_ptr< arm::pipe::BasePipeServer > clientConnection, bool echoPackets)
GatordMockService & operator=(const GatordMockService &)=delete
void SendConnectionAck()
Send a connection acknowledged packet back to the client.