Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "SendTimelinePacket.hpp" |
| 7 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 8 | namespace arm |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 9 | { |
| 10 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 11 | namespace pipe |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 12 | { |
| 13 | |
| 14 | void SendTimelinePacket::Commit() |
| 15 | { |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 16 | if (m_WriteBuffer == nullptr) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 17 | { |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 18 | // Can't commit from a null buffer |
| 19 | return; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 20 | } |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 21 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 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 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 46 | // Commit the message |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 47 | m_WriteBuffer.reset(nullptr); |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 48 | // Reset offset to start after prepended header |
| 49 | m_Offset = 8; |
| 50 | m_RemainingBufferSize = 0; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void SendTimelinePacket::ReserveBuffer() |
| 54 | { |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 55 | if (m_WriteBuffer != nullptr) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 56 | { |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 57 | // Buffer already reserved |
| 58 | return; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 59 | } |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 60 | |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 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 |
Jim Flynn | 0204f09 | 2020-06-22 20:41:43 +0100 | [diff] [blame] | 67 | if (m_WriteBuffer == nullptr) |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 68 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 69 | throw arm::pipe::BufferExhaustion("No free buffers left", LOCATION()); |
Jim Flynn | 0204f09 | 2020-06-22 20:41:43 +0100 | [diff] [blame] | 70 | } |
| 71 | if (reserved < m_Offset) |
| 72 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 73 | throw arm::pipe::BufferExhaustion("Reserved space too small for use", LOCATION()); |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 76 | if (m_DirectoryPackage) |
| 77 | { |
| 78 | m_RemainingBufferSize = reserved; |
| 79 | return; |
| 80 | } |
| 81 | // Account for the header size which is added at Commit() |
| 82 | m_RemainingBufferSize = reserved - 8; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SendTimelinePacket::SendTimelineEntityBinaryPacket(uint64_t profilingGuid) |
| 86 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 87 | ForwardWriteBinaryFunction(WriteTimelineEntityBinary, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 88 | profilingGuid); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 91 | void SendTimelinePacket::SendTimelineEventBinaryPacket(uint64_t timestamp, |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 92 | int threadId, |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 93 | uint64_t profilingGuid) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 94 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 95 | ForwardWriteBinaryFunction(WriteTimelineEventBinary, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 96 | timestamp, |
| 97 | threadId, |
| 98 | profilingGuid); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 101 | void SendTimelinePacket::SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 102 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 103 | ForwardWriteBinaryFunction(WriteTimelineEventClassBinary, |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 104 | profilingGuid, |
| 105 | nameGuid); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void SendTimelinePacket::SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label) |
| 109 | { |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 110 | ForwardWriteBinaryFunction(WriteTimelineLabelBinaryPacket, |
| 111 | profilingGuid, |
| 112 | label); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void SendTimelinePacket::SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 116 | uint64_t relationshipGuid, |
| 117 | uint64_t headGuid, |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 118 | uint64_t tailGuid, |
| 119 | uint64_t attributeGuid) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 120 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 121 | ForwardWriteBinaryFunction(WriteTimelineRelationshipBinary, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 122 | relationshipType, |
| 123 | relationshipGuid, |
| 124 | headGuid, |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 125 | tailGuid, |
| 126 | attributeGuid); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void SendTimelinePacket::SendTimelineMessageDirectoryPackage() |
| 130 | { |
| 131 | try |
| 132 | { |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 133 | // Flag to Reserve & Commit() that a DirectoryPackage is being sent |
| 134 | m_DirectoryPackage = true; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 135 | // Reserve buffer if it hasn't already been reserved |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 136 | ReserveBuffer(); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 137 | // Write to buffer |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 138 | unsigned int numberOfBytesWritten = 0; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 139 | // Offset is initialised to 8 |
| 140 | m_Offset = 0; |
| 141 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 142 | TimelinePacketStatus result = WriteTimelineMessageDirectoryPackage(&m_WriteBuffer->GetWritableData()[m_Offset], |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 143 | m_RemainingBufferSize, |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 144 | numberOfBytesWritten); |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 145 | if (result != TimelinePacketStatus::Ok) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 146 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 147 | throw arm::pipe::ProfilingException("Error processing TimelineMessageDirectoryPackage", LOCATION()); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Commit the message |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 151 | m_Offset += numberOfBytesWritten; |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 152 | m_RemainingBufferSize -= numberOfBytesWritten; |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 153 | Commit(); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 154 | } |
Matteo Martincigh | 9723d02 | 2019-11-13 10:56:41 +0000 | [diff] [blame] | 155 | catch (...) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 156 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 157 | throw arm::pipe::ProfilingException("Error processing TimelineMessageDirectoryPackage", LOCATION()); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 161 | } // namespace pipe |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 162 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 163 | } // namespace arm |