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