ArmNN
 20.05
SendTimelinePacket.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "SendTimelinePacket.hpp"
7 
8 namespace armnn
9 {
10 
11 namespace profiling
12 {
13 
15 {
16  if (m_WriteBuffer == nullptr)
17  {
18  // Can't commit from a null buffer
19  return;
20  }
21 
22  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 
46  // Commit the message
47  m_WriteBuffer.reset(nullptr);
48  // Reset offset to start after prepended header
49  m_Offset = 8;
50  m_RemainingBufferSize = 0;
51 }
52 
53 void SendTimelinePacket::ReserveBuffer()
54 {
55  if (m_WriteBuffer != nullptr)
56  {
57  // Buffer already reserved
58  return;
59  }
60 
61  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 
72  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;
79 }
80 
82 {
83  ForwardWriteBinaryFunction(WriteTimelineEntityBinary,
84  profilingGuid);
85 }
86 
88  std::thread::id threadId,
89  uint64_t profilingGuid)
90 {
91  ForwardWriteBinaryFunction(WriteTimelineEventBinary,
92  timestamp,
93  threadId,
94  profilingGuid);
95 }
96 
98 {
99  ForwardWriteBinaryFunction(WriteTimelineEventClassBinary,
100  profilingGuid);
101 }
102 
103 void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
104 {
105  ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket,
106  profilingGuid,
107  label);
108 }
109 
111  uint64_t relationshipGuid,
112  uint64_t headGuid,
113  uint64_t tailGuid)
114 {
115  ForwardWriteBinaryFunction(WriteTimelineRelationshipBinary,
116  relationshipType,
117  relationshipGuid,
118  headGuid,
119  tailGuid);
120 }
121 
123 {
124  try
125  {
126  // Flag to Reserve & Commit() that a DirectoryPackage is being sent
127  m_DirectoryPackage = true;
128  // Reserve buffer if it hasn't already been reserved
129  ReserveBuffer();
130  // Write to buffer
131  unsigned int numberOfBytesWritten = 0;
132  // Offset is initialised to 8
133  m_Offset = 0;
134 
135  TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
136  m_RemainingBufferSize,
137  numberOfBytesWritten);
139  {
140  throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
141  }
142 
143  // Commit the message
144  m_Offset += numberOfBytesWritten;
145  m_RemainingBufferSize -= numberOfBytesWritten;
146  Commit();
147  }
148  catch (...)
149  {
150  throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
151  }
152 }
153 
154 } // namespace profiling
155 
156 } // namespace armnn
virtual IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize)=0
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void SendTimelineMessageDirectoryPackage() override
Create and write a TimelineMessageDirectoryPackage in the buffer.
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)
void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label) override
Create and write a TimelineLabelBinaryPacket from the parameters to the buffer.
void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override
Create and write a TimelineEntityBinaryPacket from the parameters to the buffer.
std::pair< uint32_t, uint32_t > CreateTimelinePacketHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType, uint32_t streamId, uint32_t sequenceNumbered, uint32_t dataLength)
Creates a timeline packet header.
void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) override
Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
virtual void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true)=0
Copyright (c) 2020 ARM Limited.
TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void Commit() override
Commits the current buffer and reset the member variables.
#define MAX_METADATA_PACKET_LENGTH
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
void SendTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid) override
Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid) override
Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer.
TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)