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" |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 9 | #include "ISendCounterPacket.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 10 | #include "ICounterDirectory.hpp" |
| 11 | #include "IProfilingConnection.hpp" |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 12 | #include "ProfilingUtils.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 13 | |
| 14 | #include <atomic> |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 15 | #include <condition_variable> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 16 | #include <mutex> |
| 17 | #include <thread> |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 18 | #include <type_traits> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 19 | |
| 20 | namespace armnn |
| 21 | { |
| 22 | |
| 23 | namespace profiling |
| 24 | { |
| 25 | |
| 26 | class SendCounterPacket : public ISendCounterPacket |
| 27 | { |
| 28 | public: |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 29 | using CategoryRecord = std::vector<uint32_t>; |
| 30 | using DeviceRecord = std::vector<uint32_t>; |
| 31 | using CounterSetRecord = std::vector<uint32_t>; |
| 32 | using EventRecord = std::vector<uint32_t>; |
| 33 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 34 | using IndexValuePairsVector = std::vector<std::pair<uint16_t, uint32_t>>; |
| 35 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 36 | SendCounterPacket(IProfilingConnection& profilingConnection, IBufferManager& buffer, int timeout = 1) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 37 | : m_ProfilingConnection(profilingConnection) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 38 | , m_BufferManager(buffer) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 39 | , m_IsRunning(false) |
| 40 | , m_KeepRunning(false) |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 41 | , m_Timeout(timeout) |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 42 | {} |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 43 | ~SendCounterPacket() { Stop(); } |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 44 | |
| 45 | void SendStreamMetaDataPacket() override; |
| 46 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 47 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 48 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 49 | void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 50 | |
| 51 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 52 | const std::vector<uint16_t>& selectedCounterIds) override; |
| 53 | |
| 54 | void SetReadyToRead() override; |
| 55 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 56 | static const unsigned int PIPE_MAGIC = 0x45495434; |
| 57 | static const unsigned int MAX_METADATA_PACKET_LENGTH = 4096; |
| 58 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 59 | void Start(); |
| 60 | void Stop(); |
| 61 | bool IsRunning() { return m_IsRunning.load(); } |
| 62 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 63 | private: |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 64 | void Send(); |
| 65 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 66 | template <typename ExceptionType> |
| 67 | void CancelOperationAndThrow(const std::string& errorMessage) |
| 68 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 69 | // Throw a runtime exception with the given error message |
| 70 | throw ExceptionType(errorMessage); |
| 71 | } |
| 72 | |
| 73 | template <typename ExceptionType> |
| 74 | void CancelOperationAndThrow(std::unique_ptr<IPacketBuffer>& writerBuffer, const std::string& errorMessage) |
| 75 | { |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 76 | if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value) |
| 77 | { |
| 78 | SetReadyToRead(); |
| 79 | } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 80 | if (writerBuffer != nullptr) |
| 81 | { |
| 82 | // Cancel the operation |
| 83 | m_BufferManager.Release(writerBuffer); |
| 84 | } |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 85 | |
| 86 | // Throw a runtime exception with the given error message |
| 87 | throw ExceptionType(errorMessage); |
| 88 | } |
| 89 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 90 | void FlushBuffer(); |
| 91 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 92 | IProfilingConnection& m_ProfilingConnection; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 93 | IBufferManager& m_BufferManager; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 94 | std::mutex m_WaitMutex; |
| 95 | std::condition_variable m_WaitCondition; |
| 96 | std::thread m_SendThread; |
| 97 | std::atomic<bool> m_IsRunning; |
| 98 | std::atomic<bool> m_KeepRunning; |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 99 | int m_Timeout; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 100 | |
| 101 | protected: |
| 102 | // Helper methods, protected for testing |
| 103 | bool CreateCategoryRecord(const CategoryPtr& category, |
| 104 | const Counters& counters, |
| 105 | CategoryRecord& categoryRecord, |
| 106 | std::string& errorMessage); |
| 107 | bool CreateDeviceRecord(const DevicePtr& device, |
| 108 | DeviceRecord& deviceRecord, |
| 109 | std::string& errorMessage); |
| 110 | bool CreateCounterSetRecord(const CounterSetPtr& counterSet, |
| 111 | CounterSetRecord& counterSetRecord, |
| 112 | std::string& errorMessage); |
| 113 | bool CreateEventRecord(const CounterPtr& counter, |
| 114 | EventRecord& eventRecord, |
| 115 | std::string& errorMessage); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace profiling |
| 119 | |
| 120 | } // namespace armnn |