2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
8 #include "BufferManager.hpp"
9 #include "CommandHandler.hpp"
10 #include "ConnectionAcknowledgedCommandHandler.hpp"
11 #include "CounterDirectory.hpp"
12 #include "ICounterValues.hpp"
13 #include "PeriodicCounterCapture.hpp"
14 #include "PeriodicCounterSelectionCommandHandler.hpp"
15 #include "PerJobCounterSelectionCommandHandler.hpp"
16 #include "ProfilingConnectionFactory.hpp"
17 #include "ProfilingGuidGenerator.hpp"
18 #include "ProfilingStateMachine.hpp"
19 #include "RequestCounterDirectoryCommandHandler.hpp"
20 #include "SendCounterPacket.hpp"
21 #include "SendTimelinePacket.hpp"
22 #include "TimelinePacketWriterFactory.hpp"
30 class ProfilingService : public IReadWriteCounterValues, public IProfilingGuidGenerator
33 using ExternalProfilingOptions = IRuntime::CreationOptions::ExternalProfilingOptions;
34 using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>;
35 using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>;
36 using CounterIndices = std::vector<std::atomic<uint32_t>*>;
37 using CounterValues = std::list<std::atomic<uint32_t>>;
39 // Getter for the singleton instance
40 static ProfilingService& Instance()
42 static ProfilingService instance;
46 // Resets the profiling options, optionally clears the profiling service entirely
47 void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false);
48 ProfilingState ConfigureProfilingService(const ExternalProfilingOptions& options,
49 bool resetProfilingService = false);
52 // Updates the profiling service, making it transition to a new state if necessary
55 // Disconnects the profiling service from the external server
58 // Getters for the profiling service state
59 const ICounterDirectory& GetCounterDirectory() const;
60 ProfilingState GetCurrentState() const;
61 bool IsCounterRegistered(uint16_t counterUid) const override;
62 uint16_t GetCounterCount() const override;
63 uint32_t GetCounterValue(uint16_t counterUid) const override;
65 // Setters for the profiling service state
66 void SetCounterValue(uint16_t counterUid, uint32_t value) override;
67 uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override;
68 uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override;
69 uint32_t IncrementCounterValue(uint16_t counterUid) override;
70 uint32_t DecrementCounterValue(uint16_t counterUid) override;
72 // IProfilingGuidGenerator functions
73 /// Return the next random Guid in the sequence
74 ProfilingDynamicGuid NextGuid() override;
75 /// Create a ProfilingStaticGuid based on a hash of the string
76 ProfilingStaticGuid GenerateStaticId(const std::string& str) override;
78 std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const;
80 /// Check if the profiling is enabled
81 bool IsEnabled() { return m_Options.m_EnableProfiling; }
84 // Copy/move constructors/destructors and copy/move assignment operators are deleted
85 ProfilingService(const ProfilingService&) = delete;
86 ProfilingService(ProfilingService&&) = delete;
87 ProfilingService& operator=(const ProfilingService&) = delete;
88 ProfilingService& operator=(ProfilingService&&) = delete;
90 // Initialization/reset functions
92 void InitializeCounterValue(uint16_t counterUid);
97 void CheckCounterUid(uint16_t counterUid) const;
99 // Profiling service components
100 ExternalProfilingOptions m_Options;
101 CounterDirectory m_CounterDirectory;
102 IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory;
103 IProfilingConnectionPtr m_ProfilingConnection;
104 ProfilingStateMachine m_StateMachine;
105 CounterIndices m_CounterIndex;
106 CounterValues m_CounterValues;
107 CommandHandlerRegistry m_CommandHandlerRegistry;
108 PacketVersionResolver m_PacketVersionResolver;
109 CommandHandler m_CommandHandler;
110 BufferManager m_BufferManager;
111 SendCounterPacket m_SendCounterPacket;
112 SendTimelinePacket m_SendTimelinePacket;
114 PeriodicCounterCapture m_PeriodicCounterCapture;
115 ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
116 RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler;
117 PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler;
118 PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler;
119 ProfilingGuidGenerator m_GuidGenerator;
120 TimelinePacketWriterFactory m_TimelinePacketWriterFactory;
123 // Default constructor/destructor kept protected for testing
126 , m_CounterDirectory()
127 , m_ProfilingConnectionFactory(new ProfilingConnectionFactory())
128 , m_ProfilingConnection()
132 , m_CommandHandlerRegistry()
133 , m_PacketVersionResolver()
134 , m_CommandHandler(1000,
136 m_CommandHandlerRegistry,
137 m_PacketVersionResolver)
139 , m_SendCounterPacket(m_StateMachine, m_BufferManager)
140 , m_SendTimelinePacket(m_BufferManager)
141 , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this)
142 , m_ConnectionAcknowledgedCommandHandler(0,
144 m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(),
147 m_SendTimelinePacket,
149 , m_RequestCounterDirectoryCommandHandler(0,
151 m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(),
154 m_SendTimelinePacket,
156 , m_PeriodicCounterSelectionCommandHandler(0,
158 m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
160 m_PeriodicCounterCapture,
164 , m_PerJobCounterSelectionCommandHandler(0,
166 m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(),
168 , m_TimelinePacketWriterFactory(m_BufferManager)
170 // Register the "Connection Acknowledged" command handler
171 m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler);
173 // Register the "Request Counter Directory" command handler
174 m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler);
176 // Register the "Periodic Counter Selection" command handler
177 m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler);
179 // Register the "Per-Job Counter Selection" command handler
180 m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler);
184 // Protected methods for testing
185 void SwapProfilingConnectionFactory(ProfilingService& instance,
186 IProfilingConnectionFactory* other,
187 IProfilingConnectionFactory*& backup)
189 BOOST_ASSERT(instance.m_ProfilingConnectionFactory);
192 backup = instance.m_ProfilingConnectionFactory.release();
193 instance.m_ProfilingConnectionFactory.reset(other);
195 IProfilingConnection* GetProfilingConnection(ProfilingService& instance)
197 return instance.m_ProfilingConnection.get();
199 void TransitionToState(ProfilingService& instance, ProfilingState newState)
201 instance.m_StateMachine.TransitionToState(newState);
203 void WaitForPacketSent(ProfilingService& instance, uint32_t timeout = 1000)
205 return instance.m_SendCounterPacket.WaitForPacketSent(timeout);
208 BufferManager& GetBufferManager(ProfilingService& instance)
210 return instance.m_BufferManager;
214 } // namespace profiling