blob: 30c5444f4e0dffa2863412a4a93830951d66d7a5 [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Colm Donelana21620d2019-10-11 13:09:49 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Jim Flynnbbfe6032020-07-20 16:57:44 +01008// 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/TimelineDecoder.hpp>
20#include <server/include/timelineDecoder/TimelineCaptureCommandHandler.hpp>
21#include <server/include/timelineDecoder/TimelineDirectoryCaptureCommandHandler.hpp>
22
23// src/profiling
24#include <DirectoryCaptureCommandHandler.hpp>
Matteo Martincigh65984272019-10-17 13:26:21 +010025
Colm Donelanb682d842019-10-16 12:24:20 +010026#include <atomic>
Colm Donelana21620d2019-10-11 13:09:49 +010027#include <string>
28#include <thread>
Colm Donelana21620d2019-10-11 13:09:49 +010029
Colm Donelana21620d2019-10-11 13:09:49 +010030namespace armnn
31{
32
33namespace gatordmock
34{
35
Colm Donelana21620d2019-10-11 13:09:49 +010036/// A class that implements a Mock Gatord server. It will listen on a specified Unix domain socket (UDS)
Matthew Bentham6ae43c42022-01-10 13:34:12 +000037/// namespace for client connections. It will then allow opertaions to manage counters while receiving counter data.
Colm Donelana21620d2019-10-11 13:09:49 +010038class GatordMockService
39{
40public:
Colm Donelana21620d2019-10-11 13:09:49 +010041 /// @param registry reference to a command handler registry.
42 /// @param echoPackets if true the raw packets will be printed to stdout.
Jim Flynnbbfe6032020-07-20 16:57:44 +010043 GatordMockService(std::unique_ptr<arm::pipe::BasePipeServer> clientConnection, bool echoPackets)
Finn Williams2ed809c2020-04-20 21:21:07 +010044 : m_BasePipeServer(std::move(clientConnection))
Finn Williamse6a2ccd2020-02-27 16:21:41 +000045 , m_EchoPackets(echoPackets)
46 , m_CloseReceivingThread(false)
Keith Davis33ed2212020-03-30 10:43:41 +010047 , m_PacketVersionResolver()
48 , m_HandlerRegistry()
49 , m_TimelineDecoder()
Keith Davis33ed2212020-03-30 10:43:41 +010050 , m_CounterCaptureCommandHandler(
Finn Williamsfe5a24b2020-04-09 16:05:28 +010051 0, 4, m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), !echoPackets)
Finn Williamsd7fcafa2020-04-23 17:55:18 +010052 , 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())
Keith Davis33ed2212020-03-30 10:43:41 +010056 , m_DirectoryCaptureCommandHandler(
Finn Williamsfe5a24b2020-04-09 16:05:28 +010057 0, 2, m_PacketVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), !echoPackets)
Keith Davis33ed2212020-03-30 10:43:41 +010058 , 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(),
Finn Williamsfe5a24b2020-04-09 16:05:28 +010062 m_TimelineCaptureCommandHandler, !echoPackets)
Colm Donelana21620d2019-10-11 13:09:49 +010063 {
Keith Davis33ed2212020-03-30 10:43:41 +010064 m_TimelineDecoder.SetDefaultCallbacks();
65
Keith Davis33ed2212020-03-30 10:43:41 +010066 m_HandlerRegistry.RegisterFunctor(&m_CounterCaptureCommandHandler);
Finn Williamsd7fcafa2020-04-23 17:55:18 +010067 m_HandlerRegistry.RegisterFunctor(&m_StreamMetadataCommandHandler);
68 m_HandlerRegistry.RegisterFunctor(&m_StubCommandHandler);
Keith Davis33ed2212020-03-30 10:43:41 +010069 m_HandlerRegistry.RegisterFunctor(&m_DirectoryCaptureCommandHandler);
70 m_HandlerRegistry.RegisterFunctor(&m_TimelineDirectoryCaptureCommandHandler);
71 m_HandlerRegistry.RegisterFunctor(&m_TimelineCaptureCommandHandler);
Colm Donelana21620d2019-10-11 13:09:49 +010072 }
73
Finn Williams2ed809c2020-04-20 21:21:07 +010074 GatordMockService(const GatordMockService&) = delete;
75 GatordMockService& operator=(const GatordMockService&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010076
Finn Williams2ed809c2020-04-20 21:21:07 +010077 GatordMockService(GatordMockService&&) = delete;
78 GatordMockService& operator=(GatordMockService&&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010079
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
Finn Williams15db7452019-10-15 14:22:13 +010088 /// Send a request counter directory packet back to the client.
89 void SendRequestCounterDir();
90
Keith Davis33ed2212020-03-30 10:43:41 +010091 /// Send a activate timeline packet back to the client.
92 void SendActivateTimelinePacket();
93
94 /// Send a deactivate timeline packet back to the client.
95 void SendDeactivateTimelinePacket();
96
Colm Donelana21620d2019-10-11 13:09:49 +010097 /// 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.
102 uint32_t GetPacketsReceivedCount()
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
Colm Donelan02705242019-11-14 14:19:07 +0000111 // @return true only if the receive thread is closed or closing.
112 bool ReceiveThreadRunning()
113 {
114 return !m_CloseReceivingThread.load();
115 }
116
Colm Donelana21620d2019-10-11 13:09:49 +0100117 /// Send the counter list to ArmNN.
Colm Donelanb682d842019-10-16 12:24:20 +0100118 void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
Colm Donelana21620d2019-10-11 13:09:49 +0100119
120 /// Execute the WAIT command from the comamnd file.
Rob Hughes270233f2019-11-13 11:53:48 +0000121 void WaitCommand(uint32_t timeout);
Colm Donelana21620d2019-10-11 13:09:49 +0100122
Keith Davis33ed2212020-03-30 10:43:41 +0100123 profiling::DirectoryCaptureCommandHandler& GetDirectoryCaptureCommandHandler()
124 {
125 return m_DirectoryCaptureCommandHandler;
126 }
127
Jim Flynnbbfe6032020-07-20 16:57:44 +0100128 arm::pipe::TimelineDecoder& GetTimelineDecoder()
Keith Davis33ed2212020-03-30 10:43:41 +0100129 {
130 return m_TimelineDecoder;
131 }
132
Jim Flynnbbfe6032020-07-20 16:57:44 +0100133 arm::pipe::TimelineDirectoryCaptureCommandHandler& GetTimelineDirectoryCaptureCommandHandler()
Keith Davis33ed2212020-03-30 10:43:41 +0100134 {
135 return m_TimelineDirectoryCaptureCommandHandler;
136 }
137
Colm Donelana21620d2019-10-11 13:09:49 +0100138private:
Finn Williams2ed809c2020-04-20 21:21:07 +0100139 void ReceiveLoop();
Colm Donelana21620d2019-10-11 13:09:49 +0100140
Jim Flynnbbfe6032020-07-20 16:57:44 +0100141 std::unique_ptr<arm::pipe::BasePipeServer> m_BasePipeServer;
Finn Williamse6a2ccd2020-02-27 16:21:41 +0000142
Keith Davis33ed2212020-03-30 10:43:41 +0100143 std::atomic<uint32_t> m_PacketsReceivedCount;
Colm Donelana21620d2019-10-11 13:09:49 +0100144
145 bool m_EchoPackets;
Colm Donelana21620d2019-10-11 13:09:49 +0100146 std::thread m_ListeningThread;
147 std::atomic<bool> m_CloseReceivingThread;
Keith Davis33ed2212020-03-30 10:43:41 +0100148
Jim Flynnbbfe6032020-07-20 16:57:44 +0100149 arm::pipe::PacketVersionResolver m_PacketVersionResolver;
150 arm::pipe::CommandHandlerRegistry m_HandlerRegistry;
Keith Davis33ed2212020-03-30 10:43:41 +0100151
Jim Flynnbbfe6032020-07-20 16:57:44 +0100152 arm::pipe::TimelineDecoder m_TimelineDecoder;
Keith Davis33ed2212020-03-30 10:43:41 +0100153
Keith Davis33ed2212020-03-30 10:43:41 +0100154 gatordmock::PeriodicCounterCaptureCommandHandler m_CounterCaptureCommandHandler;
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100155 gatordmock::StreamMetadataCommandHandler m_StreamMetadataCommandHandler;
156 gatordmock::StubCommandHandler m_StubCommandHandler;
Keith Davis33ed2212020-03-30 10:43:41 +0100157
158 profiling::DirectoryCaptureCommandHandler m_DirectoryCaptureCommandHandler;
159
Jim Flynnbbfe6032020-07-20 16:57:44 +0100160 arm::pipe::TimelineCaptureCommandHandler m_TimelineCaptureCommandHandler;
161 arm::pipe::TimelineDirectoryCaptureCommandHandler m_TimelineDirectoryCaptureCommandHandler;
Colm Donelana21620d2019-10-11 13:09:49 +0100162};
Colm Donelanb682d842019-10-16 12:24:20 +0100163} // namespace gatordmock
Colm Donelana21620d2019-10-11 13:09:49 +0100164
Colm Donelanb682d842019-10-16 12:24:20 +0100165} // namespace armnn