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