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" |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 12 | #include "CounterIdMap.hpp" |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 13 | #include "ICounterRegistry.hpp" |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 14 | #include "ICounterValues.hpp" |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 15 | #include "PeriodicCounterCapture.hpp" |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 16 | #include "PeriodicCounterSelectionCommandHandler.hpp" |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 17 | #include "PerJobCounterSelectionCommandHandler.hpp" |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 18 | #include "ProfilingConnectionFactory.hpp" |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 19 | #include "ProfilingGuidGenerator.hpp" |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 20 | #include "ProfilingStateMachine.hpp" |
| 21 | #include "RequestCounterDirectoryCommandHandler.hpp" |
| 22 | #include "SendCounterPacket.hpp" |
Matteo Martincigh | cdfb941 | 2019-11-08 11:23:06 +0000 | [diff] [blame] | 23 | #include "SendTimelinePacket.hpp" |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 24 | #include "TimelinePacketWriterFactory.hpp" |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame^] | 25 | #include <armnn/backends/profiling/IBackendProfilingContext.hpp> |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 26 | |
| 27 | namespace armnn |
| 28 | { |
| 29 | |
| 30 | namespace profiling |
| 31 | { |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 32 | // Static constants describing ArmNN's counter UID's |
| 33 | static const uint16_t NETWORK_LOADS = 0; |
| 34 | static const uint16_t NETWORK_UNLOADS = 1; |
| 35 | static const uint16_t REGISTERED_BACKENDS = 2; |
| 36 | static const uint16_t UNREGISTERED_BACKENDS = 3; |
| 37 | static const uint16_t INFERENCES_RUN = 4; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 38 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 39 | class ProfilingService : public IReadWriteCounterValues, public IProfilingGuidGenerator |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 40 | { |
| 41 | public: |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 42 | using ExternalProfilingOptions = IRuntime::CreationOptions::ExternalProfilingOptions; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 43 | using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 44 | using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>; |
| 45 | using CounterIndices = std::vector<std::atomic<uint32_t>*>; |
| 46 | using CounterValues = std::list<std::atomic<uint32_t>>; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 47 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 48 | // Getter for the singleton instance |
| 49 | static ProfilingService& Instance() |
| 50 | { |
| 51 | static ProfilingService instance; |
| 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | // Resets the profiling options, optionally clears the profiling service entirely |
| 56 | void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false); |
Jim Flynn | 672d06e | 2019-10-15 10:18:11 +0100 | [diff] [blame] | 57 | ProfilingState ConfigureProfilingService(const ExternalProfilingOptions& options, |
| 58 | bool resetProfilingService = false); |
| 59 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 60 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 61 | // Updates the profiling service, making it transition to a new state if necessary |
| 62 | void Update(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 63 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 64 | // Disconnects the profiling service from the external server |
| 65 | void Disconnect(); |
| 66 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame^] | 67 | // Store a profiling context returned from a backend that support profiling. |
| 68 | void AddBackendProfilingContext(const BackendId backendId, |
| 69 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext) |
| 70 | { |
| 71 | m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext)); |
| 72 | } |
| 73 | |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 74 | const ICounterDirectory& GetCounterDirectory() const; |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 75 | ICounterRegistry& GetCounterRegistry(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 76 | ProfilingState GetCurrentState() const; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 77 | bool IsCounterRegistered(uint16_t counterUid) const override; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 78 | uint32_t GetCounterValue(uint16_t counterUid) const override; |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 79 | uint16_t GetCounterCount() const override; |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 80 | // counter global/backend mapping functions |
| 81 | const ICounterMappings& GetCounterMappings() const; |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 82 | IRegisterCounterMapping& GetCounterMappingRegistry(); |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 83 | |
| 84 | // Getters for the profiling service state |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 85 | bool IsProfilingEnabled(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 86 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 87 | // Setters for the profiling service state |
| 88 | void SetCounterValue(uint16_t counterUid, uint32_t value) override; |
| 89 | uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override; |
| 90 | uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override; |
| 91 | uint32_t IncrementCounterValue(uint16_t counterUid) override; |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 92 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 93 | // IProfilingGuidGenerator functions |
| 94 | /// Return the next random Guid in the sequence |
| 95 | ProfilingDynamicGuid NextGuid() override; |
| 96 | /// Create a ProfilingStaticGuid based on a hash of the string |
| 97 | ProfilingStaticGuid GenerateStaticId(const std::string& str) override; |
| 98 | |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 99 | std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const; |
| 100 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 101 | /// Check if the profiling is enabled |
| 102 | bool IsEnabled() { return m_Options.m_EnableProfiling; } |
| 103 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 104 | private: |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 105 | // Copy/move constructors/destructors and copy/move assignment operators are deleted |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 106 | ProfilingService(const ProfilingService&) = delete; |
| 107 | ProfilingService(ProfilingService&&) = delete; |
| 108 | ProfilingService& operator=(const ProfilingService&) = delete; |
| 109 | ProfilingService& operator=(ProfilingService&&) = delete; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 110 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 111 | // Initialization/reset functions |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 112 | void Initialize(); |
| 113 | void InitializeCounterValue(uint16_t counterUid); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 114 | void Reset(); |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 115 | void Stop(); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 116 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 117 | // Helper function |
| 118 | void CheckCounterUid(uint16_t counterUid) const; |
| 119 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 120 | // Profiling service components |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 121 | ExternalProfilingOptions m_Options; |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 122 | CounterDirectory m_CounterDirectory; |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 123 | CounterIdMap m_CounterIdMap; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 124 | IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 125 | IProfilingConnectionPtr m_ProfilingConnection; |
| 126 | ProfilingStateMachine m_StateMachine; |
| 127 | CounterIndices m_CounterIndex; |
| 128 | CounterValues m_CounterValues; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 129 | CommandHandlerRegistry m_CommandHandlerRegistry; |
| 130 | PacketVersionResolver m_PacketVersionResolver; |
| 131 | CommandHandler m_CommandHandler; |
| 132 | BufferManager m_BufferManager; |
| 133 | SendCounterPacket m_SendCounterPacket; |
Matteo Martincigh | cdfb941 | 2019-11-08 11:23:06 +0000 | [diff] [blame] | 134 | SendTimelinePacket m_SendTimelinePacket; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 135 | Holder m_Holder; |
| 136 | PeriodicCounterCapture m_PeriodicCounterCapture; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 137 | ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler; |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 138 | RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 139 | PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler; |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 140 | PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler; |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 141 | ProfilingGuidGenerator m_GuidGenerator; |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 142 | TimelinePacketWriterFactory m_TimelinePacketWriterFactory; |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame^] | 143 | std::unordered_map<BackendId, |
| 144 | std::shared_ptr<armnn::profiling::IBackendProfilingContext>> m_BackendProfilingContexts; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 145 | |
| 146 | protected: |
| 147 | // Default constructor/destructor kept protected for testing |
| 148 | ProfilingService() |
| 149 | : m_Options() |
| 150 | , m_CounterDirectory() |
| 151 | , m_ProfilingConnectionFactory(new ProfilingConnectionFactory()) |
| 152 | , m_ProfilingConnection() |
| 153 | , m_StateMachine() |
| 154 | , m_CounterIndex() |
| 155 | , m_CounterValues() |
| 156 | , m_CommandHandlerRegistry() |
| 157 | , m_PacketVersionResolver() |
| 158 | , m_CommandHandler(1000, |
| 159 | false, |
| 160 | m_CommandHandlerRegistry, |
| 161 | m_PacketVersionResolver) |
| 162 | , m_BufferManager() |
| 163 | , m_SendCounterPacket(m_StateMachine, m_BufferManager) |
Matteo Martincigh | cdfb941 | 2019-11-08 11:23:06 +0000 | [diff] [blame] | 164 | , m_SendTimelinePacket(m_BufferManager) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 165 | , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 166 | , m_ConnectionAcknowledgedCommandHandler(0, |
| 167 | 1, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 168 | m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(), |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 169 | m_CounterDirectory, |
| 170 | m_SendCounterPacket, |
Matteo Martincigh | cdfb941 | 2019-11-08 11:23:06 +0000 | [diff] [blame] | 171 | m_SendTimelinePacket, |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 172 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 173 | , m_RequestCounterDirectoryCommandHandler(0, |
| 174 | 3, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 175 | m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(), |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 176 | m_CounterDirectory, |
| 177 | m_SendCounterPacket, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 178 | m_SendTimelinePacket, |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 179 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 180 | , m_PeriodicCounterSelectionCommandHandler(0, |
| 181 | 4, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 182 | m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 183 | m_Holder, |
| 184 | m_PeriodicCounterCapture, |
| 185 | *this, |
| 186 | m_SendCounterPacket, |
| 187 | m_StateMachine) |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 188 | , m_PerJobCounterSelectionCommandHandler(0, |
| 189 | 5, |
Jim Flynn | ed25e0e | 2019-10-18 13:21:43 +0100 | [diff] [blame] | 190 | m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(), |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 191 | m_StateMachine) |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 192 | , m_TimelinePacketWriterFactory(m_BufferManager) |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 193 | { |
| 194 | // Register the "Connection Acknowledged" command handler |
| 195 | m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler); |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 196 | |
| 197 | // Register the "Request Counter Directory" command handler |
| 198 | m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 199 | |
| 200 | // Register the "Periodic Counter Selection" command handler |
| 201 | m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler); |
Matteo Martincigh | 994b534 | 2019-10-11 17:19:56 +0100 | [diff] [blame] | 202 | |
| 203 | // Register the "Per-Job Counter Selection" command handler |
| 204 | m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 205 | } |
janeil01 | 811ca55 | 2019-12-03 17:01:32 +0000 | [diff] [blame] | 206 | ~ProfilingService(); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 207 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 208 | // Protected methods for testing |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 209 | void SwapProfilingConnectionFactory(ProfilingService& instance, |
| 210 | IProfilingConnectionFactory* other, |
| 211 | IProfilingConnectionFactory*& backup) |
| 212 | { |
| 213 | BOOST_ASSERT(instance.m_ProfilingConnectionFactory); |
| 214 | BOOST_ASSERT(other); |
| 215 | |
| 216 | backup = instance.m_ProfilingConnectionFactory.release(); |
| 217 | instance.m_ProfilingConnectionFactory.reset(other); |
| 218 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 219 | IProfilingConnection* GetProfilingConnection(ProfilingService& instance) |
| 220 | { |
| 221 | return instance.m_ProfilingConnection.get(); |
| 222 | } |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 223 | void TransitionToState(ProfilingService& instance, ProfilingState newState) |
| 224 | { |
| 225 | instance.m_StateMachine.TransitionToState(newState); |
| 226 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 227 | bool WaitForPacketSent(ProfilingService& instance, uint32_t timeout = 1000) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 228 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 229 | return instance.m_SendCounterPacket.WaitForPacketSent(timeout); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 230 | } |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 231 | |
| 232 | BufferManager& GetBufferManager(ProfilingService& instance) |
| 233 | { |
| 234 | return instance.m_BufferManager; |
| 235 | } |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | } // namespace profiling |
| 239 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 240 | } // namespace armnn |