blob: 11e3d2f48981c2641cdb288bf685b1d6d3028120 [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#include "SendTimelinePacket.hpp"
7
8namespace armnn
9{
10
11namespace profiling
12{
13
14void SendTimelinePacket::Commit()
15{
Matteo Martincigh9723d022019-11-13 10:56:41 +000016 if (m_WriteBuffer == nullptr)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010017 {
Matteo Martincigh9723d022019-11-13 10:56:41 +000018 // Can't commit from a null buffer
19 return;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010020 }
Matteo Martincigh9723d022019-11-13 10:56:41 +000021
Keith Davis97da5e22020-03-05 16:25:28 +000022 if (!m_DirectoryPackage)
23 {
24 // Datalength should be Offset minus the two header words
25 m_PacketDataLength = m_Offset - m_uint32_t_size * 2;
26 // Reset offset to prepend header with full packet datalength
27 m_Offset = 0;
28
29 // Add header before commit
30 m_PacketHeader = CreateTimelinePacketHeader(1,0,1,0,0,m_PacketDataLength);
31
32 // Write the timeline binary packet header to the buffer
33 WriteUint32(m_WriteBuffer->GetWritableData(), m_Offset, m_PacketHeader.first);
34 m_Offset += m_uint32_t_size;
35 WriteUint32(m_WriteBuffer->GetWritableData(), m_Offset, m_PacketHeader.second);
36
37 m_BufferManager.Commit(m_WriteBuffer, m_PacketDataLength + m_uint32_t_size * 2);
38
39 }
40 else
41 {
42 m_DirectoryPackage = false;
43 m_BufferManager.Commit(m_WriteBuffer, m_Offset);
44 }
45
Matteo Martincigh9723d022019-11-13 10:56:41 +000046 // Commit the message
Matteo Martincigh9723d022019-11-13 10:56:41 +000047 m_WriteBuffer.reset(nullptr);
Keith Davis97da5e22020-03-05 16:25:28 +000048 // Reset offset to start after prepended header
49 m_Offset = 8;
50 m_RemainingBufferSize = 0;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010051}
52
53void SendTimelinePacket::ReserveBuffer()
54{
Matteo Martincigh9723d022019-11-13 10:56:41 +000055 if (m_WriteBuffer != nullptr)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010056 {
Matteo Martincigh9723d022019-11-13 10:56:41 +000057 // Buffer already reserved
58 return;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010059 }
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010060
Matteo Martincigh9723d022019-11-13 10:56:41 +000061 uint32_t reserved = 0;
62
63 // Reserve the buffer
64 m_WriteBuffer = m_BufferManager.Reserve(MAX_METADATA_PACKET_LENGTH, reserved);
65
66 // Check if there is enough space in the buffer
Jim Flynn0204f092020-06-22 20:41:43 +010067 if (m_WriteBuffer == nullptr)
Matteo Martincigh9723d022019-11-13 10:56:41 +000068 {
Jim Flynn0204f092020-06-22 20:41:43 +010069 throw BufferExhaustion("No free buffers left", CHECK_LOCATION());
70 }
71 if (reserved < m_Offset)
72 {
73 throw BufferExhaustion("Reserved space too small for use", CHECK_LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +000074 }
75
Keith Davis97da5e22020-03-05 16:25:28 +000076 if (m_DirectoryPackage)
77 {
78 m_RemainingBufferSize = reserved;
79 return;
80 }
81 // Account for the header size which is added at Commit()
82 m_RemainingBufferSize = reserved - 8;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010083}
84
85void SendTimelinePacket::SendTimelineEntityBinaryPacket(uint64_t profilingGuid)
86{
Keith Davis97da5e22020-03-05 16:25:28 +000087 ForwardWriteBinaryFunction(WriteTimelineEntityBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +000088 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010089}
90
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000091void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp,
Jim Flynn1fdeb992020-07-09 07:28:37 +010092 int threadId,
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000093 uint64_t profilingGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010094{
Keith Davis97da5e22020-03-05 16:25:28 +000095 ForwardWriteBinaryFunction(WriteTimelineEventBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +000096 timestamp,
97 threadId,
98 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010099}
100
Jim Flynn1892d212020-05-26 21:10:49 +0100101void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100102{
Keith Davis97da5e22020-03-05 16:25:28 +0000103 ForwardWriteBinaryFunction(WriteTimelineEventClassBinary,
Jim Flynn1892d212020-05-26 21:10:49 +0100104 profilingGuid,
105 nameGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100106}
107
108void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
109{
Matteo Martincigh9723d022019-11-13 10:56:41 +0000110 ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket,
111 profilingGuid,
112 label);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100113}
114
115void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
116 uint64_t relationshipGuid,
117 uint64_t headGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +0100118 uint64_t tailGuid,
119 uint64_t attributeGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100120{
Keith Davis97da5e22020-03-05 16:25:28 +0000121 ForwardWriteBinaryFunction(WriteTimelineRelationshipBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000122 relationshipType,
123 relationshipGuid,
124 headGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +0100125 tailGuid,
126 attributeGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100127}
128
129void SendTimelinePacket::SendTimelineMessageDirectoryPackage()
130{
131 try
132 {
Keith Davis97da5e22020-03-05 16:25:28 +0000133 // Flag to Reserve & Commit() that a DirectoryPackage is being sent
134 m_DirectoryPackage = true;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000135 // Reserve buffer if it hasn't already been reserved
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100136 ReserveBuffer();
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100137 // Write to buffer
Matteo Martincigh9723d022019-11-13 10:56:41 +0000138 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +0000139 // Offset is initialised to 8
140 m_Offset = 0;
141
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100142 TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +0000143 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000144 numberOfBytesWritten);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100145 if (result != armnn::profiling::TimelinePacketStatus::Ok)
146 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000147 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100148 }
149
150 // Commit the message
Matteo Martincigh9723d022019-11-13 10:56:41 +0000151 m_Offset += numberOfBytesWritten;
Keith Davis97da5e22020-03-05 16:25:28 +0000152 m_RemainingBufferSize -= numberOfBytesWritten;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000153 Commit();
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100154 }
Matteo Martincigh9723d022019-11-13 10:56:41 +0000155 catch (...)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100156 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000157 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100158 }
159}
160
161} // namespace profiling
162
163} // namespace armnn