IVGCVSW-4171 Fix intermittent failure on FileOnlyProfilingDecoratorTests
[platform/upstream/armnn.git] / src / profiling / ProfilingService.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
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"
23
24 namespace armnn
25 {
26
27 namespace profiling
28 {
29
30 class ProfilingService : public IReadWriteCounterValues, public IProfilingGuidGenerator
31 {
32 public:
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>>;
38
39     // Getter for the singleton instance
40     static ProfilingService& Instance()
41     {
42         static ProfilingService instance;
43         return instance;
44     }
45
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);
50
51
52     // Updates the profiling service, making it transition to a new state if necessary
53     void Update();
54
55     // Disconnects the profiling service from the external server
56     void Disconnect();
57
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;
64
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;
71
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;
77
78     std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const;
79
80     /// Check if the profiling is enabled
81     bool IsEnabled() { return m_Options.m_EnableProfiling; }
82
83 private:
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;
89
90     // Initialization/reset functions
91     void Initialize();
92     void InitializeCounterValue(uint16_t counterUid);
93     void Reset();
94     void Stop();
95
96     // Helper function
97     void CheckCounterUid(uint16_t counterUid) const;
98
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;
113     Holder m_Holder;
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;
121
122 protected:
123     // Default constructor/destructor kept protected for testing
124     ProfilingService()
125         : m_Options()
126         , m_CounterDirectory()
127         , m_ProfilingConnectionFactory(new ProfilingConnectionFactory())
128         , m_ProfilingConnection()
129         , m_StateMachine()
130         , m_CounterIndex()
131         , m_CounterValues()
132         , m_CommandHandlerRegistry()
133         , m_PacketVersionResolver()
134         , m_CommandHandler(1000,
135                            false,
136                            m_CommandHandlerRegistry,
137                            m_PacketVersionResolver)
138         , m_BufferManager()
139         , m_SendCounterPacket(m_StateMachine, m_BufferManager)
140         , m_SendTimelinePacket(m_BufferManager)
141         , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this)
142         , m_ConnectionAcknowledgedCommandHandler(0,
143                                                  1,
144                                                  m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(),
145                                                  m_CounterDirectory,
146                                                  m_SendCounterPacket,
147                                                  m_SendTimelinePacket,
148                                                  m_StateMachine)
149         , m_RequestCounterDirectoryCommandHandler(0,
150                                                   3,
151                                                   m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(),
152                                                   m_CounterDirectory,
153                                                   m_SendCounterPacket,
154                                                   m_SendTimelinePacket,
155                                                   m_StateMachine)
156         , m_PeriodicCounterSelectionCommandHandler(0,
157                                                    4,
158                                                    m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
159                                                    m_Holder,
160                                                    m_PeriodicCounterCapture,
161                                                    *this,
162                                                    m_SendCounterPacket,
163                                                    m_StateMachine)
164         , m_PerJobCounterSelectionCommandHandler(0,
165                                                  5,
166                                                  m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(),
167                                                  m_StateMachine)
168         , m_TimelinePacketWriterFactory(m_BufferManager)
169     {
170         // Register the "Connection Acknowledged" command handler
171         m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler);
172
173         // Register the "Request Counter Directory" command handler
174         m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler);
175
176         // Register the "Periodic Counter Selection" command handler
177         m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler);
178
179         // Register the "Per-Job Counter Selection" command handler
180         m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler);
181     }
182     ~ProfilingService();
183
184     // Protected methods for testing
185     void SwapProfilingConnectionFactory(ProfilingService& instance,
186                                         IProfilingConnectionFactory* other,
187                                         IProfilingConnectionFactory*& backup)
188     {
189         BOOST_ASSERT(instance.m_ProfilingConnectionFactory);
190         BOOST_ASSERT(other);
191
192         backup = instance.m_ProfilingConnectionFactory.release();
193         instance.m_ProfilingConnectionFactory.reset(other);
194     }
195     IProfilingConnection* GetProfilingConnection(ProfilingService& instance)
196     {
197         return instance.m_ProfilingConnection.get();
198     }
199     void TransitionToState(ProfilingService& instance, ProfilingState newState)
200     {
201         instance.m_StateMachine.TransitionToState(newState);
202     }
203     void WaitForPacketSent(ProfilingService& instance, uint32_t timeout = 1000)
204     {
205         return instance.m_SendCounterPacket.WaitForPacketSent(timeout);
206     }
207
208     BufferManager& GetBufferManager(ProfilingService& instance)
209     {
210         return instance.m_BufferManager;
211     }
212 };
213
214 } // namespace profiling
215
216 } // namespace armnn