Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 8 | #include "BufferManager.hpp" |
| 9 | #include "CommandHandler.hpp" |
| 10 | #include "ConnectionAcknowledgedCommandHandler.hpp" |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 11 | #include "CounterDirectory.hpp" |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 12 | #include "ICounterValues.hpp" |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 13 | #include "PeriodicCounterCapture.hpp" |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 14 | #include "PeriodicCounterSelectionCommandHandler.hpp" |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 15 | #include "PerJobCounterSelectionCommandHandler.hpp" |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 16 | #include "ProfilingConnectionFactory.hpp" |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 17 | #include "ProfilingGuidGenerator.hpp" |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 18 | #include "ProfilingStateMachine.hpp" |
| 19 | #include "RequestCounterDirectoryCommandHandler.hpp" |
| 20 | #include "SendCounterPacket.hpp" |
| 21 | #include "TimelinePacketWriterFactory.hpp" |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 22 | |
| 23 | namespace armnn |
| 24 | { |
| 25 | |
| 26 | namespace profiling |
| 27 | { |
| 28 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 29 | class ProfilingService : public IReadWriteCounterValues, public IProfilingGuidGenerator |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 30 | { |
| 31 | public: |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 32 | using ExternalProfilingOptions = Runtime::CreationOptions::ExternalProfilingOptions; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 33 | using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 34 | using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>; |
| 35 | using CounterIndices = std::vector<std::atomic<uint32_t>*>; |
| 36 | using CounterValues = std::list<std::atomic<uint32_t>>; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 37 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 38 | // Getter for the singleton instance |
| 39 | static ProfilingService& Instance() |
| 40 | { |
| 41 | static ProfilingService instance; |
| 42 | return instance; |
| 43 | } |
| 44 | |
| 45 | // Resets the profiling options, optionally clears the profiling service entirely |
| 46 | void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false); |
Jim Flynn | 672d06e | 2019-10-15 10:18:11 +0100 | [diff] [blame] | 47 | ProfilingState ConfigureProfilingService(const ExternalProfilingOptions& options, |
| 48 | bool resetProfilingService = false); |
| 49 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 50 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 51 | // Updates the profiling service, making it transition to a new state if necessary |
| 52 | void Update(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 53 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 54 | // Disconnects the profiling service from the external server |
| 55 | void Disconnect(); |
| 56 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 57 | // Getters for the profiling service state |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 58 | const ICounterDirectory& GetCounterDirectory() const; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 59 | ProfilingState GetCurrentState() const; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 60 | bool IsCounterRegistered(uint16_t counterUid) const override; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 61 | uint16_t GetCounterCount() const override; |
| 62 | uint32_t GetCounterValue(uint16_t counterUid) const override; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 63 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 64 | // Setters for the profiling service state |
| 65 | void SetCounterValue(uint16_t counterUid, uint32_t value) override; |
| 66 | uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override; |
| 67 | uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override; |
| 68 | uint32_t IncrementCounterValue(uint16_t counterUid) override; |
| 69 | uint32_t DecrementCounterValue(uint16_t counterUid) override; |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 70 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 71 | // IProfilingGuidGenerator functions |
| 72 | /// Return the next random Guid in the sequence |
| 73 | ProfilingDynamicGuid NextGuid() override; |
| 74 | /// Create a ProfilingStaticGuid based on a hash of the string |
| 75 | ProfilingStaticGuid GenerateStaticId(const std::string& str) override; |
| 76 | |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 77 | std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const; |
| 78 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 79 | private: |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 80 | // Copy/move constructors/destructors and copy/move assignment operators are deleted |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 81 | ProfilingService(const ProfilingService&) = delete; |
| 82 | ProfilingService(ProfilingService&&) = delete; |
| 83 | ProfilingService& operator=(const ProfilingService&) = delete; |
| 84 | ProfilingService& operator=(ProfilingService&&) = delete; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 85 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 86 | // Initialization/reset functions |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 87 | void Initialize(); |
| 88 | void InitializeCounterValue(uint16_t counterUid); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 89 | void Reset(); |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 90 | void Stop(); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 91 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 92 | // Helper function |
| 93 | void CheckCounterUid(uint16_t counterUid) const; |
| 94 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 95 | // Profiling service components |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 96 | ExternalProfilingOptions m_Options; |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 97 | CounterDirectory m_CounterDirectory; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 98 | IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 99 | IProfilingConnectionPtr m_ProfilingConnection; |
| 100 | ProfilingStateMachine m_StateMachine; |
| 101 | CounterIndices m_CounterIndex; |
| 102 | CounterValues m_CounterValues; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 103 | CommandHandlerRegistry m_CommandHandlerRegistry; |
| 104 | PacketVersionResolver m_PacketVersionResolver; |
| 105 | CommandHandler m_CommandHandler; |
| 106 | BufferManager m_BufferManager; |
| 107 | SendCounterPacket m_SendCounterPacket; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 108 | Holder m_Holder; |
| 109 | PeriodicCounterCapture m_PeriodicCounterCapture; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 110 | ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler; |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 111 | RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 112 | PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler; |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 113 | PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler; |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 114 | ProfilingGuidGenerator m_GuidGenerator; |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 115 | TimelinePacketWriterFactory m_TimelinePacketWriterFactory; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 116 | |
| 117 | protected: |
| 118 | // Default constructor/destructor kept protected for testing |
| 119 | ProfilingService() |
| 120 | : m_Options() |
| 121 | , m_CounterDirectory() |
| 122 | , m_ProfilingConnectionFactory(new ProfilingConnectionFactory()) |
| 123 | , m_ProfilingConnection() |
| 124 | , m_StateMachine() |
| 125 | , m_CounterIndex() |
| 126 | , m_CounterValues() |
| 127 | , m_CommandHandlerRegistry() |
| 128 | , m_PacketVersionResolver() |
| 129 | , m_CommandHandler(1000, |
| 130 | false, |
| 131 | m_CommandHandlerRegistry, |
| 132 | m_PacketVersionResolver) |
| 133 | , m_BufferManager() |
| 134 | , m_SendCounterPacket(m_StateMachine, m_BufferManager) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 135 | , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 136 | , m_ConnectionAcknowledgedCommandHandler(0, |
| 137 | 1, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 138 | m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(), |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 139 | m_CounterDirectory, |
| 140 | m_SendCounterPacket, |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 141 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 142 | , m_RequestCounterDirectoryCommandHandler(0, |
| 143 | 3, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 144 | m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(), |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 145 | m_CounterDirectory, |
| 146 | m_SendCounterPacket, |
| 147 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 148 | , m_PeriodicCounterSelectionCommandHandler(0, |
| 149 | 4, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 150 | m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 151 | m_Holder, |
| 152 | m_PeriodicCounterCapture, |
| 153 | *this, |
| 154 | m_SendCounterPacket, |
| 155 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 156 | , m_PerJobCounterSelectionCommandHandler(0, |
| 157 | 5, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 158 | m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(), |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 159 | m_StateMachine) |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 160 | , m_TimelinePacketWriterFactory(m_BufferManager) |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 161 | { |
| 162 | // Register the "Connection Acknowledged" command handler |
| 163 | m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler); |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 164 | |
| 165 | // Register the "Request Counter Directory" command handler |
| 166 | m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 167 | |
| 168 | // Register the "Periodic Counter Selection" command handler |
| 169 | m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler); |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 170 | |
| 171 | // Register the "Per-Job Counter Selection" command handler |
| 172 | m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 173 | } |
| 174 | ~ProfilingService() = default; |
| 175 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 176 | // Protected methods for testing |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 177 | void SwapProfilingConnectionFactory(ProfilingService& instance, |
| 178 | IProfilingConnectionFactory* other, |
| 179 | IProfilingConnectionFactory*& backup) |
| 180 | { |
| 181 | BOOST_ASSERT(instance.m_ProfilingConnectionFactory); |
| 182 | BOOST_ASSERT(other); |
| 183 | |
| 184 | backup = instance.m_ProfilingConnectionFactory.release(); |
| 185 | instance.m_ProfilingConnectionFactory.reset(other); |
| 186 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 187 | IProfilingConnection* GetProfilingConnection(ProfilingService& instance) |
| 188 | { |
| 189 | return instance.m_ProfilingConnection.get(); |
| 190 | } |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 191 | void TransitionToState(ProfilingService& instance, ProfilingState newState) |
| 192 | { |
| 193 | instance.m_StateMachine.TransitionToState(newState); |
| 194 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 195 | void WaitForPacketSent(ProfilingService& instance) |
| 196 | { |
| 197 | return instance.m_SendCounterPacket.WaitForPacketSent(); |
| 198 | } |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // namespace profiling |
| 202 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 203 | } // namespace armnn |