Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 8 | #include "IBufferManager.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 9 | #include "ICounterDirectory.hpp" |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 10 | #include "ISendCounterPacket.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 11 | #include "IProfilingConnection.hpp" |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 12 | #include "ProfilingStateMachine.hpp" |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 13 | #include "ProfilingUtils.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 14 | |
| 15 | #include <atomic> |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 16 | #include <condition_variable> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 17 | #include <mutex> |
| 18 | #include <thread> |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 19 | #include <type_traits> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 20 | |
| 21 | namespace armnn |
| 22 | { |
| 23 | |
| 24 | namespace profiling |
| 25 | { |
| 26 | |
| 27 | class SendCounterPacket : public ISendCounterPacket |
| 28 | { |
| 29 | public: |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 30 | 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 Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 34 | using IndexValuePairsVector = std::vector<std::pair<uint16_t, uint32_t>>; |
| 35 | |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 36 | SendCounterPacket(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer, int timeout = 1000) |
| 37 | : m_StateMachine(profilingStateMachine) |
| 38 | , m_BufferManager(buffer) |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame] | 39 | , m_Timeout(timeout) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 40 | , m_IsRunning(false) |
| 41 | , m_KeepRunning(false) |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 42 | , m_SendThreadException(nullptr) |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 43 | {} |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 44 | ~SendCounterPacket() |
| 45 | { |
| 46 | // Don't rethrow when destructing the object |
| 47 | Stop(false); |
| 48 | } |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 49 | |
| 50 | void SendStreamMetaDataPacket() override; |
| 51 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 52 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 53 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 54 | void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 55 | |
| 56 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 57 | const std::vector<uint16_t>& selectedCounterIds) override; |
| 58 | |
| 59 | void SetReadyToRead() override; |
| 60 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 61 | static const unsigned int PIPE_MAGIC = 0x45495434; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 62 | |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame] | 63 | void Start(IProfilingConnection& profilingConnection); |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 64 | void Stop(bool rethrowSendThreadExceptions = true); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 65 | bool IsRunning() { return m_IsRunning.load(); } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 66 | bool WaitForPacketSent(uint32_t timeout); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 67 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 68 | private: |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame] | 69 | void Send(IProfilingConnection& profilingConnection); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 70 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 71 | template <typename ExceptionType> |
| 72 | void CancelOperationAndThrow(const std::string& errorMessage) |
| 73 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 74 | // Throw a runtime exception with the given error message |
| 75 | throw ExceptionType(errorMessage); |
| 76 | } |
| 77 | |
| 78 | template <typename ExceptionType> |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 79 | void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 80 | { |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 81 | if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value) |
| 82 | { |
| 83 | SetReadyToRead(); |
| 84 | } |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 85 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 86 | if (writerBuffer != nullptr) |
| 87 | { |
| 88 | // Cancel the operation |
| 89 | m_BufferManager.Release(writerBuffer); |
| 90 | } |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 91 | |
| 92 | // Throw a runtime exception with the given error message |
| 93 | throw ExceptionType(errorMessage); |
| 94 | } |
| 95 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 96 | void FlushBuffer(IProfilingConnection& profilingConnection, bool notifyWatchers = true); |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 97 | |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 98 | ProfilingStateMachine& m_StateMachine; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 99 | IBufferManager& m_BufferManager; |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame] | 100 | int m_Timeout; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 101 | 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; |
Finn Williams | 73ae7fa | 2019-12-03 11:55:31 +0000 | [diff] [blame] | 106 | // m_ReadyToRead will be protected by m_WaitMutex |
| 107 | bool m_ReadyToRead; |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 108 | // m_PacketSent will be protected by m_PacketSentWaitMutex |
| 109 | bool m_PacketSent; |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 110 | std::exception_ptr m_SendThreadException; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 111 | std::mutex m_PacketSentWaitMutex; |
| 112 | std::condition_variable m_PacketSentWaitCondition; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 113 | |
| 114 | protected: |
| 115 | // Helper methods, protected for testing |
| 116 | bool CreateCategoryRecord(const CategoryPtr& category, |
| 117 | const Counters& counters, |
| 118 | CategoryRecord& categoryRecord, |
| 119 | std::string& errorMessage); |
| 120 | bool CreateDeviceRecord(const DevicePtr& device, |
| 121 | DeviceRecord& deviceRecord, |
| 122 | std::string& errorMessage); |
| 123 | bool CreateCounterSetRecord(const CounterSetPtr& counterSet, |
| 124 | CounterSetRecord& counterSetRecord, |
| 125 | std::string& errorMessage); |
| 126 | bool CreateEventRecord(const CounterPtr& counter, |
| 127 | EventRecord& eventRecord, |
| 128 | std::string& errorMessage); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace profiling |
| 132 | |
| 133 | } // namespace armnn |