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