blob: 8bad41cdfb1a4ac4733efe050d756bb9c4659c76 [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matteo Martincigh65984272019-10-17 13:26:21 +01008#include <CommandHandlerRegistry.hpp>
9#include <Packet.hpp>
10
Colm Donelanb682d842019-10-16 12:24:20 +010011#include <atomic>
Colm Donelana21620d2019-10-11 13:09:49 +010012#include <string>
13#include <thread>
Colm Donelana21620d2019-10-11 13:09:49 +010014
Keith Davis33ed2212020-03-30 10:43:41 +010015#include <TimelineDecoder.hpp>
16#include <DirectoryCaptureCommandHandler.hpp>
17#include <TimelineCaptureCommandHandler.hpp>
18#include <TimelineDirectoryCaptureCommandHandler.hpp>
19#include "PeriodicCounterCaptureCommandHandler.hpp"
20#include "StreamMetadataCommandHandler.hpp"
21
Finn Williams2ed809c2020-04-20 21:21:07 +010022#include <BasePipeServer.hpp>
23
Keith Davis33ed2212020-03-30 10:43:41 +010024#include "PacketVersionResolver.hpp"
Finn Williamsd7fcafa2020-04-23 17:55:18 +010025#include "StubCommandHandler.hpp"
Keith Davis33ed2212020-03-30 10:43:41 +010026
Colm Donelana21620d2019-10-11 13:09:49 +010027namespace armnn
28{
29
30namespace gatordmock
31{
32
Colm Donelana21620d2019-10-11 13:09:49 +010033/// 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.
35class GatordMockService
36{
37public:
Colm Donelana21620d2019-10-11 13:09:49 +010038 /// @param registry reference to a command handler registry.
39 /// @param echoPackets if true the raw packets will be printed to stdout.
Finn Williams2ed809c2020-04-20 21:21:07 +010040 GatordMockService(std::unique_ptr<armnnProfiling::BasePipeServer> clientConnection, bool echoPackets)
41 : m_BasePipeServer(std::move(clientConnection))
Finn Williamse6a2ccd2020-02-27 16:21:41 +000042 , m_EchoPackets(echoPackets)
43 , m_CloseReceivingThread(false)
Keith Davis33ed2212020-03-30 10:43:41 +010044 , m_PacketVersionResolver()
45 , m_HandlerRegistry()
46 , m_TimelineDecoder()
Keith Davis33ed2212020-03-30 10:43:41 +010047 , m_CounterCaptureCommandHandler(
Finn Williamsfe5a24b2020-04-09 16:05:28 +010048 0, 4, m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), !echoPackets)
Finn Williamsd7fcafa2020-04-23 17:55:18 +010049 , 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())
Keith Davis33ed2212020-03-30 10:43:41 +010053 , m_DirectoryCaptureCommandHandler(
Finn Williamsfe5a24b2020-04-09 16:05:28 +010054 0, 2, m_PacketVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), !echoPackets)
Keith Davis33ed2212020-03-30 10:43:41 +010055 , 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(),
Finn Williamsfe5a24b2020-04-09 16:05:28 +010059 m_TimelineCaptureCommandHandler, !echoPackets)
Colm Donelana21620d2019-10-11 13:09:49 +010060 {
Keith Davis33ed2212020-03-30 10:43:41 +010061 m_TimelineDecoder.SetDefaultCallbacks();
62
Keith Davis33ed2212020-03-30 10:43:41 +010063 m_HandlerRegistry.RegisterFunctor(&m_CounterCaptureCommandHandler);
Finn Williamsd7fcafa2020-04-23 17:55:18 +010064 m_HandlerRegistry.RegisterFunctor(&m_StreamMetadataCommandHandler);
65 m_HandlerRegistry.RegisterFunctor(&m_StubCommandHandler);
Keith Davis33ed2212020-03-30 10:43:41 +010066 m_HandlerRegistry.RegisterFunctor(&m_DirectoryCaptureCommandHandler);
67 m_HandlerRegistry.RegisterFunctor(&m_TimelineDirectoryCaptureCommandHandler);
68 m_HandlerRegistry.RegisterFunctor(&m_TimelineCaptureCommandHandler);
Colm Donelana21620d2019-10-11 13:09:49 +010069 }
70
Finn Williams2ed809c2020-04-20 21:21:07 +010071 GatordMockService(const GatordMockService&) = delete;
72 GatordMockService& operator=(const GatordMockService&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010073
Finn Williams2ed809c2020-04-20 21:21:07 +010074 GatordMockService(GatordMockService&&) = delete;
75 GatordMockService& operator=(GatordMockService&&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010076
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
Finn Williams15db7452019-10-15 14:22:13 +010085 /// Send a request counter directory packet back to the client.
86 void SendRequestCounterDir();
87
Keith Davis33ed2212020-03-30 10:43:41 +010088 /// Send a activate timeline packet back to the client.
89 void SendActivateTimelinePacket();
90
91 /// Send a deactivate timeline packet back to the client.
92 void SendDeactivateTimelinePacket();
93
Colm Donelana21620d2019-10-11 13:09:49 +010094 /// 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.
99 uint32_t GetPacketsReceivedCount()
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
Colm Donelan02705242019-11-14 14:19:07 +0000108 // @return true only if the receive thread is closed or closing.
109 bool ReceiveThreadRunning()
110 {
111 return !m_CloseReceivingThread.load();
112 }
113
Colm Donelana21620d2019-10-11 13:09:49 +0100114 /// Send the counter list to ArmNN.
Colm Donelanb682d842019-10-16 12:24:20 +0100115 void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
Colm Donelana21620d2019-10-11 13:09:49 +0100116
117 /// Execute the WAIT command from the comamnd file.
Rob Hughes270233f2019-11-13 11:53:48 +0000118 void WaitCommand(uint32_t timeout);
Colm Donelana21620d2019-10-11 13:09:49 +0100119
Keith Davis33ed2212020-03-30 10:43:41 +0100120 profiling::DirectoryCaptureCommandHandler& GetDirectoryCaptureCommandHandler()
121 {
122 return m_DirectoryCaptureCommandHandler;
123 }
124
125 timelinedecoder::TimelineDecoder& GetTimelineDecoder()
126 {
127 return m_TimelineDecoder;
128 }
129
130 timelinedecoder::TimelineDirectoryCaptureCommandHandler& GetTimelineDirectoryCaptureCommandHandler()
131 {
132 return m_TimelineDirectoryCaptureCommandHandler;
133 }
134
Colm Donelana21620d2019-10-11 13:09:49 +0100135private:
Finn Williams2ed809c2020-04-20 21:21:07 +0100136 void ReceiveLoop();
Colm Donelana21620d2019-10-11 13:09:49 +0100137
Finn Williams2ed809c2020-04-20 21:21:07 +0100138 std::unique_ptr<armnnProfiling::BasePipeServer> m_BasePipeServer;
Finn Williamse6a2ccd2020-02-27 16:21:41 +0000139
Keith Davis33ed2212020-03-30 10:43:41 +0100140 std::atomic<uint32_t> m_PacketsReceivedCount;
Colm Donelana21620d2019-10-11 13:09:49 +0100141
142 bool m_EchoPackets;
Colm Donelana21620d2019-10-11 13:09:49 +0100143 std::thread m_ListeningThread;
144 std::atomic<bool> m_CloseReceivingThread;
Keith Davis33ed2212020-03-30 10:43:41 +0100145
146 profiling::PacketVersionResolver m_PacketVersionResolver;
147 profiling::CommandHandlerRegistry m_HandlerRegistry;
148
149 timelinedecoder::TimelineDecoder m_TimelineDecoder;
150
Keith Davis33ed2212020-03-30 10:43:41 +0100151 gatordmock::PeriodicCounterCaptureCommandHandler m_CounterCaptureCommandHandler;
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100152 gatordmock::StreamMetadataCommandHandler m_StreamMetadataCommandHandler;
153 gatordmock::StubCommandHandler m_StubCommandHandler;
Keith Davis33ed2212020-03-30 10:43:41 +0100154
155 profiling::DirectoryCaptureCommandHandler m_DirectoryCaptureCommandHandler;
156
157 timelinedecoder::TimelineCaptureCommandHandler m_TimelineCaptureCommandHandler;
158 timelinedecoder::TimelineDirectoryCaptureCommandHandler m_TimelineDirectoryCaptureCommandHandler;
Colm Donelana21620d2019-10-11 13:09:49 +0100159};
Colm Donelanb682d842019-10-16 12:24:20 +0100160} // namespace gatordmock
Colm Donelana21620d2019-10-11 13:09:49 +0100161
Colm Donelanb682d842019-10-16 12:24:20 +0100162} // namespace armnn