blob: 5152d66cabed742b38c1fe21e2de4dc59f419fc1 [file] [log] [blame]
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// 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
67 if (m_WriteBuffer == nullptr || reserved < m_Offset)
68 {
69 throw BufferExhaustion("No space left on buffer", CHECK_LOCATION());
70 }
71
Keith Davis97da5e22020-03-05 16:25:28 +000072 if (m_DirectoryPackage)
73 {
74 m_RemainingBufferSize = reserved;
75 return;
76 }
77 // Account for the header size which is added at Commit()
78 m_RemainingBufferSize = reserved - 8;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010079}
80
81void SendTimelinePacket::SendTimelineEntityBinaryPacket(uint64_t profilingGuid)
82{
Keith Davis97da5e22020-03-05 16:25:28 +000083 ForwardWriteBinaryFunction(WriteTimelineEntityBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +000084 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010085}
86
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000087void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp,
88 std::thread::id threadId,
89 uint64_t profilingGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010090{
Keith Davis97da5e22020-03-05 16:25:28 +000091 ForwardWriteBinaryFunction(WriteTimelineEventBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +000092 timestamp,
93 threadId,
94 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010095}
96
97void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid)
98{
Keith Davis97da5e22020-03-05 16:25:28 +000099 ForwardWriteBinaryFunction(WriteTimelineEventClassBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000100 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100101}
102
103void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
104{
Matteo Martincigh9723d022019-11-13 10:56:41 +0000105 ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket,
106 profilingGuid,
107 label);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100108}
109
110void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
111 uint64_t relationshipGuid,
112 uint64_t headGuid,
113 uint64_t tailGuid)
114{
Keith Davis97da5e22020-03-05 16:25:28 +0000115 ForwardWriteBinaryFunction(WriteTimelineRelationshipBinary,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000116 relationshipType,
117 relationshipGuid,
118 headGuid,
119 tailGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100120}
121
122void SendTimelinePacket::SendTimelineMessageDirectoryPackage()
123{
124 try
125 {
Keith Davis97da5e22020-03-05 16:25:28 +0000126 // Flag to Reserve & Commit() that a DirectoryPackage is being sent
127 m_DirectoryPackage = true;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000128 // Reserve buffer if it hasn't already been reserved
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100129 ReserveBuffer();
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100130 // Write to buffer
Matteo Martincigh9723d022019-11-13 10:56:41 +0000131 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +0000132 // Offset is initialised to 8
133 m_Offset = 0;
134
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100135 TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +0000136 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000137 numberOfBytesWritten);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100138 if (result != armnn::profiling::TimelinePacketStatus::Ok)
139 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000140 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100141 }
142
143 // Commit the message
Matteo Martincigh9723d022019-11-13 10:56:41 +0000144 m_Offset += numberOfBytesWritten;
Keith Davis97da5e22020-03-05 16:25:28 +0000145 m_RemainingBufferSize -= numberOfBytesWritten;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000146 Commit();
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100147 }
Matteo Martincigh9723d022019-11-13 10:56:41 +0000148 catch (...)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100149 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000150 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100151 }
152}
153
154} // namespace profiling
155
156} // namespace armnn