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