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" |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 11 | #include "ProfilingUtils.hpp" |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 12 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 13 | #include <type_traits> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | namespace profiling |
| 19 | { |
| 20 | |
| 21 | class SendCounterPacket : public ISendCounterPacket |
| 22 | { |
| 23 | public: |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 24 | using CategoryRecord = std::vector<uint32_t>; |
| 25 | using DeviceRecord = std::vector<uint32_t>; |
| 26 | using CounterSetRecord = std::vector<uint32_t>; |
| 27 | using EventRecord = std::vector<uint32_t>; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 28 | using IndexValuePairsVector = std::vector<CounterValue>; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 29 | |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 30 | SendCounterPacket(IBufferManager& buffer) |
| 31 | : m_BufferManager(buffer) |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 32 | {} |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 33 | |
| 34 | void SendStreamMetaDataPacket() override; |
| 35 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 36 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 37 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 38 | void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 39 | |
| 40 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 41 | const std::vector<uint16_t>& selectedCounterIds) override; |
| 42 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 43 | static const unsigned int PIPE_MAGIC = 0x45495434; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 44 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 45 | private: |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 46 | template <typename ExceptionType> |
| 47 | void CancelOperationAndThrow(const std::string& errorMessage) |
| 48 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 49 | // Throw a runtime exception with the given error message |
| 50 | throw ExceptionType(errorMessage); |
| 51 | } |
| 52 | |
| 53 | template <typename ExceptionType> |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 54 | void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 55 | { |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 56 | if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value) |
| 57 | { |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 58 | m_BufferManager.FlushReadList(); |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 59 | } |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 60 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 61 | if (writerBuffer != nullptr) |
| 62 | { |
| 63 | // Cancel the operation |
| 64 | m_BufferManager.Release(writerBuffer); |
| 65 | } |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 66 | |
| 67 | // Throw a runtime exception with the given error message |
| 68 | throw ExceptionType(errorMessage); |
| 69 | } |
| 70 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 71 | IBufferManager& m_BufferManager; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 72 | |
| 73 | protected: |
| 74 | // Helper methods, protected for testing |
| 75 | bool CreateCategoryRecord(const CategoryPtr& category, |
| 76 | const Counters& counters, |
| 77 | CategoryRecord& categoryRecord, |
| 78 | std::string& errorMessage); |
| 79 | bool CreateDeviceRecord(const DevicePtr& device, |
| 80 | DeviceRecord& deviceRecord, |
| 81 | std::string& errorMessage); |
| 82 | bool CreateCounterSetRecord(const CounterSetPtr& counterSet, |
| 83 | CounterSetRecord& counterSetRecord, |
| 84 | std::string& errorMessage); |
| 85 | bool CreateEventRecord(const CounterPtr& counter, |
| 86 | EventRecord& eventRecord, |
| 87 | std::string& errorMessage); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace profiling |
| 91 | |
| 92 | } // namespace armnn |