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