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