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