blob: e1a42aa496f710c0797658e79905ace6a246e355 [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
Matteo Martincighe8485382019-10-10 14:08:21 +010068 void WaitForPacketSent()
69 {
70 std::unique_lock<std::mutex> lock(m_PacketSentWaitMutex);
71
72 // Blocks until notified that at least a packet has been sent
73 m_PacketSentWaitCondition.wait(lock);
74 }
75
Ferran Balaguer73882172019-09-02 16:39:42 +010076private:
Matteo Martincighe61ffd02019-10-07 10:19:35 +010077 void Send(IProfilingConnection& profilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +010078
Matteo Martincigh149528e2019-09-05 12:02:04 +010079 template <typename ExceptionType>
80 void CancelOperationAndThrow(const std::string& errorMessage)
81 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010082 // Throw a runtime exception with the given error message
83 throw ExceptionType(errorMessage);
84 }
85
86 template <typename ExceptionType>
87 void CancelOperationAndThrow(std::unique_ptr<IPacketBuffer>& writerBuffer, const std::string& errorMessage)
88 {
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010089 if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value)
90 {
91 SetReadyToRead();
92 }
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010093
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010094 if (writerBuffer != nullptr)
95 {
96 // Cancel the operation
97 m_BufferManager.Release(writerBuffer);
98 }
Matteo Martincigh149528e2019-09-05 12:02:04 +010099
100 // Throw a runtime exception with the given error message
101 throw ExceptionType(errorMessage);
102 }
103
Matteo Martincighe8485382019-10-10 14:08:21 +0100104 void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100105
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100106 ProfilingStateMachine& m_StateMachine;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100107 IBufferManager& m_BufferManager;
Matteo Martincighe61ffd02019-10-07 10:19:35 +0100108 int m_Timeout;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100109 std::mutex m_WaitMutex;
110 std::condition_variable m_WaitCondition;
111 std::thread m_SendThread;
112 std::atomic<bool> m_IsRunning;
113 std::atomic<bool> m_KeepRunning;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100114 std::exception_ptr m_SendThreadException;
Matteo Martincighe8485382019-10-10 14:08:21 +0100115 std::mutex m_PacketSentWaitMutex;
116 std::condition_variable m_PacketSentWaitCondition;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100117
118protected:
119 // Helper methods, protected for testing
120 bool CreateCategoryRecord(const CategoryPtr& category,
121 const Counters& counters,
122 CategoryRecord& categoryRecord,
123 std::string& errorMessage);
124 bool CreateDeviceRecord(const DevicePtr& device,
125 DeviceRecord& deviceRecord,
126 std::string& errorMessage);
127 bool CreateCounterSetRecord(const CounterSetPtr& counterSet,
128 CounterSetRecord& counterSetRecord,
129 std::string& errorMessage);
130 bool CreateEventRecord(const CounterPtr& counter,
131 EventRecord& eventRecord,
132 std::string& errorMessage);
Ferran Balaguer73882172019-09-02 16:39:42 +0100133};
134
135} // namespace profiling
136
137} // namespace armnn