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