Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ProfilingUtils.hpp" |
| 7 | |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 8 | #include <common/include/Assert.hpp> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 9 | #include <common/include/CommonProfilingUtils.hpp> |
Jim Flynn | 9c85b41 | 2022-03-16 00:27:43 +0000 | [diff] [blame] | 10 | #include <common/include/NumericCast.hpp> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 11 | #include <common/include/ProfilingException.hpp> |
| 12 | #include <common/include/SwTrace.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 13 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 14 | #include <armnn/Version.hpp> |
| 15 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 16 | #include <fstream> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 17 | #include <iostream> |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 18 | #include <limits> |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 19 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 20 | namespace arm |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 21 | { |
| 22 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 23 | namespace pipe |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 24 | { |
| 25 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 26 | void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize) |
| 27 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 28 | ARM_PIPE_ASSERT(packetBuffer); |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 29 | |
| 30 | WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize); |
| 31 | } |
| 32 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 33 | uint32_t ConstructHeader(uint32_t packetFamily, |
| 34 | uint32_t packetId) |
| 35 | { |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 36 | return (( packetFamily & 0x0000003F ) << 26 )| |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 37 | (( packetId & 0x000003FF ) << 16 ); |
| 38 | } |
| 39 | |
| 40 | uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType) |
| 41 | { |
| 42 | return ((packetFamily & 0x0000003F) << 26) | |
| 43 | ((packetClass & 0x0000007F) << 19) | |
| 44 | ((packetType & 0x00000007) << 16); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 48 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 49 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 50 | |
| 51 | WriteUint64(packetBuffer->GetWritableData(), offset, value); |
| 52 | } |
| 53 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 54 | void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 55 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 56 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 57 | |
| 58 | WriteUint32(packetBuffer->GetWritableData(), offset, value); |
| 59 | } |
| 60 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 61 | void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 62 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 63 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 64 | |
| 65 | WriteUint16(packetBuffer->GetWritableData(), offset, value); |
| 66 | } |
| 67 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 68 | void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value) |
| 69 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 70 | ARM_PIPE_ASSERT(packetBuffer); |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 71 | |
| 72 | WriteUint8(packetBuffer->GetWritableData(), offset, value); |
| 73 | } |
| 74 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 75 | void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]) |
| 76 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 77 | ARM_PIPE_ASSERT(packetBuffer); |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 78 | |
| 79 | ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue); |
| 80 | } |
| 81 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 82 | uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 83 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 84 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 85 | |
| 86 | return ReadUint64(packetBuffer->GetReadableData(), offset); |
| 87 | } |
| 88 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 89 | uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 90 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 91 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 92 | |
| 93 | return ReadUint32(packetBuffer->GetReadableData(), offset); |
| 94 | } |
| 95 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 96 | uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 97 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 98 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 99 | |
| 100 | return ReadUint16(packetBuffer->GetReadableData(), offset); |
| 101 | } |
| 102 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 103 | uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 104 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 105 | ARM_PIPE_ASSERT(packetBuffer); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 106 | |
| 107 | return ReadUint8(packetBuffer->GetReadableData(), offset); |
| 108 | } |
| 109 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 110 | std::string GetProcessName() |
| 111 | { |
| 112 | std::ifstream comm("/proc/self/comm"); |
| 113 | std::string name; |
| 114 | getline(comm, name); |
| 115 | return name; |
| 116 | } |
| 117 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 118 | /// Creates a timeline packet header |
| 119 | /// |
| 120 | /// \params |
| 121 | /// packetFamiliy Timeline Packet Family |
| 122 | /// packetClass Timeline Packet Class |
| 123 | /// packetType Timeline Packet Type |
| 124 | /// streamId Stream identifier |
| 125 | /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 126 | /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 127 | /// |
| 128 | /// \returns |
| 129 | /// Pair of uint32_t containing word0 and word1 of the header |
| 130 | std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily, |
| 131 | uint32_t packetClass, |
| 132 | uint32_t packetType, |
| 133 | uint32_t streamId, |
| 134 | uint32_t sequenceNumbered, |
| 135 | uint32_t dataLength) |
| 136 | { |
| 137 | // Packet header word 0: |
| 138 | // 26:31 [6] packet_family: timeline Packet Family, value 0b000001 |
| 139 | // 19:25 [7] packet_class: packet class |
| 140 | // 16:18 [3] packet_type: packet type |
| 141 | // 8:15 [8] reserved: all zeros |
| 142 | // 0:7 [8] stream_id: stream identifier |
| 143 | uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) | |
| 144 | ((packetClass & 0x0000007F) << 19) | |
| 145 | ((packetType & 0x00000007) << 16) | |
| 146 | ((streamId & 0x00000007) << 0); |
| 147 | |
| 148 | // Packet header word 1: |
| 149 | // 25:31 [7] reserved: all zeros |
| 150 | // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number |
| 151 | // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted |
| 152 | uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | |
| 153 | ((dataLength & 0x00FFFFFF) << 0); |
| 154 | |
| 155 | return std::make_pair(packetHeaderWord0, packetHeaderWord1); |
| 156 | } |
| 157 | |
| 158 | /// Creates a packet header for the timeline messages: |
| 159 | /// * declareLabel |
| 160 | /// * declareEntity |
| 161 | /// * declareEventClass |
| 162 | /// * declareRelationship |
| 163 | /// * declareEvent |
| 164 | /// |
| 165 | /// \param |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 166 | /// dataLength The length of the message body in bytes |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 167 | /// |
| 168 | /// \returns |
| 169 | /// Pair of uint32_t containing word0 and word1 of the header |
| 170 | std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength) |
| 171 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 172 | return CreateTimelinePacketHeader(1, // Packet family |
| 173 | 0, // Packet class |
| 174 | 1, // Packet type |
| 175 | 0, // Stream id |
| 176 | 0, // Sequence number |
| 177 | dataLength); // Data length |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 180 | TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, |
| 181 | const std::string& label, |
| 182 | unsigned char* buffer, |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 183 | unsigned int remainingBufferSize, |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 184 | unsigned int& numberOfBytesWritten) |
| 185 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 186 | // Initialize the output value |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 187 | numberOfBytesWritten = 0; |
| 188 | |
| 189 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 190 | if (buffer == nullptr || remainingBufferSize == 0) |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 191 | { |
| 192 | return TimelinePacketStatus::BufferExhaustion; |
| 193 | } |
| 194 | |
| 195 | // Utils |
| 196 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 197 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 198 | |
| 199 | // Convert the label into a SWTrace string |
| 200 | std::vector<uint32_t> swTraceLabel; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 201 | bool result = arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(label, swTraceLabel); |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 202 | if (!result) |
| 203 | { |
| 204 | return TimelinePacketStatus::Error; |
| 205 | } |
| 206 | |
| 207 | // Calculate the size of the SWTrace string label (in bytes) |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 208 | unsigned int swTraceLabelSize = arm::pipe::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size; |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 209 | |
| 210 | // Calculate the length of the data (in bytes) |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 211 | unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id |
| 212 | uint64_t_size + // Profiling GUID |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 213 | swTraceLabelSize; // Label |
| 214 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 215 | // Check whether the timeline binary packet fits in the given buffer |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 216 | if (timelineLabelPacketDataLength > remainingBufferSize) |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 217 | { |
| 218 | return TimelinePacketStatus::BufferExhaustion; |
| 219 | } |
| 220 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 221 | // Initialize the offset for writing in the buffer |
| 222 | unsigned int offset = 0; |
| 223 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 224 | // Write decl_Id to the buffer |
| 225 | WriteUint32(buffer, offset, 0u); |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 226 | offset += uint32_t_size; |
| 227 | |
| 228 | // Write the timeline binary packet payload to the buffer |
| 229 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 230 | offset += uint64_t_size; |
| 231 | for (uint32_t swTraceLabelWord : swTraceLabel) |
| 232 | { |
| 233 | WriteUint32(buffer, offset, swTraceLabelWord); // Label |
| 234 | offset += uint32_t_size; |
| 235 | } |
| 236 | |
| 237 | // Update the number of bytes written |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 238 | numberOfBytesWritten = timelineLabelPacketDataLength; |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 239 | |
| 240 | return TimelinePacketStatus::Ok; |
| 241 | } |
| 242 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 243 | TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid, |
| 244 | unsigned char* buffer, |
| 245 | unsigned int remainingBufferSize, |
| 246 | unsigned int& numberOfBytesWritten) |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 247 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 248 | // Initialize the output value |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 249 | numberOfBytesWritten = 0; |
| 250 | |
| 251 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 252 | if (buffer == nullptr || remainingBufferSize == 0) |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 253 | { |
| 254 | return TimelinePacketStatus::BufferExhaustion; |
| 255 | } |
| 256 | |
| 257 | // Utils |
| 258 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 259 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 260 | |
| 261 | // Calculate the length of the data (in bytes) |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 262 | unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 263 | |
| 264 | // Check whether the timeline binary packet fits in the given buffer |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 265 | if (timelineEntityDataLength > remainingBufferSize) |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 266 | { |
| 267 | return TimelinePacketStatus::BufferExhaustion; |
| 268 | } |
| 269 | |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 270 | // Initialize the offset for writing in the buffer |
| 271 | unsigned int offset = 0; |
| 272 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 273 | // Write the decl_Id to the buffer |
| 274 | WriteUint32(buffer, offset, 1u); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 275 | offset += uint32_t_size; |
| 276 | |
| 277 | // Write the timeline binary packet payload to the buffer |
| 278 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 279 | |
| 280 | // Update the number of bytes written |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 281 | numberOfBytesWritten = timelineEntityDataLength; |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 282 | |
| 283 | return TimelinePacketStatus::Ok; |
| 284 | } |
| 285 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 286 | TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType, |
| 287 | uint64_t relationshipGuid, |
| 288 | uint64_t headGuid, |
| 289 | uint64_t tailGuid, |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 290 | uint64_t attributeGuid, |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 291 | unsigned char* buffer, |
| 292 | unsigned int remainingBufferSize, |
| 293 | unsigned int& numberOfBytesWritten) |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 294 | { |
| 295 | // Initialize the output value |
| 296 | numberOfBytesWritten = 0; |
| 297 | |
| 298 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 299 | if (buffer == nullptr || remainingBufferSize == 0) |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 300 | { |
| 301 | return TimelinePacketStatus::BufferExhaustion; |
| 302 | } |
| 303 | |
| 304 | // Utils |
| 305 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 306 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 307 | |
| 308 | // Calculate the length of the data (in bytes) |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 309 | unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 310 | uint64_t_size * 4; // Relationship GUID + Head GUID + |
| 311 | // tail GUID + attributeGuid |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 312 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 313 | // Check whether the timeline binary fits in the given buffer |
| 314 | if (timelineRelationshipDataLength > remainingBufferSize) |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 315 | { |
| 316 | return TimelinePacketStatus::BufferExhaustion; |
| 317 | } |
| 318 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 319 | // Initialize the offset for writing in the buffer |
| 320 | unsigned int offset = 0; |
| 321 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 322 | uint32_t relationshipTypeUint = 0; |
| 323 | |
| 324 | switch (relationshipType) |
| 325 | { |
| 326 | case ProfilingRelationshipType::RetentionLink: |
| 327 | relationshipTypeUint = 0; |
| 328 | break; |
| 329 | case ProfilingRelationshipType::ExecutionLink: |
| 330 | relationshipTypeUint = 1; |
| 331 | break; |
| 332 | case ProfilingRelationshipType::DataLink: |
| 333 | relationshipTypeUint = 2; |
| 334 | break; |
| 335 | case ProfilingRelationshipType::LabelLink: |
| 336 | relationshipTypeUint = 3; |
| 337 | break; |
| 338 | default: |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 339 | throw arm::pipe::InvalidArgumentException("Unknown relationship type given."); |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 342 | // Write the timeline binary payload to the buffer |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 343 | // decl_id of the timeline message |
| 344 | uint32_t declId = 3; |
| 345 | WriteUint32(buffer, offset, declId); // decl_id |
| 346 | offset += uint32_t_size; |
| 347 | WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type |
| 348 | offset += uint32_t_size; |
| 349 | WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship |
| 350 | offset += uint64_t_size; |
| 351 | WriteUint64(buffer, offset, headGuid); // head of relationship GUID |
| 352 | offset += uint64_t_size; |
| 353 | WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID |
Finn Williams | 0a336dc | 2020-05-11 15:39:58 +0100 | [diff] [blame] | 354 | offset += uint64_t_size; |
| 355 | WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID |
| 356 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 357 | |
| 358 | // Update the number of bytes written |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 359 | numberOfBytesWritten = timelineRelationshipDataLength; |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 360 | |
| 361 | return TimelinePacketStatus::Ok; |
| 362 | } |
| 363 | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 364 | TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 365 | unsigned int remainingBufferSize, |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 366 | unsigned int& numberOfBytesWritten) |
| 367 | { |
| 368 | // Initialize the output value |
| 369 | numberOfBytesWritten = 0; |
| 370 | |
| 371 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 372 | if (buffer == nullptr || remainingBufferSize == 0) |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 373 | { |
| 374 | return TimelinePacketStatus::BufferExhaustion; |
| 375 | } |
| 376 | |
| 377 | // Utils |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 378 | unsigned int uint8_t_size = sizeof(uint8_t); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 379 | unsigned int uint32_t_size = sizeof(uint32_t); |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 380 | unsigned int uint64_t_size = sizeof(uint64_t); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 381 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 382 | // The payload/data of the packet consists of swtrace event definitions encoded according |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 383 | // to the swtrace directory specification. The messages being the five defined below: |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 384 | // |
| 385 | // | decl_id | decl_name | ui_name | arg_types | arg_names | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 386 | // |-----------|---------------------|-----------------------|-------------|-------------------------------------| |
| 387 | // | 0 | declareLabel | declare label | ps | guid,value | |
| 388 | // | 1 | declareEntity | declare entity | p | guid | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 389 | // | 2 | declareEventClass | declare event class | pp | guid,nameGuid | |
| 390 | // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, | |
| 391 | // | | | | | headGuid,tailGuid,attributeGuid | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 392 | // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 393 | std::vector<std::vector<std::string>> timelineDirectoryMessages |
| 394 | { |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 395 | { "0", "declareLabel", "declare label", "ps", "guid,value" }, |
| 396 | { "1", "declareEntity", "declare entity", "p", "guid" }, |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 397 | { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" }, |
| 398 | { "3", "declareRelationship", "declare relationship", "Ipppp", |
| 399 | "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" }, |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 400 | { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" } |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 401 | }; |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 402 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 403 | // Build the message declarations |
| 404 | std::vector<uint32_t> swTraceBuffer; |
| 405 | for (const auto& directoryComponent : timelineDirectoryMessages) |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 406 | { |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 407 | // decl_id |
| 408 | uint32_t declId = 0; |
| 409 | try |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 410 | { |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 411 | declId = arm::pipe::numeric_cast<uint32_t>(std::stoul(directoryComponent[0])); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 412 | } |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 413 | catch (const std::exception&) |
| 414 | { |
| 415 | return TimelinePacketStatus::Error; |
| 416 | } |
| 417 | swTraceBuffer.push_back(declId); |
| 418 | |
| 419 | bool result = true; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 420 | result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceNameCharPolicy>( |
| 421 | directoryComponent[1], swTraceBuffer); // decl_name |
| 422 | result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> ( |
| 423 | directoryComponent[2], swTraceBuffer); // ui_name |
| 424 | result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceTypeCharPolicy>( |
| 425 | directoryComponent[3], swTraceBuffer); // arg_types |
| 426 | result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> ( |
| 427 | directoryComponent[4], swTraceBuffer); // arg_names |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 428 | if (!result) |
| 429 | { |
| 430 | return TimelinePacketStatus::Error; |
| 431 | } |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 434 | unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes) |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 435 | arm::pipe::numeric_cast<unsigned int>(swTraceBuffer.size()) * |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 436 | uint32_t_size; // Trace directory (5 messages) |
| 437 | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 438 | // Calculate the timeline directory binary packet size (in bytes) |
| 439 | unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words) |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 440 | dataLength; // Payload |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 441 | |
| 442 | // Check whether the timeline directory binary packet fits in the given buffer |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 443 | if (timelineDirectoryPacketSize > remainingBufferSize) |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 444 | { |
| 445 | return TimelinePacketStatus::BufferExhaustion; |
| 446 | } |
| 447 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 448 | // Create packet header |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 449 | auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, arm::pipe::numeric_cast<uint32_t>(dataLength)); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 450 | |
| 451 | // Initialize the offset for writing in the buffer |
| 452 | unsigned int offset = 0; |
| 453 | |
| 454 | // Write the timeline binary packet header to the buffer |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 455 | WriteUint32(buffer, offset, packetHeader.first); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 456 | offset += uint32_t_size; |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 457 | WriteUint32(buffer, offset, packetHeader.second); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 458 | offset += uint32_t_size; |
| 459 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 460 | // Write the stream header |
| 461 | uint8_t streamVersion = 4; |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 462 | uint8_t pointerBytes = arm::pipe::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t |
| 463 | uint8_t threadIdBytes = arm::pipe::numeric_cast<uint8_t>(ThreadIdSize); |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 464 | switch (threadIdBytes) |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 465 | { |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 466 | case 4: // Typically Windows and Android |
| 467 | case 8: // Typically Linux |
| 468 | break; // Valid values |
| 469 | default: |
| 470 | return TimelinePacketStatus::Error; // Invalid value |
| 471 | } |
| 472 | WriteUint8(buffer, offset, streamVersion); |
| 473 | offset += uint8_t_size; |
| 474 | WriteUint8(buffer, offset, pointerBytes); |
| 475 | offset += uint8_t_size; |
| 476 | WriteUint8(buffer, offset, threadIdBytes); |
| 477 | offset += uint8_t_size; |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 478 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 479 | // Write the SWTrace directory |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 480 | uint32_t numberOfDeclarations = arm::pipe::numeric_cast<uint32_t>(timelineDirectoryMessages.size()); |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 481 | WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations |
| 482 | offset += uint32_t_size; |
| 483 | for (uint32_t i : swTraceBuffer) |
| 484 | { |
| 485 | WriteUint32(buffer, offset, i); // Message declarations |
| 486 | offset += uint32_t_size; |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Update the number of bytes written |
| 490 | numberOfBytesWritten = timelineDirectoryPacketSize; |
| 491 | |
| 492 | return TimelinePacketStatus::Ok; |
| 493 | } |
| 494 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 495 | TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid, |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 496 | uint64_t nameGuid, |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 497 | unsigned char* buffer, |
| 498 | unsigned int remainingBufferSize, |
| 499 | unsigned int& numberOfBytesWritten) |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 500 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 501 | // Initialize the output value |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 502 | numberOfBytesWritten = 0; |
| 503 | |
| 504 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 505 | if (buffer == nullptr || remainingBufferSize == 0) |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 506 | { |
| 507 | return TimelinePacketStatus::BufferExhaustion; |
| 508 | } |
| 509 | |
| 510 | // Utils |
| 511 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 512 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 513 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 514 | // decl_id of the timeline message |
| 515 | uint32_t declId = 2; |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 516 | |
| 517 | // Calculate the length of the data (in bytes) |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 518 | unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 519 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 520 | // Check whether the timeline binary fits in the given buffer |
| 521 | if (dataSize > remainingBufferSize) |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 522 | { |
| 523 | return TimelinePacketStatus::BufferExhaustion; |
| 524 | } |
| 525 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 526 | // Initialize the offset for writing in the buffer |
| 527 | unsigned int offset = 0; |
| 528 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 529 | // Write the timeline binary payload to the buffer |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 530 | WriteUint32(buffer, offset, declId); // decl_id |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 531 | offset += uint32_t_size; |
| 532 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
Jim Flynn | 1892d21 | 2020-05-26 21:10:49 +0100 | [diff] [blame] | 533 | offset += uint64_t_size; |
| 534 | WriteUint64(buffer, offset, nameGuid); // Name GUID |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 535 | |
| 536 | // Update the number of bytes written |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 537 | numberOfBytesWritten = dataSize; |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 538 | |
| 539 | return TimelinePacketStatus::Ok; |
| 540 | } |
| 541 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 542 | TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 543 | int threadId, |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 544 | uint64_t profilingGuid, |
| 545 | unsigned char* buffer, |
| 546 | unsigned int remainingBufferSize, |
| 547 | unsigned int& numberOfBytesWritten) |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 548 | { |
| 549 | // Initialize the output value |
| 550 | numberOfBytesWritten = 0; |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 551 | // Check that the given buffer is valid |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 552 | if (buffer == nullptr || remainingBufferSize == 0) |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 553 | { |
| 554 | return TimelinePacketStatus::BufferExhaustion; |
| 555 | } |
| 556 | |
| 557 | // Utils |
| 558 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 559 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 560 | |
| 561 | // decl_id of the timeline message |
| 562 | uint32_t declId = 4; |
| 563 | |
| 564 | // Calculate the length of the data (in bytes) |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 565 | unsigned int timelineEventDataLength = uint32_t_size + // decl_id |
| 566 | uint64_t_size + // Timestamp |
Colm Donelan | 5bb3d8a | 2020-05-12 16:36:46 +0100 | [diff] [blame] | 567 | ThreadIdSize + // Thread id |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 568 | uint64_t_size; // Profiling GUID |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 569 | |
| 570 | // Check whether the timeline binary packet fits in the given buffer |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 571 | if (timelineEventDataLength > remainingBufferSize) |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 572 | { |
| 573 | return TimelinePacketStatus::BufferExhaustion; |
| 574 | } |
| 575 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 576 | // Initialize the offset for writing in the buffer |
| 577 | unsigned int offset = 0; |
| 578 | |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 579 | // Write the timeline binary payload to the buffer |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 580 | WriteUint32(buffer, offset, declId); // decl_id |
| 581 | offset += uint32_t_size; |
| 582 | WriteUint64(buffer, offset, timestamp); // Timestamp |
| 583 | offset += uint64_t_size; |
Colm Donelan | 5bb3d8a | 2020-05-12 16:36:46 +0100 | [diff] [blame] | 584 | WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id |
| 585 | offset += ThreadIdSize; |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 586 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 587 | offset += uint64_t_size; |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 588 | // Update the number of bytes written |
Keith Davis | 97da5e2 | 2020-03-05 16:25:28 +0000 | [diff] [blame] | 589 | numberOfBytesWritten = timelineEventDataLength; |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 590 | |
| 591 | return TimelinePacketStatus::Ok; |
| 592 | } |
| 593 | |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 594 | uint64_t GetTimestamp() |
| 595 | { |
| 596 | #if USE_CLOCK_MONOTONIC_RAW |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 597 | using clock = armnn::MonotonicClockRaw; |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 598 | #else |
| 599 | using clock = std::chrono::steady_clock; |
| 600 | #endif |
| 601 | |
| 602 | // Take a timestamp |
Finn Williams | d9ba1a7 | 2020-04-16 15:32:28 +0100 | [diff] [blame] | 603 | auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch()); |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 604 | |
Finn Williams | d9ba1a7 | 2020-04-16 15:32:28 +0100 | [diff] [blame] | 605 | return static_cast<uint64_t>(timestamp.count()); |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 606 | } |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 607 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 608 | arm::pipe::Packet ReceivePacket(const unsigned char* buffer, uint32_t length) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 609 | { |
| 610 | if (buffer == nullptr) |
| 611 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 612 | throw arm::pipe::ProfilingException("data buffer is nullptr"); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 613 | } |
| 614 | if (length < 8) |
| 615 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 616 | throw arm::pipe::ProfilingException("length of data buffer is less than 8"); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | uint32_t metadataIdentifier = 0; |
| 620 | std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier)); |
| 621 | |
| 622 | uint32_t dataLength = 0; |
| 623 | std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength)); |
| 624 | |
| 625 | std::unique_ptr<unsigned char[]> packetData; |
| 626 | if (dataLength > 0) |
| 627 | { |
| 628 | packetData = std::make_unique<unsigned char[]>(dataLength); |
| 629 | std::memcpy(packetData.get(), buffer + 8u, dataLength); |
| 630 | } |
| 631 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 632 | return arm::pipe::Packet(metadataIdentifier, dataLength, packetData); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 635 | } // namespace pipe |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 636 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 637 | } // namespace arm |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 638 | |
| 639 | namespace std |
| 640 | { |
| 641 | |
Jim Flynn | 1fdeb99 | 2020-07-09 07:28:37 +0100 | [diff] [blame] | 642 | bool operator==(const std::vector<uint8_t>& left, int right) |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 643 | { |
| 644 | return std::memcmp(left.data(), &right, left.size()) == 0; |
| 645 | } |
| 646 | |
| 647 | } // namespace std |