xref: /aosp_15_r20/external/armnn/profiling/client/src/PeriodicCounterCapture.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "IPeriodicCounterCapture.hpp"
9 #include "SendCounterPacket.hpp"
10 
11 #include <client/include/CounterIdMap.hpp>
12 #include <client/include/Holder.hpp>
13 #include <client/include/ICounterValues.hpp>
14 
15 #include <client/include/backends/IBackendProfilingContext.hpp>
16 
17 #include <common/include/Packet.hpp>
18 
19 #include <atomic>
20 
21 #if !defined(ARMNN_DISABLE_THREADS)
22 #include <mutex>
23 #include <thread>
24 #endif
25 
26 namespace arm
27 {
28 
29 namespace pipe
30 {
31 
32 class PeriodicCounterCapture final : public IPeriodicCounterCapture
33 {
34 public:
PeriodicCounterCapture(const Holder & data,ISendCounterPacket & packet,IReadCounterValues & readCounterValue,const ICounterMappings & counterIdMap,const std::unordered_map<std::string,std::shared_ptr<IBackendProfilingContext>> & backendProfilingContexts)35     PeriodicCounterCapture(const Holder& data,
36                            ISendCounterPacket& packet,
37                            IReadCounterValues& readCounterValue,
38                            const ICounterMappings& counterIdMap,
39                            const std::unordered_map<std::string,
40                            std::shared_ptr<IBackendProfilingContext>>& backendProfilingContexts)
41             : m_CaptureDataHolder(data)
42             , m_IsRunning(false)
43             , m_KeepRunning(false)
44             , m_ReadCounterValues(readCounterValue)
45             , m_SendCounterPacket(packet)
46             , m_CounterIdMap(counterIdMap)
47             , m_BackendProfilingContexts(backendProfilingContexts)
48     {}
~PeriodicCounterCapture()49     ~PeriodicCounterCapture() { Stop(); }
50 
51     void Start() override;
52     void Stop() override;
IsRunning() const53     bool IsRunning() const { return m_IsRunning; }
54 
55 private:
56     CaptureData ReadCaptureData();
57     void Capture(IReadCounterValues& readCounterValues);
58     void DispatchPeriodicCounterCapturePacket(
59             const std::string& backendId, const std::vector<Timestamp>& timestampValues);
60 
61     const Holder&             m_CaptureDataHolder;
62     bool                      m_IsRunning;
63     std::atomic<bool>         m_KeepRunning;
64 #if !defined(ARMNN_DISABLE_THREADS)
65     std::thread               m_PeriodCaptureThread;
66 #endif
67     IReadCounterValues&       m_ReadCounterValues;
68     ISendCounterPacket&       m_SendCounterPacket;
69     const ICounterMappings&   m_CounterIdMap;
70     const std::unordered_map<std::string,
71             std::shared_ptr<IBackendProfilingContext>>& m_BackendProfilingContexts;
72 };
73 
74 } // namespace pipe
75 
76 } // namespace arm
77