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