ArmNN  NotReleased
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  // Commit the message
23  m_BufferManager.Commit(m_WriteBuffer, m_Offset);
24  m_WriteBuffer.reset(nullptr);
25  m_Offset = 0;
26  m_BufferSize = 0;
27 }
28 
29 void SendTimelinePacket::ReserveBuffer()
30 {
31  if (m_WriteBuffer != nullptr)
32  {
33  // Buffer already reserved
34  return;
35  }
36 
37  uint32_t reserved = 0;
38 
39  // Reserve the buffer
40  m_WriteBuffer = m_BufferManager.Reserve(MAX_METADATA_PACKET_LENGTH, reserved);
41 
42  // Check if there is enough space in the buffer
43  if (m_WriteBuffer == nullptr || reserved < m_Offset)
44  {
45  throw BufferExhaustion("No space left on buffer", CHECK_LOCATION());
46  }
47 
48  m_BufferSize = reserved;
49 }
50 
52 {
53  ForwardWriteBinaryFunction(WriteTimelineEntityBinaryPacket,
54  profilingGuid);
55 }
56 
58  std::thread::id threadId,
59  uint64_t profilingGuid)
60 {
61  ForwardWriteBinaryFunction(WriteTimelineEventBinaryPacket,
62  timestamp,
63  threadId,
64  profilingGuid);
65 }
66 
68 {
69  ForwardWriteBinaryFunction(WriteTimelineEventClassBinaryPacket,
70  profilingGuid);
71 }
72 
73 void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
74 {
75  ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket,
76  profilingGuid,
77  label);
78 }
79 
81  uint64_t relationshipGuid,
82  uint64_t headGuid,
83  uint64_t tailGuid)
84 {
85  ForwardWriteBinaryFunction(WriteTimelineRelationshipBinaryPacket,
86  relationshipType,
87  relationshipGuid,
88  headGuid,
89  tailGuid);
90 }
91 
93 {
94  try
95  {
96  // Reserve buffer if it hasn't already been reserved
97  ReserveBuffer();
98 
99  // Write to buffer
100  unsigned int numberOfBytesWritten = 0;
101  TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
102  m_BufferSize,
103  numberOfBytesWritten);
105  {
106  throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
107  }
108 
109  // Commit the message
110  m_Offset += numberOfBytesWritten;
111  m_BufferSize -= numberOfBytesWritten;
112  Commit();
113  }
114  catch (...)
115  {
116  throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
117  }
118 }
119 
120 } // namespace profiling
121 
122 } // namespace armnn
void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override
Create and write a TimelineEntityBinaryPacket from the parameters to the buffer.
TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label) override
Create and write a TimelineLabelBinaryPacket from the parameters to the buffer.
void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) override
Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:169
void SendTimelineMessageDirectoryPackage() override
Create and write a TimelineMessageDirectoryPackage in the buffer.
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
virtual void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true)=0
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 WriteTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
virtual IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize)=0
#define MAX_METADATA_PACKET_LENGTH
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
void SendTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid) override
Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
void Commit() override
Commits the current buffer and reset the member variables.