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