blob: a0fbf5c01fa5bb94650b6d8f7990496ee1c56b8b [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
82void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp, uint32_t threadId, uint64_t profilingGuid)
83{
84 FORWARD_WRITE_BINARY_FUNC(WriteTimelineEventBinaryPacket,
85 timestamp,
86 threadId,
87 profilingGuid,
88 &m_WriteBuffer->GetWritableData()[m_Offset],
89 m_BufferSize);
90}
91
92void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid)
93{
94 FORWARD_WRITE_BINARY_FUNC(WriteTimelineEventClassBinaryPacket,
95 profilingGuid,
96 &m_WriteBuffer->GetWritableData()[m_Offset],
97 m_BufferSize);
98}
99
100void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
101{
102 FORWARD_WRITE_BINARY_FUNC(WriteTimelineLabelBinaryPacket,
103 profilingGuid,
104 label,
105 &m_WriteBuffer->GetWritableData()[m_Offset],
106 m_BufferSize);
107}
108
109void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
110 uint64_t relationshipGuid,
111 uint64_t headGuid,
112 uint64_t tailGuid)
113{
114 FORWARD_WRITE_BINARY_FUNC(WriteTimelineRelationshipBinaryPacket,
115 relationshipType,
116 relationshipGuid,
117 headGuid,
118 tailGuid,
119 &m_WriteBuffer->GetWritableData()[m_Offset],
120 m_BufferSize);
121}
122
123void SendTimelinePacket::SendTimelineMessageDirectoryPackage()
124{
125 try
126 {
127 // Reserve buffer if hasn't already reserved
128 ReserveBuffer();
129
130 unsigned int numberOfBytes = 0;
131 // Write to buffer
132 TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
133 m_BufferSize,
134 numberOfBytes);
135
136 if (result != armnn::profiling::TimelinePacketStatus::Ok)
137 {
138 throw RuntimeException("Error processing TimelineMessageDirectoryPackage.", CHECK_LOCATION());
139 }
140
141 // Commit the message
142 m_Offset += numberOfBytes;
143 m_BufferSize -= numberOfBytes;
144 m_BufferManager.Commit(m_WriteBuffer, m_Offset);
145 }
146 catch(...)
147 {
148 throw RuntimeException("Error processing TimelineMessageDirectoryPackage.", CHECK_LOCATION());
149 }
150}
151
152} // namespace profiling
153
154} // namespace armnn