blob: 9361efbc745325c187722bef1118f5f138d024d4 [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01008#include "IBufferManager.hpp"
Matteo Martincigh24e8f922019-09-19 11:57:46 +01009#include "ICounterDirectory.hpp"
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010010#include "ISendCounterPacket.hpp"
Matteo Martincigh24e8f922019-09-19 11:57:46 +010011#include "IProfilingConnection.hpp"
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010012#include "ProfilingStateMachine.hpp"
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010013#include "ProfilingUtils.hpp"
Matteo Martincigh24e8f922019-09-19 11:57:46 +010014
15#include <atomic>
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010016#include <condition_variable>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010017#include <mutex>
18#include <thread>
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010019#include <type_traits>
Ferran Balaguer73882172019-09-02 16:39:42 +010020
21namespace armnn
22{
23
24namespace profiling
25{
26
27class SendCounterPacket : public ISendCounterPacket
28{
29public:
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010030 using CategoryRecord = std::vector<uint32_t>;
31 using DeviceRecord = std::vector<uint32_t>;
32 using CounterSetRecord = std::vector<uint32_t>;
33 using EventRecord = std::vector<uint32_t>;
Francis Murtagh3a161982019-09-04 15:25:02 +010034 using IndexValuePairsVector = std::vector<std::pair<uint16_t, uint32_t>>;
35
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010036 SendCounterPacket(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer, int timeout = 1000)
37 : m_StateMachine(profilingStateMachine)
38 , m_BufferManager(buffer)
Matteo Martincighe61ffd02019-10-07 10:19:35 +010039 , m_Timeout(timeout)
Matteo Martincigh24e8f922019-09-19 11:57:46 +010040 , m_IsRunning(false)
41 , m_KeepRunning(false)
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010042 , m_SendThreadException(nullptr)
Matteo Martincigh149528e2019-09-05 12:02:04 +010043 {}
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010044 ~SendCounterPacket()
45 {
46 // Don't rethrow when destructing the object
47 Stop(false);
48 }
Ferran Balaguer73882172019-09-02 16:39:42 +010049
50 void SendStreamMetaDataPacket() override;
51
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010052 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010053
Francis Murtagh3a161982019-09-04 15:25:02 +010054 void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010055
56 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
57 const std::vector<uint16_t>& selectedCounterIds) override;
58
59 void SetReadyToRead() override;
60
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010061 static const unsigned int PIPE_MAGIC = 0x45495434;
62 static const unsigned int MAX_METADATA_PACKET_LENGTH = 4096;
63
Matteo Martincighe61ffd02019-10-07 10:19:35 +010064 void Start(IProfilingConnection& profilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010065 void Stop(bool rethrowSendThreadExceptions = true);
Matteo Martincigh24e8f922019-09-19 11:57:46 +010066 bool IsRunning() { return m_IsRunning.load(); }
67
Ferran Balaguer73882172019-09-02 16:39:42 +010068private:
Matteo Martincighe61ffd02019-10-07 10:19:35 +010069 void Send(IProfilingConnection& profilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +010070
Matteo Martincigh149528e2019-09-05 12:02:04 +010071 template <typename ExceptionType>
72 void CancelOperationAndThrow(const std::string& errorMessage)
73 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010074 // Throw a runtime exception with the given error message
75 throw ExceptionType(errorMessage);
76 }
77
78 template <typename ExceptionType>
79 void CancelOperationAndThrow(std::unique_ptr<IPacketBuffer>& writerBuffer, const std::string& errorMessage)
80 {
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010081 if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value)
82 {
83 SetReadyToRead();
84 }
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010085
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010086 if (writerBuffer != nullptr)
87 {
88 // Cancel the operation
89 m_BufferManager.Release(writerBuffer);
90 }
Matteo Martincigh149528e2019-09-05 12:02:04 +010091
92 // Throw a runtime exception with the given error message
93 throw ExceptionType(errorMessage);
94 }
95
Matteo Martincighe61ffd02019-10-07 10:19:35 +010096 void FlushBuffer(IProfilingConnection& profilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010097
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010098 ProfilingStateMachine& m_StateMachine;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010099 IBufferManager& m_BufferManager;
Matteo Martincighe61ffd02019-10-07 10:19:35 +0100100 int m_Timeout;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100101 std::mutex m_WaitMutex;
102 std::condition_variable m_WaitCondition;
103 std::thread m_SendThread;
104 std::atomic<bool> m_IsRunning;
105 std::atomic<bool> m_KeepRunning;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100106 std::exception_ptr m_SendThreadException;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100107
108protected:
109 // Helper methods, protected for testing
110 bool CreateCategoryRecord(const CategoryPtr& category,
111 const Counters& counters,
112 CategoryRecord& categoryRecord,
113 std::string& errorMessage);
114 bool CreateDeviceRecord(const DevicePtr& device,
115 DeviceRecord& deviceRecord,
116 std::string& errorMessage);
117 bool CreateCounterSetRecord(const CounterSetPtr& counterSet,
118 CounterSetRecord& counterSetRecord,
119 std::string& errorMessage);
120 bool CreateEventRecord(const CounterPtr& counter,
121 EventRecord& eventRecord,
122 std::string& errorMessage);
Ferran Balaguer73882172019-09-02 16:39:42 +0100123};
124
125} // namespace profiling
126
127} // namespace armnn