Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "IBufferManager.hpp" |
Colm Donelan | 5ccb33d | 2020-01-24 16:27:02 +0000 | [diff] [blame] | 9 | #include "armnn/profiling/ISendTimelinePacket.hpp" |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 10 | #include "ProfilingUtils.hpp" |
| 11 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 12 | #include <boost/assert.hpp> |
| 13 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | namespace profiling |
| 20 | { |
| 21 | |
| 22 | class SendTimelinePacket : public ISendTimelinePacket |
| 23 | { |
| 24 | public: |
| 25 | SendTimelinePacket(IBufferManager& bufferManager) |
| 26 | : m_BufferManager(bufferManager) |
| 27 | , m_WriteBuffer(nullptr) |
| 28 | , m_Offset(0u) |
| 29 | , m_BufferSize(0u) |
| 30 | {} |
| 31 | |
| 32 | /// Commits the current buffer and reset the member variables |
| 33 | void Commit() override; |
| 34 | |
| 35 | /// Create and write a TimelineEntityBinaryPacket from the parameters to the buffer. |
| 36 | void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override; |
| 37 | |
| 38 | /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer. |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 39 | void SendTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 40 | |
| 41 | /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer. |
| 42 | void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) override; |
| 43 | |
| 44 | /// Create and write a TimelineLabelBinaryPacket from the parameters to the buffer. |
| 45 | void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label) override; |
| 46 | |
| 47 | /// Create and write a TimelineMessageDirectoryPackage in the buffer |
| 48 | void SendTimelineMessageDirectoryPackage() override; |
| 49 | |
| 50 | /// Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer. |
| 51 | virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 52 | uint64_t relationshipGuid, |
| 53 | uint64_t headGuid, |
| 54 | uint64_t tailGuid) override; |
| 55 | private: |
| 56 | /// Reserves maximum packet size from buffer |
| 57 | void ReserveBuffer(); |
| 58 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 59 | template <typename Func, typename ... Params> |
| 60 | void ForwardWriteBinaryFunction(Func& func, Params&& ... params); |
| 61 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 62 | IBufferManager& m_BufferManager; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 63 | IPacketBufferPtr m_WriteBuffer; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 64 | unsigned int m_Offset; |
| 65 | unsigned int m_BufferSize; |
| 66 | }; |
| 67 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 68 | template <typename Func, typename ... Params> |
| 69 | void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params) |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | ReserveBuffer(); |
| 74 | BOOST_ASSERT(m_WriteBuffer); |
| 75 | unsigned int numberOfBytesWritten = 0; |
| 76 | while (true) |
| 77 | { |
| 78 | TimelinePacketStatus result = func(std::forward<Params>(params)..., |
| 79 | &m_WriteBuffer->GetWritableData()[m_Offset], |
| 80 | m_BufferSize, |
| 81 | numberOfBytesWritten); |
| 82 | switch (result) |
| 83 | { |
| 84 | case TimelinePacketStatus::BufferExhaustion: |
| 85 | Commit(); |
| 86 | ReserveBuffer(); |
| 87 | continue; |
| 88 | |
| 89 | case TimelinePacketStatus::Error: |
| 90 | throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION()); |
| 91 | |
| 92 | default: |
| 93 | m_Offset += numberOfBytesWritten; |
| 94 | m_BufferSize -= numberOfBytesWritten; |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | catch (...) |
| 100 | { |
| 101 | throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION()); |
| 102 | } |
| 103 | } |
| 104 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 105 | } // namespace profiling |
| 106 | |
| 107 | } // namespace armnn |