blob: d769b64a05e3e9a7392e7a90d08d14c4f122174d [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
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;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010027}
28
29void SendTimelinePacket::ReserveBuffer()
30{
Matteo Martincigh9723d022019-11-13 10:56:41 +000031 if (m_WriteBuffer != nullptr)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010032 {
Matteo Martincigh9723d022019-11-13 10:56:41 +000033 // Buffer already reserved
34 return;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010035 }
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010036
Matteo Martincigh9723d022019-11-13 10:56:41 +000037 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;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010049}
50
51void SendTimelinePacket::SendTimelineEntityBinaryPacket(uint64_t profilingGuid)
52{
Matteo Martincigh9723d022019-11-13 10:56:41 +000053 ForwardWriteBinaryFunction(WriteTimelineEntityBinaryPacket,
54 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010055}
56
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000057void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp,
58 std::thread::id threadId,
59 uint64_t profilingGuid)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010060{
Matteo Martincigh9723d022019-11-13 10:56:41 +000061 ForwardWriteBinaryFunction(WriteTimelineEventBinaryPacket,
62 timestamp,
63 threadId,
64 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010065}
66
67void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid)
68{
Matteo Martincigh9723d022019-11-13 10:56:41 +000069 ForwardWriteBinaryFunction(WriteTimelineEventClassBinaryPacket,
70 profilingGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010071}
72
73void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label)
74{
Matteo Martincigh9723d022019-11-13 10:56:41 +000075 ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket,
76 profilingGuid,
77 label);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010078}
79
80void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
81 uint64_t relationshipGuid,
82 uint64_t headGuid,
83 uint64_t tailGuid)
84{
Matteo Martincigh9723d022019-11-13 10:56:41 +000085 ForwardWriteBinaryFunction(WriteTimelineRelationshipBinaryPacket,
86 relationshipType,
87 relationshipGuid,
88 headGuid,
89 tailGuid);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010090}
91
92void SendTimelinePacket::SendTimelineMessageDirectoryPackage()
93{
94 try
95 {
Matteo Martincigh9723d022019-11-13 10:56:41 +000096 // Reserve buffer if it hasn't already been reserved
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010097 ReserveBuffer();
98
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010099 // Write to buffer
Matteo Martincigh9723d022019-11-13 10:56:41 +0000100 unsigned int numberOfBytesWritten = 0;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100101 TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset],
102 m_BufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +0000103 numberOfBytesWritten);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100104 if (result != armnn::profiling::TimelinePacketStatus::Ok)
105 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000106 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100107 }
108
109 // Commit the message
Matteo Martincigh9723d022019-11-13 10:56:41 +0000110 m_Offset += numberOfBytesWritten;
111 m_BufferSize -= numberOfBytesWritten;
112 Commit();
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100113 }
Matteo Martincigh9723d022019-11-13 10:56:41 +0000114 catch (...)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100115 {
Matteo Martincigh9723d022019-11-13 10:56:41 +0000116 throw RuntimeException("Error processing TimelineMessageDirectoryPackage", CHECK_LOCATION());
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100117 }
118}
119
120} // namespace profiling
121
122} // namespace armnn