blob: edeb6bde90f5228c8ee9986295e9ca7ce6769246 [file] [log] [blame]
Keith Davis02356de2019-08-26 18:28:17 +01001//
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"
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010010#include "CounterDirectory.hpp"
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010011#include "ICounterValues.hpp"
Matteo Martincigh54fb9572019-10-02 12:50:57 +010012#include "CommandHandler.hpp"
13#include "BufferManager.hpp"
14#include "SendCounterPacket.hpp"
15#include "ConnectionAcknowledgedCommandHandler.hpp"
Keith Davis02356de2019-08-26 18:28:17 +010016
17namespace armnn
18{
19
20namespace profiling
21{
22
Matteo Martincigh54fb9572019-10-02 12:50:57 +010023class ProfilingService : public IReadWriteCounterValues
Keith Davis02356de2019-08-26 18:28:17 +010024{
25public:
Matteo Martincigha84edee2019-10-02 12:50:57 +010026 using ExternalProfilingOptions = Runtime::CreationOptions::ExternalProfilingOptions;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010027 using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>;
Matteo Martincigha84edee2019-10-02 12:50:57 +010028 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 Davis02356de2019-08-26 18:28:17 +010031
Matteo Martincigha84edee2019-10-02 12:50:57 +010032 // 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 Martincigh54fb9572019-10-02 12:50:57 +010042 // Updates the profiling service, making it transition to a new state if necessary
43 void Update();
Keith Davis02356de2019-08-26 18:28:17 +010044
Matteo Martincigha84edee2019-10-02 12:50:57 +010045 // Getters for the profiling service state
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010046 const ICounterDirectory& GetCounterDirectory() const;
Keith Davis02356de2019-08-26 18:28:17 +010047 ProfilingState GetCurrentState() const;
Matteo Martincigha84edee2019-10-02 12:50:57 +010048 uint16_t GetCounterCount() const override;
49 uint32_t GetCounterValue(uint16_t counterUid) const override;
Keith Davis02356de2019-08-26 18:28:17 +010050
Matteo Martincigha84edee2019-10-02 12:50:57 +010051 // 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;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +010057
Keith Davis02356de2019-08-26 18:28:17 +010058private:
Matteo Martincigh54fb9572019-10-02 12:50:57 +010059 // Copy/move constructors/destructors and copy/move assignment operators are deleted
Matteo Martincigha84edee2019-10-02 12:50:57 +010060 ProfilingService(const ProfilingService&) = delete;
61 ProfilingService(ProfilingService&&) = delete;
62 ProfilingService& operator=(const ProfilingService&) = delete;
63 ProfilingService& operator=(ProfilingService&&) = delete;
Keith Davis02356de2019-08-26 18:28:17 +010064
Matteo Martincigh54fb9572019-10-02 12:50:57 +010065 // Initialization/reset functions
Matteo Martincigha84edee2019-10-02 12:50:57 +010066 void Initialize();
67 void InitializeCounterValue(uint16_t counterUid);
Matteo Martincigh54fb9572019-10-02 12:50:57 +010068 void Reset();
Matteo Martincigha84edee2019-10-02 12:50:57 +010069
Matteo Martincigh54fb9572019-10-02 12:50:57 +010070 // Profiling service components
Matteo Martincigha84edee2019-10-02 12:50:57 +010071 ExternalProfilingOptions m_Options;
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010072 CounterDirectory m_CounterDirectory;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010073 IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory;
Matteo Martincigha84edee2019-10-02 12:50:57 +010074 IProfilingConnectionPtr m_ProfilingConnection;
75 ProfilingStateMachine m_StateMachine;
76 CounterIndices m_CounterIndex;
77 CounterValues m_CounterValues;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010078 CommandHandlerRegistry m_CommandHandlerRegistry;
79 PacketVersionResolver m_PacketVersionResolver;
80 CommandHandler m_CommandHandler;
81 BufferManager m_BufferManager;
82 SendCounterPacket m_SendCounterPacket;
83 ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
84
85protected:
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 Martincighd0613b52019-10-09 16:47:04 +0100112 // Protected methods for testing
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100113 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 Martincighd0613b52019-10-09 16:47:04 +0100123 IProfilingConnection* GetProfilingConnection(ProfilingService& instance)
124 {
125 return instance.m_ProfilingConnection.get();
126 }
Keith Davis02356de2019-08-26 18:28:17 +0100127};
128
129} // namespace profiling
130
Matteo Martincigha84edee2019-10-02 12:50:57 +0100131} // namespace armnn