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