Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 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 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 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) |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 28 | , m_Offset(8u) |
| 29 | , m_RemainingBufferSize(0u) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 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. |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 39 | void SendTimelineEventBinaryPacket(uint64_t timestamp, int 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. |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 42 | void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 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, |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 54 | uint64_t tailGuid, |
| 55 | uint64_t attributeGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 56 | private: |
| 57 | /// Reserves maximum packet size from buffer |
| 58 | void ReserveBuffer(); |
| 59 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 60 | template <typename Func, typename ... Params> |
| 61 | void ForwardWriteBinaryFunction(Func& func, Params&& ... params); |
| 62 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 63 | IBufferManager& m_BufferManager; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 64 | IPacketBufferPtr m_WriteBuffer; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 65 | unsigned int m_Offset; |
| 66 | unsigned int m_RemainingBufferSize; |
| 67 | |
| 68 | const unsigned int m_uint32_t_size = sizeof(uint32_t); |
| 69 | |
| 70 | std::pair<uint32_t, uint32_t> m_PacketHeader; |
| 71 | uint32_t m_PacketDataLength; |
| 72 | |
| 73 | bool m_DirectoryPackage = false; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 76 | template<typename Func, typename ... Params> |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 77 | void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params) |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | ReserveBuffer(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 82 | ARMNN_ASSERT(m_WriteBuffer); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 83 | unsigned int numberOfBytesWritten = 0; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 84 | // Header will be prepended to the buffer on Commit() |
| 85 | while ( true ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 86 | { |
| 87 | TimelinePacketStatus result = func(std::forward<Params>(params)..., |
| 88 | &m_WriteBuffer->GetWritableData()[m_Offset], |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 89 | m_RemainingBufferSize, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 90 | numberOfBytesWritten); |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 91 | switch ( result ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 92 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 93 | case TimelinePacketStatus::BufferExhaustion: |
| 94 | Commit(); |
| 95 | ReserveBuffer(); |
| 96 | continue; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 97 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 98 | case TimelinePacketStatus::Error: |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 99 | throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION()); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 100 | |
Keith Davis | 5238aff | 2020-03-11 12:17:05 +0000 | [diff] [blame] | 101 | default: |
| 102 | m_Offset += numberOfBytesWritten; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 103 | m_RemainingBufferSize -= numberOfBytesWritten; |
| 104 | return; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 108 | catch (const RuntimeException& ex) |
| 109 | { |
| 110 | // don't swallow in the catch all block |
| 111 | throw ex; |
| 112 | } |
| 113 | catch (const BufferExhaustion& ex) |
| 114 | { |
| 115 | // ditto |
| 116 | throw ex; |
| 117 | } |
| 118 | catch (const Exception& ex) |
| 119 | { |
| 120 | throw ex; |
| 121 | } |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 122 | catch ( ... ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 123 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 124 | throw RuntimeException("Unknown Exception thrown while sending TimelineBinaryPacket", CHECK_LOCATION()); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 128 | } // namespace profiling |
| 129 | |
| 130 | } // namespace armnn |