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" |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 9 | #include "ProfilingUtils.hpp" |
| 10 | |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 11 | #include <client/include/ISendTimelinePacket.hpp> |
| 12 | |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 13 | #include <common/include/Assert.hpp> |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 14 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 17 | namespace arm |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 18 | { |
| 19 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 20 | namespace pipe |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 21 | { |
| 22 | |
| 23 | class SendTimelinePacket : public ISendTimelinePacket |
| 24 | { |
| 25 | public: |
| 26 | SendTimelinePacket(IBufferManager& bufferManager) |
| 27 | : m_BufferManager(bufferManager) |
| 28 | , m_WriteBuffer(nullptr) |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 29 | , m_Offset(8u) |
| 30 | , m_RemainingBufferSize(0u) |
Jim Flynn | 6e33041 | 2021-09-06 17:11:01 +0100 | [diff] [blame] | 31 | , m_PacketDataLength(0u) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 32 | {} |
| 33 | |
| 34 | /// Commits the current buffer and reset the member variables |
| 35 | void Commit() override; |
| 36 | |
| 37 | /// Create and write a TimelineEntityBinaryPacket from the parameters to the buffer. |
| 38 | void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override; |
| 39 | |
| 40 | /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer. |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 41 | void SendTimelineEventBinaryPacket(uint64_t timestamp, int threadId, uint64_t profilingGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 42 | |
| 43 | /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer. |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 44 | void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 45 | |
| 46 | /// Create and write a TimelineLabelBinaryPacket from the parameters to the buffer. |
| 47 | void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label) override; |
| 48 | |
| 49 | /// Create and write a TimelineMessageDirectoryPackage in the buffer |
| 50 | void SendTimelineMessageDirectoryPackage() override; |
| 51 | |
| 52 | /// Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer. |
| 53 | virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 54 | uint64_t relationshipGuid, |
| 55 | uint64_t headGuid, |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 56 | uint64_t tailGuid, |
| 57 | uint64_t attributeGuid) override; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 58 | private: |
| 59 | /// Reserves maximum packet size from buffer |
| 60 | void ReserveBuffer(); |
| 61 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 62 | template <typename Func, typename ... Params> |
| 63 | void ForwardWriteBinaryFunction(Func& func, Params&& ... params); |
| 64 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 65 | IBufferManager& m_BufferManager; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 66 | IPacketBufferPtr m_WriteBuffer; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 67 | unsigned int m_Offset; |
| 68 | unsigned int m_RemainingBufferSize; |
| 69 | |
| 70 | const unsigned int m_uint32_t_size = sizeof(uint32_t); |
| 71 | |
| 72 | std::pair<uint32_t, uint32_t> m_PacketHeader; |
| 73 | uint32_t m_PacketDataLength; |
| 74 | |
| 75 | bool m_DirectoryPackage = false; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 78 | template<typename Func, typename ... Params> |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 79 | void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params) |
| 80 | { |
| 81 | try |
| 82 | { |
| 83 | ReserveBuffer(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 84 | ARM_PIPE_ASSERT(m_WriteBuffer); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 85 | unsigned int numberOfBytesWritten = 0; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 86 | // Header will be prepended to the buffer on Commit() |
| 87 | while ( true ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 88 | { |
| 89 | TimelinePacketStatus result = func(std::forward<Params>(params)..., |
| 90 | &m_WriteBuffer->GetWritableData()[m_Offset], |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 91 | m_RemainingBufferSize, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 92 | numberOfBytesWritten); |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 93 | switch ( result ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 94 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 95 | case TimelinePacketStatus::BufferExhaustion: |
| 96 | Commit(); |
| 97 | ReserveBuffer(); |
| 98 | continue; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 99 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 100 | case TimelinePacketStatus::Error: |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 101 | throw arm::pipe::ProfilingException("Error processing while sending TimelineBinaryPacket", |
| 102 | LOCATION()); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 103 | |
Keith Davis | 5238aff | 2020-03-11 12:17:05 +0000 | [diff] [blame] | 104 | default: |
| 105 | m_Offset += numberOfBytesWritten; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 106 | m_RemainingBufferSize -= numberOfBytesWritten; |
| 107 | return; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 111 | catch (const arm::pipe::BufferExhaustion& ex) |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 112 | { |
| 113 | // ditto |
| 114 | throw ex; |
| 115 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 116 | catch (const arm::pipe::ProfilingException& ex) |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 117 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 118 | // don't swallow in the catch all block |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 119 | throw ex; |
| 120 | } |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 121 | catch ( ... ) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 122 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 123 | throw arm::pipe::ProfilingException("Unknown Exception thrown while sending TimelineBinaryPacket", LOCATION()); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 127 | } // namespace pipe |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 128 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 129 | } // namespace arm |