blob: 1afcb1ca9721dcf8221355873fd055df45246e40 [file] [log] [blame]
Keith Davis02356de2019-08-26 18:28:17 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "ProfilingStateMachine.hpp"
9#include "ProfilingConnectionFactory.hpp"
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010010#include "CounterDirectory.hpp"
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010011#include "ICounterValues.hpp"
Matteo Martincigh54fb9572019-10-02 12:50:57 +010012#include "CommandHandler.hpp"
13#include "BufferManager.hpp"
14#include "SendCounterPacket.hpp"
Matteo Martincighe8485382019-10-10 14:08:21 +010015#include "PeriodicCounterCapture.hpp"
Matteo Martincigh54fb9572019-10-02 12:50:57 +010016#include "ConnectionAcknowledgedCommandHandler.hpp"
Matteo Martincigh8efc5002019-10-10 14:30:29 +010017#include "RequestCounterDirectoryCommandHandler.hpp"
Matteo Martincighe8485382019-10-10 14:08:21 +010018#include "PeriodicCounterSelectionCommandHandler.hpp"
Matteo Martincigh994b5342019-10-11 17:19:56 +010019#include "PerJobCounterSelectionCommandHandler.hpp"
Keith Davis02356de2019-08-26 18:28:17 +010020
21namespace armnn
22{
23
24namespace profiling
25{
26
Matteo Martincigh54fb9572019-10-02 12:50:57 +010027class ProfilingService : public IReadWriteCounterValues
Keith Davis02356de2019-08-26 18:28:17 +010028{
29public:
Matteo Martincigha84edee2019-10-02 12:50:57 +010030 using ExternalProfilingOptions = Runtime::CreationOptions::ExternalProfilingOptions;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010031 using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>;
Matteo Martincigha84edee2019-10-02 12:50:57 +010032 using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>;
33 using CounterIndices = std::vector<std::atomic<uint32_t>*>;
34 using CounterValues = std::list<std::atomic<uint32_t>>;
Keith Davis02356de2019-08-26 18:28:17 +010035
Matteo Martincigha84edee2019-10-02 12:50:57 +010036 // Getter for the singleton instance
37 static ProfilingService& Instance()
38 {
39 static ProfilingService instance;
40 return instance;
41 }
42
43 // Resets the profiling options, optionally clears the profiling service entirely
44 void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false);
45
Matteo Martincigh54fb9572019-10-02 12:50:57 +010046 // Updates the profiling service, making it transition to a new state if necessary
47 void Update();
Keith Davis02356de2019-08-26 18:28:17 +010048
Jim Flynn53e46992019-10-14 12:31:10 +010049 // Disconnects the profiling service from the external server
50 void Disconnect();
51
Matteo Martincigha84edee2019-10-02 12:50:57 +010052 // Getters for the profiling service state
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010053 const ICounterDirectory& GetCounterDirectory() const;
Keith Davis02356de2019-08-26 18:28:17 +010054 ProfilingState GetCurrentState() const;
Matteo Martincighe8485382019-10-10 14:08:21 +010055 bool IsCounterRegistered(uint16_t counterUid) const override;
Matteo Martincigha84edee2019-10-02 12:50:57 +010056 uint16_t GetCounterCount() const override;
57 uint32_t GetCounterValue(uint16_t counterUid) const override;
Keith Davis02356de2019-08-26 18:28:17 +010058
Matteo Martincigha84edee2019-10-02 12:50:57 +010059 // Setters for the profiling service state
60 void SetCounterValue(uint16_t counterUid, uint32_t value) override;
61 uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override;
62 uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override;
63 uint32_t IncrementCounterValue(uint16_t counterUid) override;
64 uint32_t DecrementCounterValue(uint16_t counterUid) override;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +010065
Keith Davis02356de2019-08-26 18:28:17 +010066private:
Matteo Martincigh54fb9572019-10-02 12:50:57 +010067 // Copy/move constructors/destructors and copy/move assignment operators are deleted
Matteo Martincigha84edee2019-10-02 12:50:57 +010068 ProfilingService(const ProfilingService&) = delete;
69 ProfilingService(ProfilingService&&) = delete;
70 ProfilingService& operator=(const ProfilingService&) = delete;
71 ProfilingService& operator=(ProfilingService&&) = delete;
Keith Davis02356de2019-08-26 18:28:17 +010072
Matteo Martincigh54fb9572019-10-02 12:50:57 +010073 // Initialization/reset functions
Matteo Martincigha84edee2019-10-02 12:50:57 +010074 void Initialize();
75 void InitializeCounterValue(uint16_t counterUid);
Matteo Martincigh54fb9572019-10-02 12:50:57 +010076 void Reset();
Jim Flynn53e46992019-10-14 12:31:10 +010077 void Stop();
Matteo Martincigha84edee2019-10-02 12:50:57 +010078
Matteo Martincighe8485382019-10-10 14:08:21 +010079 // Helper function
80 void CheckCounterUid(uint16_t counterUid) const;
81
Matteo Martincigh54fb9572019-10-02 12:50:57 +010082 // Profiling service components
Matteo Martincigha84edee2019-10-02 12:50:57 +010083 ExternalProfilingOptions m_Options;
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010084 CounterDirectory m_CounterDirectory;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010085 IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory;
Matteo Martincigha84edee2019-10-02 12:50:57 +010086 IProfilingConnectionPtr m_ProfilingConnection;
87 ProfilingStateMachine m_StateMachine;
88 CounterIndices m_CounterIndex;
89 CounterValues m_CounterValues;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010090 CommandHandlerRegistry m_CommandHandlerRegistry;
91 PacketVersionResolver m_PacketVersionResolver;
92 CommandHandler m_CommandHandler;
93 BufferManager m_BufferManager;
94 SendCounterPacket m_SendCounterPacket;
Matteo Martincighe8485382019-10-10 14:08:21 +010095 Holder m_Holder;
96 PeriodicCounterCapture m_PeriodicCounterCapture;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010097 ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
Matteo Martincigh8efc5002019-10-10 14:30:29 +010098 RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler;
Matteo Martincighe8485382019-10-10 14:08:21 +010099 PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler;
Matteo Martincigh994b5342019-10-11 17:19:56 +0100100 PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler;
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100101
102protected:
103 // Default constructor/destructor kept protected for testing
104 ProfilingService()
105 : m_Options()
106 , m_CounterDirectory()
107 , m_ProfilingConnectionFactory(new ProfilingConnectionFactory())
108 , m_ProfilingConnection()
109 , m_StateMachine()
110 , m_CounterIndex()
111 , m_CounterValues()
112 , m_CommandHandlerRegistry()
113 , m_PacketVersionResolver()
114 , m_CommandHandler(1000,
115 false,
116 m_CommandHandlerRegistry,
117 m_PacketVersionResolver)
118 , m_BufferManager()
119 , m_SendCounterPacket(m_StateMachine, m_BufferManager)
Matteo Martincighe8485382019-10-10 14:08:21 +0100120 , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this)
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100121 , m_ConnectionAcknowledgedCommandHandler(1,
122 m_PacketVersionResolver.ResolvePacketVersion(1).GetEncodedValue(),
123 m_StateMachine)
Matteo Martincigh8efc5002019-10-10 14:30:29 +0100124 , m_RequestCounterDirectoryCommandHandler(3,
125 m_PacketVersionResolver.ResolvePacketVersion(3).GetEncodedValue(),
126 m_CounterDirectory,
127 m_SendCounterPacket,
128 m_StateMachine)
Matteo Martincighe8485382019-10-10 14:08:21 +0100129 , m_PeriodicCounterSelectionCommandHandler(4,
130 m_PacketVersionResolver.ResolvePacketVersion(4).GetEncodedValue(),
131 m_Holder,
132 m_PeriodicCounterCapture,
133 *this,
134 m_SendCounterPacket,
135 m_StateMachine)
Matteo Martincigh994b5342019-10-11 17:19:56 +0100136 , m_PerJobCounterSelectionCommandHandler(5,
137 m_PacketVersionResolver.ResolvePacketVersion(4).GetEncodedValue(),
138 m_StateMachine)
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100139 {
140 // Register the "Connection Acknowledged" command handler
141 m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler);
Matteo Martincigh8efc5002019-10-10 14:30:29 +0100142
143 // Register the "Request Counter Directory" command handler
144 m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler);
Matteo Martincighe8485382019-10-10 14:08:21 +0100145
146 // Register the "Periodic Counter Selection" command handler
147 m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler);
Matteo Martincigh994b5342019-10-11 17:19:56 +0100148
149 // Register the "Per-Job Counter Selection" command handler
150 m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100151 }
152 ~ProfilingService() = default;
153
Matteo Martincighd0613b52019-10-09 16:47:04 +0100154 // Protected methods for testing
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100155 void SwapProfilingConnectionFactory(ProfilingService& instance,
156 IProfilingConnectionFactory* other,
157 IProfilingConnectionFactory*& backup)
158 {
159 BOOST_ASSERT(instance.m_ProfilingConnectionFactory);
160 BOOST_ASSERT(other);
161
162 backup = instance.m_ProfilingConnectionFactory.release();
163 instance.m_ProfilingConnectionFactory.reset(other);
164 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100165 IProfilingConnection* GetProfilingConnection(ProfilingService& instance)
166 {
167 return instance.m_ProfilingConnection.get();
168 }
Matteo Martincigh8efc5002019-10-10 14:30:29 +0100169 void TransitionToState(ProfilingService& instance, ProfilingState newState)
170 {
171 instance.m_StateMachine.TransitionToState(newState);
172 }
Matteo Martincighe8485382019-10-10 14:08:21 +0100173 void WaitForPacketSent(ProfilingService& instance)
174 {
175 return instance.m_SendCounterPacket.WaitForPacketSent();
176 }
Keith Davis02356de2019-08-26 18:28:17 +0100177};
178
179} // namespace profiling
180
Matteo Martincigha84edee2019-10-02 12:50:57 +0100181} // namespace armnn