ArmNN
 20.05
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 
11 #include <atomic>
12 #include <string>
13 #include <thread>
14 
15 #include <TimelineDecoder.hpp>
21 
22 #include <BasePipeServer.hpp>
23 
25 #include "StubCommandHandler.hpp"
26 
27 namespace armnn
28 {
29 
30 namespace gatordmock
31 {
32 
33 /// A class that implements a Mock Gatord server. It will listen on a specified Unix domain socket (UDS)
34 /// namespace for client connections. It will then allow opertaions to manage coutners while receiving counter data.
36 {
37 public:
38  /// @param registry reference to a command handler registry.
39  /// @param echoPackets if true the raw packets will be printed to stdout.
40  GatordMockService(std::unique_ptr<armnnProfiling::BasePipeServer> clientConnection, bool echoPackets)
41  : m_BasePipeServer(std::move(clientConnection))
42  , m_EchoPackets(echoPackets)
43  , m_CloseReceivingThread(false)
44  , m_PacketVersionResolver()
45  , m_HandlerRegistry()
46  , m_TimelineDecoder()
47  , m_CounterCaptureCommandHandler(
48  0, 4, m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), !echoPackets)
49  , m_StreamMetadataCommandHandler(
50  0, 0, m_PacketVersionResolver.ResolvePacketVersion(0, 0).GetEncodedValue(), !echoPackets)
51  // This stub lets us ignore any counter capture packets we receive without throwing an error
52  , m_StubCommandHandler(3, 0, m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue())
53  , m_DirectoryCaptureCommandHandler(
54  0, 2, m_PacketVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), !echoPackets)
55  , m_TimelineCaptureCommandHandler(
56  1, 1, m_PacketVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), m_TimelineDecoder)
57  , m_TimelineDirectoryCaptureCommandHandler(
58  1, 0, m_PacketVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(),
59  m_TimelineCaptureCommandHandler, !echoPackets)
60  {
61  m_TimelineDecoder.SetDefaultCallbacks();
62 
63  m_HandlerRegistry.RegisterFunctor(&m_CounterCaptureCommandHandler);
64  m_HandlerRegistry.RegisterFunctor(&m_StreamMetadataCommandHandler);
65  m_HandlerRegistry.RegisterFunctor(&m_StubCommandHandler);
66  m_HandlerRegistry.RegisterFunctor(&m_DirectoryCaptureCommandHandler);
67  m_HandlerRegistry.RegisterFunctor(&m_TimelineDirectoryCaptureCommandHandler);
68  m_HandlerRegistry.RegisterFunctor(&m_TimelineCaptureCommandHandler);
69  }
70 
71  GatordMockService(const GatordMockService&) = delete;
73 
76 
77  /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
78  /// packet differs from others as we need to determine endianness.
79  /// @return true only if a valid stream met data packet has been received.
80  bool WaitForStreamMetaData();
81 
82  /// Send a connection acknowledged packet back to the client.
83  void SendConnectionAck();
84 
85  /// Send a request counter directory packet back to the client.
86  void SendRequestCounterDir();
87 
88  /// Send a activate timeline packet back to the client.
90 
91  /// Send a deactivate timeline packet back to the client.
93 
94  /// Start the thread that will receive all packets and print them nicely to stdout.
95  bool LaunchReceivingThread();
96 
97  /// Return the total number of periodic counter capture packets received since the receive thread started.
98  /// @return number of periodic counter capture packets received.
100  {
101  return m_PacketsReceivedCount.load(std::memory_order_acquire);
102  }
103 
104  /// This is a placeholder method to prevent main exiting. It can be removed once the
105  /// command handling code is added.
106  void WaitForReceivingThread();
107 
108  // @return true only if the receive thread is closed or closing.
110  {
111  return !m_CloseReceivingThread.load();
112  }
113 
114  /// Send the counter list to ArmNN.
115  void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
116 
117  /// Execute the WAIT command from the comamnd file.
118  void WaitCommand(uint32_t timeout);
119 
121  {
122  return m_DirectoryCaptureCommandHandler;
123  }
124 
126  {
127  return m_TimelineDecoder;
128  }
129 
131  {
132  return m_TimelineDirectoryCaptureCommandHandler;
133  }
134 
135 private:
136  void ReceiveLoop();
137 
138  std::unique_ptr<armnnProfiling::BasePipeServer> m_BasePipeServer;
139 
140  std::atomic<uint32_t> m_PacketsReceivedCount;
141 
142  bool m_EchoPackets;
143  std::thread m_ListeningThread;
144  std::atomic<bool> m_CloseReceivingThread;
145 
146  profiling::PacketVersionResolver m_PacketVersionResolver;
147  profiling::CommandHandlerRegistry m_HandlerRegistry;
148 
149  timelinedecoder::TimelineDecoder m_TimelineDecoder;
150 
151  gatordmock::PeriodicCounterCaptureCommandHandler m_CounterCaptureCommandHandler;
152  gatordmock::StreamMetadataCommandHandler m_StreamMetadataCommandHandler;
153  gatordmock::StubCommandHandler m_StubCommandHandler;
154 
155  profiling::DirectoryCaptureCommandHandler m_DirectoryCaptureCommandHandler;
156 
157  timelinedecoder::TimelineCaptureCommandHandler m_TimelineCaptureCommandHandler;
158  timelinedecoder::TimelineDirectoryCaptureCommandHandler m_TimelineDirectoryCaptureCommandHandler;
159 };
160 } // namespace gatordmock
161 
162 } // namespace armnn
DataLayout::NHWC false
Copyright (c) 2020 ARM Limited.
void SendDeactivateTimelinePacket()
Send a deactivate timeline packet back to the client.
timelinedecoder::TimelineDirectoryCaptureCommandHandler & GetTimelineDirectoryCaptureCommandHandler()
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.
void RegisterFunctor(CommandHandlerFunctor *functor, uint32_t familyId, uint32_t packetId, uint32_t version)
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.
GatordMockService(std::unique_ptr< armnnProfiling::BasePipeServer > clientConnection, bool echoPackets)
void WaitForReceivingThread()
This is a placeholder method to prevent main exiting.
timelinedecoder::TimelineDecoder & GetTimelineDecoder()
GatordMockService & operator=(const GatordMockService &)=delete
void SendConnectionAck()
Send a connection acknowledged packet back to the client.