blob: fa483e0a51572e536a4b55195725f007397cacbc [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
Jim Flynnc454ac92022-03-16 18:43:18 +000019#include <server/include/timelineDecoder/DirectoryCaptureCommandHandler.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010020#include <server/include/timelineDecoder/TimelineDecoder.hpp>
21#include <server/include/timelineDecoder/TimelineCaptureCommandHandler.hpp>
22#include <server/include/timelineDecoder/TimelineDirectoryCaptureCommandHandler.hpp>
23
Colm Donelanb682d842019-10-16 12:24:20 +010024#include <atomic>
Colm Donelana21620d2019-10-11 13:09:49 +010025#include <string>
26#include <thread>
Colm Donelana21620d2019-10-11 13:09:49 +010027
Colm Donelana21620d2019-10-11 13:09:49 +010028namespace armnn
29{
30
31namespace gatordmock
32{
33
Colm Donelana21620d2019-10-11 13:09:49 +010034/// 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 +000035/// namespace for client connections. It will then allow opertaions to manage counters while receiving counter data.
Colm Donelana21620d2019-10-11 13:09:49 +010036class GatordMockService
37{
38public:
Colm Donelana21620d2019-10-11 13:09:49 +010039 /// @param registry reference to a command handler registry.
40 /// @param echoPackets if true the raw packets will be printed to stdout.
Jim Flynnbbfe6032020-07-20 16:57:44 +010041 GatordMockService(std::unique_ptr<arm::pipe::BasePipeServer> clientConnection, bool echoPackets)
Finn Williams2ed809c2020-04-20 21:21:07 +010042 : m_BasePipeServer(std::move(clientConnection))
Finn Williamse6a2ccd2020-02-27 16:21:41 +000043 , m_EchoPackets(echoPackets)
44 , m_CloseReceivingThread(false)
Keith Davis33ed2212020-03-30 10:43:41 +010045 , m_PacketVersionResolver()
46 , m_HandlerRegistry()
47 , m_TimelineDecoder()
Keith Davis33ed2212020-03-30 10:43:41 +010048 , m_CounterCaptureCommandHandler(
Finn Williamsfe5a24b2020-04-09 16:05:28 +010049 0, 4, m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), !echoPackets)
Finn Williamsd7fcafa2020-04-23 17:55:18 +010050 , 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())
Keith Davis33ed2212020-03-30 10:43:41 +010054 , m_DirectoryCaptureCommandHandler(
Jim Flynn4a962112022-03-13 20:18:58 +000055 "ARMNN", 0, 2, m_PacketVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), !echoPackets)
Keith Davis33ed2212020-03-30 10:43:41 +010056 , 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(),
Finn Williamsfe5a24b2020-04-09 16:05:28 +010060 m_TimelineCaptureCommandHandler, !echoPackets)
Colm Donelana21620d2019-10-11 13:09:49 +010061 {
Keith Davis33ed2212020-03-30 10:43:41 +010062 m_TimelineDecoder.SetDefaultCallbacks();
63
Keith Davis33ed2212020-03-30 10:43:41 +010064 m_HandlerRegistry.RegisterFunctor(&m_CounterCaptureCommandHandler);
Finn Williamsd7fcafa2020-04-23 17:55:18 +010065 m_HandlerRegistry.RegisterFunctor(&m_StreamMetadataCommandHandler);
66 m_HandlerRegistry.RegisterFunctor(&m_StubCommandHandler);
Keith Davis33ed2212020-03-30 10:43:41 +010067 m_HandlerRegistry.RegisterFunctor(&m_DirectoryCaptureCommandHandler);
68 m_HandlerRegistry.RegisterFunctor(&m_TimelineDirectoryCaptureCommandHandler);
69 m_HandlerRegistry.RegisterFunctor(&m_TimelineCaptureCommandHandler);
Colm Donelana21620d2019-10-11 13:09:49 +010070 }
71
Finn Williams2ed809c2020-04-20 21:21:07 +010072 GatordMockService(const GatordMockService&) = delete;
73 GatordMockService& operator=(const GatordMockService&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010074
Finn Williams2ed809c2020-04-20 21:21:07 +010075 GatordMockService(GatordMockService&&) = delete;
76 GatordMockService& operator=(GatordMockService&&) = delete;
Colm Donelana21620d2019-10-11 13:09:49 +010077
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
Finn Williams15db7452019-10-15 14:22:13 +010086 /// Send a request counter directory packet back to the client.
87 void SendRequestCounterDir();
88
Keith Davis33ed2212020-03-30 10:43:41 +010089 /// 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
Colm Donelana21620d2019-10-11 13:09:49 +010095 /// 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.
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
Colm Donelan02705242019-11-14 14:19:07 +0000109 // @return true only if the receive thread is closed or closing.
110 bool ReceiveThreadRunning()
111 {
112 return !m_CloseReceivingThread.load();
113 }
114
Colm Donelana21620d2019-10-11 13:09:49 +0100115 /// Send the counter list to ArmNN.
Colm Donelanb682d842019-10-16 12:24:20 +0100116 void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters);
Colm Donelana21620d2019-10-11 13:09:49 +0100117
118 /// Execute the WAIT command from the comamnd file.
Rob Hughes270233f2019-11-13 11:53:48 +0000119 void WaitCommand(uint32_t timeout);
Colm Donelana21620d2019-10-11 13:09:49 +0100120
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000121 arm::pipe::DirectoryCaptureCommandHandler& GetDirectoryCaptureCommandHandler()
Keith Davis33ed2212020-03-30 10:43:41 +0100122 {
123 return m_DirectoryCaptureCommandHandler;
124 }
125
Jim Flynnbbfe6032020-07-20 16:57:44 +0100126 arm::pipe::TimelineDecoder& GetTimelineDecoder()
Keith Davis33ed2212020-03-30 10:43:41 +0100127 {
128 return m_TimelineDecoder;
129 }
130
Jim Flynnbbfe6032020-07-20 16:57:44 +0100131 arm::pipe::TimelineDirectoryCaptureCommandHandler& GetTimelineDirectoryCaptureCommandHandler()
Keith Davis33ed2212020-03-30 10:43:41 +0100132 {
133 return m_TimelineDirectoryCaptureCommandHandler;
134 }
135
Colm Donelana21620d2019-10-11 13:09:49 +0100136private:
Finn Williams2ed809c2020-04-20 21:21:07 +0100137 void ReceiveLoop();
Colm Donelana21620d2019-10-11 13:09:49 +0100138
Jim Flynnbbfe6032020-07-20 16:57:44 +0100139 std::unique_ptr<arm::pipe::BasePipeServer> m_BasePipeServer;
Finn Williamse6a2ccd2020-02-27 16:21:41 +0000140
Keith Davis33ed2212020-03-30 10:43:41 +0100141 std::atomic<uint32_t> m_PacketsReceivedCount;
Colm Donelana21620d2019-10-11 13:09:49 +0100142
143 bool m_EchoPackets;
Colm Donelana21620d2019-10-11 13:09:49 +0100144 std::thread m_ListeningThread;
145 std::atomic<bool> m_CloseReceivingThread;
Keith Davis33ed2212020-03-30 10:43:41 +0100146
Jim Flynnbbfe6032020-07-20 16:57:44 +0100147 arm::pipe::PacketVersionResolver m_PacketVersionResolver;
148 arm::pipe::CommandHandlerRegistry m_HandlerRegistry;
Keith Davis33ed2212020-03-30 10:43:41 +0100149
Jim Flynnbbfe6032020-07-20 16:57:44 +0100150 arm::pipe::TimelineDecoder m_TimelineDecoder;
Keith Davis33ed2212020-03-30 10:43:41 +0100151
Keith Davis33ed2212020-03-30 10:43:41 +0100152 gatordmock::PeriodicCounterCaptureCommandHandler m_CounterCaptureCommandHandler;
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100153 gatordmock::StreamMetadataCommandHandler m_StreamMetadataCommandHandler;
154 gatordmock::StubCommandHandler m_StubCommandHandler;
Keith Davis33ed2212020-03-30 10:43:41 +0100155
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000156 arm::pipe::DirectoryCaptureCommandHandler m_DirectoryCaptureCommandHandler;
Keith Davis33ed2212020-03-30 10:43:41 +0100157
Jim Flynnbbfe6032020-07-20 16:57:44 +0100158 arm::pipe::TimelineCaptureCommandHandler m_TimelineCaptureCommandHandler;
159 arm::pipe::TimelineDirectoryCaptureCommandHandler m_TimelineDirectoryCaptureCommandHandler;
Colm Donelana21620d2019-10-11 13:09:49 +0100160};
Colm Donelanb682d842019-10-16 12:24:20 +0100161} // namespace gatordmock
Colm Donelana21620d2019-10-11 13:09:49 +0100162
Colm Donelanb682d842019-10-16 12:24:20 +0100163} // namespace armnn