blob: 707bfba5c4a94505b9b084ce2a92fcdf5da94b46 [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{
16 if (m_WriteBuffer != nullptr)
17 {
18 // Commit the message
19 m_BufferManager.Commit(m_WriteBuffer, m_Offset);
20 m_WriteBuffer.reset(nullptr);
21 m_Offset = 0;
22 m_BufferSize = 0;
23 }
24}
25
26void SendTimelinePacket::ReserveBuffer()
27{
28 if (m_WriteBuffer == nullptr)
29 {
30 uint32_t reserved = 0;
31
32 // Reserve the buffer
33 m_WriteBuffer = m_BufferManager.Reserve(MAX_METADATA_PACKET_LENGTH, reserved);
34
35 // Check if there is enough space in the buffer
36 if (m_WriteBuffer == nullptr || reserved < m_Offset)
37 {
Matteo Martincigh102cdbd2019-10-28 11:42:50 +000038 throw BufferExhaustion("No space left on buffer", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010039 }
40 m_BufferSize = reserved;
41 }
42}
43
44#define FORWARD_WRITE_BINARY_FUNC(func, ...) \
45try \
46{ \
47 ReserveBuffer(); \
48 unsigned int numberOfBytes = 0; \
49 while (1) \
50 { \
51 TimelinePacketStatus result = func(__VA_ARGS__, numberOfBytes); \
52 if (result == armnn::profiling::TimelinePacketStatus::BufferExhaustion) \
53 { \
54 Commit(); \
55 ReserveBuffer(); \
56 } \
57 else if (result == armnn::profiling::TimelinePacketStatus::Error) \
58 { \
59 throw RuntimeException("Error processing while sending TimelineBinaryPacket.", CHECK_LOCATION()); \
60 } \
61 else \
62 { \
63 break; \
64 } \
65 } \
66 m_Offset += numberOfBytes; \
67 m_BufferSize -= numberOfBytes; \
68} \
69catch(...) \
70{ \
71 throw RuntimeException("Error processing while sending TimelineBinaryPacket.", CHECK_LOCATION()); \
72}
73
74void SendTimelinePacket::SendTimelineEntityBinaryPacket(uint64_t profilingGuid)
75{
76 FORWARD_WRITE_BINARY_FUNC(WriteTimelineEntityBinaryPacket,
77 profilingGuid,
78 &m_WriteBuffer->GetWritableData()[m_Offset],
79 m_BufferSize);
80}
81
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000082void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp,
83 std::thread::id threadId,
84 uint64_t profilingGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010085{
86 FORWARD_WRITE_BINARY_FUNC(WriteTimelineEventBinaryPacket,
87 timestamp,
88 threadId,
89 profilingGuid,
90 &m_WriteBuffer->GetWritableData()[m_Offset],
91 m_BufferSize);
92}
93
94void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid)
95{
96 FORWARD_WRITE_BINARY_FUNC(WriteTimelineEventClassBinaryPacket,
97 profilingGuid,
98 &m_WriteBuffer->GetWritableData()[m_Offset],
99 m_BufferSize);
100}
101
102void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
103{
104 FORWARD_WRITE_BINARY_FUNC(WriteTimelineLabelBinaryPacket,
105 profilingGuid,
106 label,
107 &m_WriteBuffer->GetWritableData()[m_Offset],
108 m_BufferSize);
109}
110
111void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
112 uint64_t relationshipGuid,
113 uint64_t headGuid,
114 uint64_t tailGuid)
115{
116 FORWARD_WRITE_BINARY_FUNC(WriteTimelineRelationshipBinaryPacket,
117 relationshipType,
118 relationshipGuid,
119 headGuid,
120 tailGuid,
121 &m_WriteBuffer->GetWritableData()[m_Offset],
122 m_BufferSize);
123}
124
125void SendTimelinePacket::SendTimelineMessageDirectoryPackage()
126{
127 try
128 {
129 // Reserve buffer if hasn't already reserved
130 ReserveBuffer();
131
132 unsigned int numberOfBytes = 0;
133 // Write to buffer
134 TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
135 m_BufferSize,
136 numberOfBytes);
137
138 if (result != armnn::profiling::TimelinePacketStatus::Ok)
139 {
140 throw RuntimeException("Error processing TimelineMessageDirectoryPackage.", CHECK_LOCATION());
141 }
142
143 // Commit the message
144 m_Offset += numberOfBytes;
145 m_BufferSize -= numberOfBytes;
146 m_BufferManager.Commit(m_WriteBuffer, m_Offset);
147 }
148 catch(...)
149 {
150 throw RuntimeException("Error processing TimelineMessageDirectoryPackage.", CHECK_LOCATION());
151 }
152}
153
154} // namespace profiling
155
156} // namespace armnn