blob: f20671fe35fac446963d0b4056aa32ce335724fe [file] [log] [blame]
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01001//
Jim Flynn1fdeb992020-07-09 07:28:37 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "IBufferManager.hpp"
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01009#include "ProfilingUtils.hpp"
10
Jim Flynn27761832022-03-20 21:52:17 +000011#include <client/include/ISendTimelinePacket.hpp>
12
Jim Flynn6730fe92022-03-10 22:57:47 +000013#include <common/include/Assert.hpp>
Matteo Martincigh9723d022019-11-13 10:56:41 +000014
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010015#include <memory>
16
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000017namespace arm
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010018{
19
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000020namespace pipe
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010021{
22
23class SendTimelinePacket : public ISendTimelinePacket
24{
25public:
26 SendTimelinePacket(IBufferManager& bufferManager)
27 : m_BufferManager(bufferManager)
28 , m_WriteBuffer(nullptr)
Keith Davis97da5e22020-03-05 16:25:28 +000029 , m_Offset(8u)
30 , m_RemainingBufferSize(0u)
Jim Flynn6e330412021-09-06 17:11:01 +010031 , m_PacketDataLength(0u)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010032 {}
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 Flynn1fdeb992020-07-09 07:28:37 +010041 void SendTimelineEventBinaryPacket(uint64_t timestamp, int threadId, uint64_t profilingGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010042
43 /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
Jim Flynn1892d212020-05-26 21:10:49 +010044 void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010045
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 Williams0a336dc2020-05-11 15:39:58 +010056 uint64_t tailGuid,
57 uint64_t attributeGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010058private:
59 /// Reserves maximum packet size from buffer
60 void ReserveBuffer();
61
Matteo Martincigh9723d022019-11-13 10:56:41 +000062 template <typename Func, typename ... Params>
63 void ForwardWriteBinaryFunction(Func& func, Params&& ... params);
64
Keith Davis97da5e22020-03-05 16:25:28 +000065 IBufferManager& m_BufferManager;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000066 IPacketBufferPtr m_WriteBuffer;
Keith Davis97da5e22020-03-05 16:25:28 +000067 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 Armagan7bbdf9d2019-10-24 10:26:05 +010076};
77
Keith Davis97da5e22020-03-05 16:25:28 +000078template<typename Func, typename ... Params>
Matteo Martincigh9723d022019-11-13 10:56:41 +000079void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params)
80{
81 try
82 {
83 ReserveBuffer();
Jim Flynn6730fe92022-03-10 22:57:47 +000084 ARM_PIPE_ASSERT(m_WriteBuffer);
Matteo Martincigh9723d022019-11-13 10:56:41 +000085 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +000086 // Header will be prepended to the buffer on Commit()
87 while ( true )
Matteo Martincigh9723d022019-11-13 10:56:41 +000088 {
89 TimelinePacketStatus result = func(std::forward<Params>(params)...,
90 &m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +000091 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +000092 numberOfBytesWritten);
Keith Davis97da5e22020-03-05 16:25:28 +000093 switch ( result )
Matteo Martincigh9723d022019-11-13 10:56:41 +000094 {
Keith Davis97da5e22020-03-05 16:25:28 +000095 case TimelinePacketStatus::BufferExhaustion:
96 Commit();
97 ReserveBuffer();
98 continue;
Matteo Martincigh9723d022019-11-13 10:56:41 +000099
Keith Davis97da5e22020-03-05 16:25:28 +0000100 case TimelinePacketStatus::Error:
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000101 throw arm::pipe::ProfilingException("Error processing while sending TimelineBinaryPacket",
102 LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000103
Keith Davis5238aff2020-03-11 12:17:05 +0000104 default:
105 m_Offset += numberOfBytesWritten;
Keith Davis97da5e22020-03-05 16:25:28 +0000106 m_RemainingBufferSize -= numberOfBytesWritten;
107 return;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000108 }
109 }
110 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000111 catch (const arm::pipe::BufferExhaustion& ex)
Jim Flynn6398a982020-05-27 17:05:21 +0100112 {
113 // ditto
114 throw ex;
115 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000116 catch (const arm::pipe::ProfilingException& ex)
Jim Flynn6398a982020-05-27 17:05:21 +0100117 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000118 // don't swallow in the catch all block
Jim Flynn6398a982020-05-27 17:05:21 +0100119 throw ex;
120 }
Keith Davis97da5e22020-03-05 16:25:28 +0000121 catch ( ... )
Matteo Martincigh9723d022019-11-13 10:56:41 +0000122 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000123 throw arm::pipe::ProfilingException("Unknown Exception thrown while sending TimelineBinaryPacket", LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000124 }
125}
126
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000127} // namespace pipe
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100128
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000129} // namespace arm