Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ProfilingUtils.hpp" |
| 7 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 8 | #include <armnn/Version.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 9 | #include <armnn/Conversion.hpp> |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 10 | |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 11 | #include <WallClockTimer.hpp> |
| 12 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 13 | #include <boost/assert.hpp> |
| 14 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 15 | #include <fstream> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 16 | #include <iostream> |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 17 | #include <limits> |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 18 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 19 | namespace armnn |
| 20 | { |
| 21 | |
| 22 | namespace profiling |
| 23 | { |
| 24 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 25 | namespace |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 26 | { |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 27 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 28 | void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0) |
| 29 | { |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 30 | // Check that it is possible to generate the next UID without causing an overflow |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 31 | switch (cores) |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 32 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 33 | case 0: |
| 34 | case 1: |
| 35 | // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of |
| 36 | // running multiple parallel workloads and will not provide multiple streams of data for each event) |
| 37 | if (uid == std::numeric_limits<uint16_t>::max()) |
| 38 | { |
| 39 | throw RuntimeException("Generating the next UID for profiling would result in an overflow"); |
| 40 | } |
| 41 | break; |
| 42 | default: // cores > 1 |
| 43 | // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum |
| 44 | // allowed value for a counter UID is consequently: uint16_t_max - cores + 1 |
| 45 | if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1) |
| 46 | { |
| 47 | throw RuntimeException("Generating the next UID for profiling would result in an overflow"); |
| 48 | } |
| 49 | break; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 50 | } |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 51 | } |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 52 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 53 | } // Anonymous namespace |
| 54 | |
| 55 | uint16_t GetNextUid(bool peekOnly) |
| 56 | { |
| 57 | // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value |
| 58 | static uint16_t uid = 1; |
| 59 | |
| 60 | // Check that it is possible to generate the next UID without causing an overflow (throws in case of error) |
| 61 | ThrowIfCantGenerateNextUid(uid); |
| 62 | |
| 63 | if (peekOnly) |
| 64 | { |
| 65 | // Peek only |
| 66 | return uid; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Get the next UID |
| 71 | return uid++; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::vector<uint16_t> GetNextCounterUids(uint16_t cores) |
| 76 | { |
| 77 | // The UID used for counters only. The first valid UID is 0 |
| 78 | static uint16_t counterUid = 0; |
| 79 | |
| 80 | // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error) |
| 81 | ThrowIfCantGenerateNextUid(counterUid, cores); |
| 82 | |
| 83 | // Get the next counter UIDs |
| 84 | size_t counterUidsSize = cores == 0 ? 1 : cores; |
| 85 | std::vector<uint16_t> counterUids(counterUidsSize, 0); |
| 86 | for (size_t i = 0; i < counterUidsSize; i++) |
| 87 | { |
| 88 | counterUids[i] = counterUid++; |
| 89 | } |
| 90 | return counterUids; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 93 | void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize) |
| 94 | { |
| 95 | BOOST_ASSERT(packetBuffer); |
| 96 | |
| 97 | WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize); |
| 98 | } |
| 99 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 100 | uint32_t ConstructHeader(uint32_t packetFamily, |
| 101 | uint32_t packetId) |
| 102 | { |
| 103 | return ((packetFamily & 0x3F) << 26)| |
| 104 | ((packetId & 0x3FF) << 16); |
| 105 | } |
| 106 | |
| 107 | uint32_t ConstructHeader(uint32_t packetFamily, |
| 108 | uint32_t packetClass, |
| 109 | uint32_t packetType) |
| 110 | { |
| 111 | return ((packetFamily & 0x3F) << 26)| |
| 112 | ((packetClass & 0x3FF) << 19)| |
| 113 | ((packetType & 0x3FFF) << 16); |
| 114 | } |
| 115 | |
| 116 | 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] | 117 | { |
| 118 | BOOST_ASSERT(packetBuffer); |
| 119 | |
| 120 | WriteUint64(packetBuffer->GetWritableData(), offset, value); |
| 121 | } |
| 122 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 123 | void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 124 | { |
| 125 | BOOST_ASSERT(packetBuffer); |
| 126 | |
| 127 | WriteUint32(packetBuffer->GetWritableData(), offset, value); |
| 128 | } |
| 129 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 130 | void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 131 | { |
| 132 | BOOST_ASSERT(packetBuffer); |
| 133 | |
| 134 | WriteUint16(packetBuffer->GetWritableData(), offset, value); |
| 135 | } |
| 136 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 137 | void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize) |
| 138 | { |
| 139 | BOOST_ASSERT(buffer); |
| 140 | BOOST_ASSERT(value); |
| 141 | |
| 142 | for (unsigned int i = 0; i < valueSize; i++, offset++) |
| 143 | { |
| 144 | buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i); |
| 145 | } |
| 146 | } |
| 147 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 148 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value) |
| 149 | { |
| 150 | BOOST_ASSERT(buffer); |
| 151 | |
| 152 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
| 153 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 154 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 155 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 156 | buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF); |
| 157 | buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF); |
| 158 | buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF); |
| 159 | buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF); |
| 160 | } |
| 161 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 162 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value) |
| 163 | { |
| 164 | BOOST_ASSERT(buffer); |
| 165 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 166 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 167 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 168 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 169 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 170 | } |
| 171 | |
| 172 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value) |
| 173 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 174 | BOOST_ASSERT(buffer); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 175 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 176 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 177 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 178 | } |
| 179 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 180 | void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]) |
| 181 | { |
| 182 | BOOST_ASSERT(packetBuffer); |
| 183 | |
| 184 | ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue); |
| 185 | } |
| 186 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 187 | uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 188 | { |
| 189 | BOOST_ASSERT(packetBuffer); |
| 190 | |
| 191 | return ReadUint64(packetBuffer->GetReadableData(), offset); |
| 192 | } |
| 193 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 194 | uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 195 | { |
| 196 | BOOST_ASSERT(packetBuffer); |
| 197 | |
| 198 | return ReadUint32(packetBuffer->GetReadableData(), offset); |
| 199 | } |
| 200 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 201 | uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 202 | { |
| 203 | BOOST_ASSERT(packetBuffer); |
| 204 | |
| 205 | return ReadUint16(packetBuffer->GetReadableData(), offset); |
| 206 | } |
| 207 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 208 | uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset) |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 209 | { |
| 210 | BOOST_ASSERT(packetBuffer); |
| 211 | |
| 212 | return ReadUint8(packetBuffer->GetReadableData(), offset); |
| 213 | } |
| 214 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 215 | void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]) |
| 216 | { |
| 217 | BOOST_ASSERT(buffer); |
| 218 | BOOST_ASSERT(outValue); |
| 219 | |
| 220 | for (unsigned int i = 0; i < valueSize; i++, offset++) |
| 221 | { |
| 222 | outValue[i] = static_cast<uint8_t>(buffer[offset]); |
| 223 | } |
| 224 | } |
| 225 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 226 | uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset) |
| 227 | { |
| 228 | BOOST_ASSERT(buffer); |
| 229 | |
| 230 | uint64_t value = 0; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 231 | value = static_cast<uint64_t>(buffer[offset]); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 232 | value |= static_cast<uint64_t>(buffer[offset + 1]) << 8; |
| 233 | value |= static_cast<uint64_t>(buffer[offset + 2]) << 16; |
| 234 | value |= static_cast<uint64_t>(buffer[offset + 3]) << 24; |
| 235 | value |= static_cast<uint64_t>(buffer[offset + 4]) << 32; |
| 236 | value |= static_cast<uint64_t>(buffer[offset + 5]) << 40; |
| 237 | value |= static_cast<uint64_t>(buffer[offset + 6]) << 48; |
| 238 | value |= static_cast<uint64_t>(buffer[offset + 7]) << 56; |
| 239 | |
| 240 | return value; |
| 241 | } |
| 242 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 243 | uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset) |
| 244 | { |
| 245 | BOOST_ASSERT(buffer); |
| 246 | |
| 247 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 248 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 249 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 250 | value |= static_cast<uint32_t>(buffer[offset + 2]) << 16; |
| 251 | value |= static_cast<uint32_t>(buffer[offset + 3]) << 24; |
| 252 | return value; |
| 253 | } |
| 254 | |
| 255 | uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset) |
| 256 | { |
| 257 | BOOST_ASSERT(buffer); |
| 258 | |
| 259 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 260 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 261 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 262 | return static_cast<uint16_t>(value); |
| 263 | } |
| 264 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 265 | uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset) |
| 266 | { |
| 267 | BOOST_ASSERT(buffer); |
| 268 | |
| 269 | return buffer[offset]; |
| 270 | } |
| 271 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 272 | std::string GetSoftwareInfo() |
| 273 | { |
| 274 | return std::string("ArmNN"); |
| 275 | } |
| 276 | |
| 277 | std::string GetHardwareVersion() |
| 278 | { |
| 279 | return std::string(); |
| 280 | } |
| 281 | |
| 282 | std::string GetSoftwareVersion() |
| 283 | { |
| 284 | std::string armnnVersion(ARMNN_VERSION); |
| 285 | std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2); |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | std::string GetProcessName() |
| 290 | { |
| 291 | std::ifstream comm("/proc/self/comm"); |
| 292 | std::string name; |
| 293 | getline(comm, name); |
| 294 | return name; |
| 295 | } |
| 296 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 297 | /// Creates a timeline packet header |
| 298 | /// |
| 299 | /// \params |
| 300 | /// packetFamiliy Timeline Packet Family |
| 301 | /// packetClass Timeline Packet Class |
| 302 | /// packetType Timeline Packet Type |
| 303 | /// streamId Stream identifier |
| 304 | /// 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] | 305 | /// 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] | 306 | /// |
| 307 | /// \returns |
| 308 | /// Pair of uint32_t containing word0 and word1 of the header |
| 309 | std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily, |
| 310 | uint32_t packetClass, |
| 311 | uint32_t packetType, |
| 312 | uint32_t streamId, |
| 313 | uint32_t sequenceNumbered, |
| 314 | uint32_t dataLength) |
| 315 | { |
| 316 | // Packet header word 0: |
| 317 | // 26:31 [6] packet_family: timeline Packet Family, value 0b000001 |
| 318 | // 19:25 [7] packet_class: packet class |
| 319 | // 16:18 [3] packet_type: packet type |
| 320 | // 8:15 [8] reserved: all zeros |
| 321 | // 0:7 [8] stream_id: stream identifier |
| 322 | uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) | |
| 323 | ((packetClass & 0x0000007F) << 19) | |
| 324 | ((packetType & 0x00000007) << 16) | |
| 325 | ((streamId & 0x00000007) << 0); |
| 326 | |
| 327 | // Packet header word 1: |
| 328 | // 25:31 [7] reserved: all zeros |
| 329 | // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number |
| 330 | // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted |
| 331 | uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) | |
| 332 | ((dataLength & 0x00FFFFFF) << 0); |
| 333 | |
| 334 | return std::make_pair(packetHeaderWord0, packetHeaderWord1); |
| 335 | } |
| 336 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 337 | // Calculate the actual length an SwString will be including the terminating null character |
| 338 | // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding |
| 339 | // the size to allow the offset to be correctly updated when decoding a binary packet. |
| 340 | uint32_t CalculateSizeOfPaddedSwString(const std::string& str) |
| 341 | { |
| 342 | std::vector<uint32_t> swTraceString; |
| 343 | StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString); |
| 344 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 345 | uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size; |
| 346 | return size; |
| 347 | } |
| 348 | |
| 349 | // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 350 | SwTraceMessage ReadSwTraceMessage(const IPacketBufferPtr& packetBuffer, unsigned int& offset) |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 351 | { |
| 352 | BOOST_ASSERT(packetBuffer); |
| 353 | |
| 354 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 355 | |
| 356 | SwTraceMessage swTraceMessage; |
| 357 | |
| 358 | // Read the decl_id |
| 359 | uint32_t readDeclId = ReadUint32(packetBuffer, offset); |
| 360 | swTraceMessage.id = readDeclId; |
| 361 | |
| 362 | // SWTrace "namestring" format |
| 363 | // length of the string (first 4 bytes) + string + null terminator |
| 364 | |
| 365 | // Check the decl_name |
| 366 | offset += uint32_t_size; |
| 367 | uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset); |
| 368 | |
| 369 | offset += uint32_t_size; |
| 370 | std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1); |
| 371 | std::memcpy(swTraceStringBuffer.data(), |
| 372 | packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size()); |
| 373 | |
| 374 | swTraceMessage.name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name |
| 375 | |
| 376 | // Check the ui_name |
| 377 | offset += CalculateSizeOfPaddedSwString(swTraceMessage.name); |
| 378 | uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset); |
| 379 | |
| 380 | offset += uint32_t_size; |
| 381 | swTraceStringBuffer.resize(swTraceUINameLength - 1); |
| 382 | std::memcpy(swTraceStringBuffer.data(), |
| 383 | packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size()); |
| 384 | |
| 385 | swTraceMessage.uiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name |
| 386 | |
| 387 | // Check arg_types |
| 388 | offset += CalculateSizeOfPaddedSwString(swTraceMessage.uiName); |
| 389 | uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset); |
| 390 | |
| 391 | offset += uint32_t_size; |
| 392 | swTraceStringBuffer.resize(swTraceArgTypesLength - 1); |
| 393 | std::memcpy(swTraceStringBuffer.data(), |
| 394 | packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size()); |
| 395 | |
| 396 | swTraceMessage.argTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types |
| 397 | |
| 398 | std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); |
| 399 | |
| 400 | // Check arg_names |
| 401 | offset += CalculateSizeOfPaddedSwString(swTraceString); |
| 402 | uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset); |
| 403 | |
| 404 | offset += uint32_t_size; |
| 405 | swTraceStringBuffer.resize(swTraceArgNamesLength - 1); |
| 406 | std::memcpy(swTraceStringBuffer.data(), |
| 407 | packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size()); |
| 408 | |
| 409 | swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); |
| 410 | std::stringstream stringStream(swTraceString); |
| 411 | std::string argName; |
| 412 | while (std::getline(stringStream, argName, ',')) |
| 413 | { |
| 414 | swTraceMessage.argNames.push_back(argName); |
| 415 | } |
| 416 | |
| 417 | offset += CalculateSizeOfPaddedSwString(swTraceString); |
| 418 | |
| 419 | return swTraceMessage; |
| 420 | } |
| 421 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 422 | /// Creates a packet header for the timeline messages: |
| 423 | /// * declareLabel |
| 424 | /// * declareEntity |
| 425 | /// * declareEventClass |
| 426 | /// * declareRelationship |
| 427 | /// * declareEvent |
| 428 | /// |
| 429 | /// \param |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 430 | /// dataLength The length of the message body in bytes |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 431 | /// |
| 432 | /// \returns |
| 433 | /// Pair of uint32_t containing word0 and word1 of the header |
| 434 | std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength) |
| 435 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 436 | return CreateTimelinePacketHeader(1, // Packet family |
| 437 | 0, // Packet class |
| 438 | 1, // Packet type |
| 439 | 0, // Stream id |
| 440 | 0, // Sequence number |
| 441 | dataLength); // Data length |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 444 | TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, |
| 445 | const std::string& label, |
| 446 | unsigned char* buffer, |
| 447 | unsigned int bufferSize, |
| 448 | unsigned int& numberOfBytesWritten) |
| 449 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 450 | // Initialize the output value |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 451 | numberOfBytesWritten = 0; |
| 452 | |
| 453 | // Check that the given buffer is valid |
| 454 | if (buffer == nullptr || bufferSize == 0) |
| 455 | { |
| 456 | return TimelinePacketStatus::BufferExhaustion; |
| 457 | } |
| 458 | |
| 459 | // Utils |
| 460 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 461 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 462 | |
| 463 | // Convert the label into a SWTrace string |
| 464 | std::vector<uint32_t> swTraceLabel; |
| 465 | bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel); |
| 466 | if (!result) |
| 467 | { |
| 468 | return TimelinePacketStatus::Error; |
| 469 | } |
| 470 | |
| 471 | // Calculate the size of the SWTrace string label (in bytes) |
| 472 | unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size; |
| 473 | |
| 474 | // Calculate the length of the data (in bytes) |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 475 | unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id |
| 476 | uint64_t_size + // Profiling GUID |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 477 | swTraceLabelSize; // Label |
| 478 | |
| 479 | // Calculate the timeline binary packet size (in bytes) |
| 480 | unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words) |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 481 | timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 482 | |
| 483 | // Check whether the timeline binary packet fits in the given buffer |
| 484 | if (timelineLabelPacketSize > bufferSize) |
| 485 | { |
| 486 | return TimelinePacketStatus::BufferExhaustion; |
| 487 | } |
| 488 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 489 | // Create packet header |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 490 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineLabelPacketDataLength); |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 491 | |
| 492 | // Initialize the offset for writing in the buffer |
| 493 | unsigned int offset = 0; |
| 494 | |
| 495 | // Write the timeline binary packet header to the buffer |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 496 | WriteUint32(buffer, offset, packetHeader.first); |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 497 | offset += uint32_t_size; |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 498 | WriteUint32(buffer, offset, packetHeader.second); |
| 499 | offset += uint32_t_size; |
| 500 | |
| 501 | // Write decl_Id to the buffer |
| 502 | WriteUint32(buffer, offset, 0u); |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 503 | offset += uint32_t_size; |
| 504 | |
| 505 | // Write the timeline binary packet payload to the buffer |
| 506 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 507 | offset += uint64_t_size; |
| 508 | for (uint32_t swTraceLabelWord : swTraceLabel) |
| 509 | { |
| 510 | WriteUint32(buffer, offset, swTraceLabelWord); // Label |
| 511 | offset += uint32_t_size; |
| 512 | } |
| 513 | |
| 514 | // Update the number of bytes written |
| 515 | numberOfBytesWritten = timelineLabelPacketSize; |
| 516 | |
| 517 | return TimelinePacketStatus::Ok; |
| 518 | } |
| 519 | |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 520 | TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, |
| 521 | unsigned char* buffer, |
| 522 | unsigned int bufferSize, |
| 523 | unsigned int& numberOfBytesWritten) |
| 524 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 525 | // Initialize the output value |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 526 | numberOfBytesWritten = 0; |
| 527 | |
| 528 | // Check that the given buffer is valid |
| 529 | if (buffer == nullptr || bufferSize == 0) |
| 530 | { |
| 531 | return TimelinePacketStatus::BufferExhaustion; |
| 532 | } |
| 533 | |
| 534 | // Utils |
| 535 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 536 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 537 | |
| 538 | // Calculate the length of the data (in bytes) |
| 539 | unsigned int timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID |
| 540 | |
| 541 | |
| 542 | // Calculate the timeline binary packet size (in bytes) |
| 543 | unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words) |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 544 | uint32_t_size + // decl_Id |
| 545 | timelineEntityPacketDataLength; // Profiling GUID |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 546 | |
| 547 | // Check whether the timeline binary packet fits in the given buffer |
| 548 | if (timelineEntityPacketSize > bufferSize) |
| 549 | { |
| 550 | return TimelinePacketStatus::BufferExhaustion; |
| 551 | } |
| 552 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 553 | // Create packet header |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 554 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEntityPacketDataLength); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 555 | |
| 556 | // Initialize the offset for writing in the buffer |
| 557 | unsigned int offset = 0; |
| 558 | |
| 559 | // Write the timeline binary packet header to the buffer |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 560 | WriteUint32(buffer, offset, packetHeader.first); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 561 | offset += uint32_t_size; |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 562 | WriteUint32(buffer, offset, packetHeader.second); |
| 563 | offset += uint32_t_size; |
| 564 | |
| 565 | // Write the decl_Id to the buffer |
| 566 | WriteUint32(buffer, offset, 1u); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 567 | offset += uint32_t_size; |
| 568 | |
| 569 | // Write the timeline binary packet payload to the buffer |
| 570 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 571 | |
| 572 | // Update the number of bytes written |
| 573 | numberOfBytesWritten = timelineEntityPacketSize; |
| 574 | |
| 575 | return TimelinePacketStatus::Ok; |
| 576 | } |
| 577 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 578 | TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 579 | uint64_t relationshipGuid, |
| 580 | uint64_t headGuid, |
| 581 | uint64_t tailGuid, |
| 582 | unsigned char* buffer, |
| 583 | unsigned int bufferSize, |
| 584 | unsigned int& numberOfBytesWritten) |
| 585 | { |
| 586 | // Initialize the output value |
| 587 | numberOfBytesWritten = 0; |
| 588 | |
| 589 | // Check that the given buffer is valid |
| 590 | if (buffer == nullptr || bufferSize == 0) |
| 591 | { |
| 592 | return TimelinePacketStatus::BufferExhaustion; |
| 593 | } |
| 594 | |
| 595 | // Utils |
| 596 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 597 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 598 | |
| 599 | // Calculate the length of the data (in bytes) |
| 600 | unsigned int timelineRelationshipPacketDataLength = uint32_t_size * 2 + // decl_id + Relationship Type |
| 601 | uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID |
| 602 | |
| 603 | // Calculate the timeline binary packet size (in bytes) |
| 604 | unsigned int timelineRelationshipPacketSize = 2 * uint32_t_size + // Header (2 words) |
| 605 | timelineRelationshipPacketDataLength; |
| 606 | |
| 607 | // Check whether the timeline binary packet fits in the given buffer |
| 608 | if (timelineRelationshipPacketSize > bufferSize) |
| 609 | { |
| 610 | return TimelinePacketStatus::BufferExhaustion; |
| 611 | } |
| 612 | |
| 613 | // Create packet header |
| 614 | uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineRelationshipPacketDataLength); |
| 615 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength); |
| 616 | |
| 617 | // Initialize the offset for writing in the buffer |
| 618 | unsigned int offset = 0; |
| 619 | |
| 620 | // Write the timeline binary packet header to the buffer |
| 621 | WriteUint32(buffer, offset, packetHeader.first); |
| 622 | offset += uint32_t_size; |
| 623 | WriteUint32(buffer, offset, packetHeader.second); |
| 624 | offset += uint32_t_size; |
| 625 | |
| 626 | uint32_t relationshipTypeUint = 0; |
| 627 | |
| 628 | switch (relationshipType) |
| 629 | { |
| 630 | case ProfilingRelationshipType::RetentionLink: |
| 631 | relationshipTypeUint = 0; |
| 632 | break; |
| 633 | case ProfilingRelationshipType::ExecutionLink: |
| 634 | relationshipTypeUint = 1; |
| 635 | break; |
| 636 | case ProfilingRelationshipType::DataLink: |
| 637 | relationshipTypeUint = 2; |
| 638 | break; |
| 639 | case ProfilingRelationshipType::LabelLink: |
| 640 | relationshipTypeUint = 3; |
| 641 | break; |
| 642 | default: |
| 643 | throw InvalidArgumentException("Unknown relationship type given."); |
| 644 | } |
| 645 | |
| 646 | // Write the timeline binary packet payload to the buffer |
| 647 | // decl_id of the timeline message |
| 648 | uint32_t declId = 3; |
| 649 | WriteUint32(buffer, offset, declId); // decl_id |
| 650 | offset += uint32_t_size; |
| 651 | WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type |
| 652 | offset += uint32_t_size; |
| 653 | WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship |
| 654 | offset += uint64_t_size; |
| 655 | WriteUint64(buffer, offset, headGuid); // head of relationship GUID |
| 656 | offset += uint64_t_size; |
| 657 | WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID |
| 658 | |
| 659 | // Update the number of bytes written |
| 660 | numberOfBytesWritten = timelineRelationshipPacketSize; |
| 661 | |
| 662 | return TimelinePacketStatus::Ok; |
| 663 | } |
| 664 | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 665 | TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, |
| 666 | unsigned int bufferSize, |
| 667 | unsigned int& numberOfBytesWritten) |
| 668 | { |
| 669 | // Initialize the output value |
| 670 | numberOfBytesWritten = 0; |
| 671 | |
| 672 | // Check that the given buffer is valid |
| 673 | if (buffer == nullptr || bufferSize == 0) |
| 674 | { |
| 675 | return TimelinePacketStatus::BufferExhaustion; |
| 676 | } |
| 677 | |
| 678 | // Utils |
| 679 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 680 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 681 | // 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] | 682 | // to the swtrace directory specification. The messages being the five defined below: |
| 683 | // | decl_id | decl_name | ui_name | arg_types | arg_names | |
| 684 | // |-----------|---------------------|-----------------------|-------------|-------------------------------------| |
| 685 | // | 0 | declareLabel | declare label | ps | guid,value | |
| 686 | // | 1 | declareEntity | declare entity | p | guid | |
| 687 | // | 2 | declareEventClass | declare event class | p | guid | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 688 | // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, | |
| 689 | // | | | | | headGuid,tailGuid | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 690 | // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid | |
| 691 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 692 | std::vector<std::vector<std::string>> timelineDirectoryMessages |
| 693 | { |
| 694 | {"declareLabel", "declare label", "ps", "guid,value"}, |
| 695 | {"declareEntity", "declare entity", "p", "guid"}, |
| 696 | {"declareEventClass", "declare event class", "p", "guid"}, |
| 697 | {"declareRelationship", "declare relationship", "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"}, |
| 698 | {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"} |
| 699 | }; |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 700 | |
| 701 | unsigned int messagesDataLength = 0u; |
| 702 | std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages; |
| 703 | |
| 704 | for (const auto& timelineDirectoryMessage : timelineDirectoryMessages) |
| 705 | { |
| 706 | messagesDataLength += uint32_t_size; // decl_id |
| 707 | |
| 708 | std::vector<std::vector<uint32_t>> swTraceStringsVector; |
| 709 | for (const auto& label : timelineDirectoryMessage) |
| 710 | { |
| 711 | std::vector<uint32_t> swTraceString; |
| 712 | bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString); |
| 713 | if (!result) |
| 714 | { |
| 715 | return TimelinePacketStatus::Error; |
| 716 | } |
| 717 | |
| 718 | messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size; |
| 719 | swTraceStringsVector.push_back(swTraceString); |
| 720 | } |
| 721 | swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector); |
| 722 | } |
| 723 | |
| 724 | // Calculate the timeline directory binary packet size (in bytes) |
| 725 | unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words) |
| 726 | messagesDataLength; // 5 messages length |
| 727 | |
| 728 | // Check whether the timeline directory binary packet fits in the given buffer |
| 729 | if (timelineDirectoryPacketSize > bufferSize) |
| 730 | { |
| 731 | return TimelinePacketStatus::BufferExhaustion; |
| 732 | } |
| 733 | |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 734 | // Create packet header |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 735 | uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength); |
| 736 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, dataLength); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 737 | |
| 738 | // Initialize the offset for writing in the buffer |
| 739 | unsigned int offset = 0; |
| 740 | |
| 741 | // Write the timeline binary packet header to the buffer |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 742 | WriteUint32(buffer, offset, packetHeader.first); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 743 | offset += uint32_t_size; |
Jan Eilers | b884ea4 | 2019-10-16 09:54:15 +0100 | [diff] [blame] | 744 | WriteUint32(buffer, offset, packetHeader.second); |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 745 | offset += uint32_t_size; |
| 746 | |
| 747 | for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i) |
| 748 | { |
| 749 | // Write the timeline binary packet payload to the buffer |
| 750 | WriteUint32(buffer, offset, i); // decl_id |
| 751 | offset += uint32_t_size; |
| 752 | |
| 753 | for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i]) |
| 754 | { |
| 755 | for (uint32_t swTraceDeclStringWord : swTraceString) |
| 756 | { |
| 757 | WriteUint32(buffer, offset, swTraceDeclStringWord); |
| 758 | offset += uint32_t_size; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // Update the number of bytes written |
| 764 | numberOfBytesWritten = timelineDirectoryPacketSize; |
| 765 | |
| 766 | return TimelinePacketStatus::Ok; |
| 767 | } |
| 768 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 769 | TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, |
| 770 | unsigned char* buffer, |
| 771 | unsigned int bufferSize, |
| 772 | unsigned int& numberOfBytesWritten) |
| 773 | { |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 774 | // Initialize the output value |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 775 | numberOfBytesWritten = 0; |
| 776 | |
| 777 | // Check that the given buffer is valid |
| 778 | if (buffer == nullptr || bufferSize == 0) |
| 779 | { |
| 780 | return TimelinePacketStatus::BufferExhaustion; |
| 781 | } |
| 782 | |
| 783 | // Utils |
| 784 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 785 | unsigned int uint64_t_size = sizeof(uint64_t); |
| 786 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 787 | // decl_id of the timeline message |
| 788 | uint32_t declId = 2; |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 789 | |
| 790 | // Calculate the length of the data (in bytes) |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 791 | unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 792 | |
| 793 | // Calculate the timeline binary packet size (in bytes) |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 794 | unsigned int packetSize = 2 * uint32_t_size + // Header (2 words) |
| 795 | packetBodySize; // Body |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 796 | |
| 797 | // Check whether the timeline binary packet fits in the given buffer |
| 798 | if (packetSize > bufferSize) |
| 799 | { |
| 800 | return TimelinePacketStatus::BufferExhaustion; |
| 801 | } |
| 802 | |
| 803 | // Create packet header |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 804 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(packetBodySize); |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 805 | |
| 806 | // Initialize the offset for writing in the buffer |
| 807 | unsigned int offset = 0; |
| 808 | |
| 809 | // Write the timeline binary packet header to the buffer |
| 810 | WriteUint32(buffer, offset, packetHeader.first); |
| 811 | offset += uint32_t_size; |
| 812 | WriteUint32(buffer, offset, packetHeader.second); |
| 813 | offset += uint32_t_size; |
| 814 | |
| 815 | // Write the timeline binary packet payload to the buffer |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 816 | WriteUint32(buffer, offset, declId); // decl_id |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 817 | offset += uint32_t_size; |
| 818 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 819 | |
| 820 | // Update the number of bytes written |
| 821 | numberOfBytesWritten = packetSize; |
| 822 | |
| 823 | return TimelinePacketStatus::Ok; |
| 824 | } |
| 825 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 826 | TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp, |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 827 | std::thread::id threadId, |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 828 | uint64_t profilingGuid, |
| 829 | unsigned char* buffer, |
| 830 | unsigned int bufferSize, |
| 831 | unsigned int& numberOfBytesWritten) |
| 832 | { |
| 833 | // Initialize the output value |
| 834 | numberOfBytesWritten = 0; |
| 835 | |
| 836 | // Check that the given buffer is valid |
| 837 | if (buffer == nullptr || bufferSize == 0) |
| 838 | { |
| 839 | return TimelinePacketStatus::BufferExhaustion; |
| 840 | } |
| 841 | |
| 842 | // Utils |
| 843 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 844 | unsigned int uint64_t_size = sizeof(uint64_t); |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 845 | unsigned int threadId_size = sizeof(std::thread::id); |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 846 | |
| 847 | // decl_id of the timeline message |
| 848 | uint32_t declId = 4; |
| 849 | |
| 850 | // Calculate the length of the data (in bytes) |
| 851 | unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id |
| 852 | uint64_t_size + // Timestamp |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 853 | threadId_size + // Thread id |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 854 | uint64_t_size; // Profiling GUID |
| 855 | |
| 856 | // Calculate the timeline binary packet size (in bytes) |
| 857 | unsigned int timelineEventPacketSize = 2 * uint32_t_size + // Header (2 words) |
| 858 | timelineEventPacketDataLength; // Timestamp + thread id + profiling GUID |
| 859 | |
| 860 | // Check whether the timeline binary packet fits in the given buffer |
| 861 | if (timelineEventPacketSize > bufferSize) |
| 862 | { |
| 863 | return TimelinePacketStatus::BufferExhaustion; |
| 864 | } |
| 865 | |
| 866 | // Create packet header |
| 867 | std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEventPacketDataLength); |
| 868 | |
| 869 | // Initialize the offset for writing in the buffer |
| 870 | unsigned int offset = 0; |
| 871 | |
| 872 | // Write the timeline binary packet header to the buffer |
| 873 | WriteUint32(buffer, offset, packetHeader.first); |
| 874 | offset += uint32_t_size; |
| 875 | WriteUint32(buffer, offset, packetHeader.second); |
| 876 | offset += uint32_t_size; |
| 877 | |
| 878 | // Write the timeline binary packet payload to the buffer |
| 879 | WriteUint32(buffer, offset, declId); // decl_id |
| 880 | offset += uint32_t_size; |
| 881 | WriteUint64(buffer, offset, timestamp); // Timestamp |
| 882 | offset += uint64_t_size; |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 883 | WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id |
| 884 | offset += threadId_size; |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 885 | WriteUint64(buffer, offset, profilingGuid); // Profiling GUID |
| 886 | offset += uint64_t_size; |
| 887 | |
| 888 | // Update the number of bytes written |
| 889 | numberOfBytesWritten = timelineEventPacketSize; |
| 890 | |
| 891 | return TimelinePacketStatus::Ok; |
| 892 | } |
| 893 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 894 | std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth) |
| 895 | { |
| 896 | std::stringstream outputStream, centrePadding; |
| 897 | int padding = spacingWidth - static_cast<int>(stringToPass.size()); |
| 898 | |
| 899 | for (int i = 0; i < padding / 2; ++i) |
| 900 | { |
| 901 | centrePadding << " "; |
| 902 | } |
| 903 | |
| 904 | outputStream << centrePadding.str() << stringToPass << centrePadding.str(); |
| 905 | |
| 906 | if (padding > 0 && padding %2 != 0) |
| 907 | { |
| 908 | outputStream << " "; |
| 909 | } |
| 910 | |
| 911 | return outputStream.str(); |
| 912 | } |
| 913 | |
| 914 | void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair) |
| 915 | { |
| 916 | std::string body; |
| 917 | |
| 918 | body.append(CentreAlignFormatting(devicePair.second->m_Name, 20)); |
| 919 | body.append(" | "); |
| 920 | body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13)); |
| 921 | body.append(" | "); |
| 922 | body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10)); |
| 923 | body.append("\n"); |
| 924 | |
| 925 | std::cout << std::string(body.size(), '-') << "\n"; |
| 926 | std::cout<< body; |
| 927 | } |
| 928 | |
| 929 | void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair) |
| 930 | { |
| 931 | std::string body; |
| 932 | |
| 933 | body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20)); |
| 934 | body.append(" | "); |
| 935 | body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13)); |
| 936 | body.append(" | "); |
| 937 | body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10)); |
| 938 | body.append("\n"); |
| 939 | |
| 940 | std::cout << std::string(body.size(), '-') << "\n"; |
| 941 | |
| 942 | std::cout<< body; |
| 943 | } |
| 944 | |
| 945 | void PrintCounterDetails(std::shared_ptr<Counter>& counter) |
| 946 | { |
| 947 | std::string body; |
| 948 | |
| 949 | body.append(CentreAlignFormatting(counter->m_Name, 20)); |
| 950 | body.append(" | "); |
| 951 | body.append(CentreAlignFormatting(counter->m_Description, 50)); |
| 952 | body.append(" | "); |
| 953 | body.append(CentreAlignFormatting(counter->m_Units, 14)); |
| 954 | body.append(" | "); |
| 955 | body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6)); |
| 956 | body.append(" | "); |
| 957 | body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10)); |
| 958 | body.append(" | "); |
| 959 | body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8)); |
| 960 | body.append(" | "); |
| 961 | body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14)); |
| 962 | body.append(" | "); |
| 963 | body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20)); |
| 964 | body.append(" | "); |
| 965 | body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16)); |
| 966 | body.append(" | "); |
| 967 | body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14)); |
| 968 | |
| 969 | body.append("\n"); |
| 970 | |
| 971 | std::cout << std::string(body.size(), '-') << "\n"; |
| 972 | |
| 973 | std::cout << body; |
| 974 | } |
| 975 | |
| 976 | void PrintCategoryDetails(const std::unique_ptr<Category>& category, |
| 977 | std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap) |
| 978 | { |
| 979 | std::string categoryBody; |
| 980 | std::string categoryHeader; |
| 981 | |
| 982 | categoryHeader.append(CentreAlignFormatting("Name", 20)); |
| 983 | categoryHeader.append(" | "); |
| 984 | categoryHeader.append(CentreAlignFormatting("Device", 12)); |
| 985 | categoryHeader.append(" | "); |
| 986 | categoryHeader.append(CentreAlignFormatting("Counter set UID:", 16)); |
| 987 | categoryHeader.append(" | "); |
| 988 | categoryHeader.append(CentreAlignFormatting("Event Count", 14)); |
| 989 | categoryHeader.append("\n"); |
| 990 | |
| 991 | categoryBody.append(CentreAlignFormatting(category->m_Name, 20)); |
| 992 | categoryBody.append(" | "); |
| 993 | categoryBody.append(CentreAlignFormatting(std::to_string(category->m_DeviceUid), 12)); |
| 994 | categoryBody.append(" | "); |
| 995 | categoryBody.append(CentreAlignFormatting(std::to_string(category->m_CounterSetUid), 16)); |
| 996 | categoryBody.append(" | "); |
| 997 | categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14)); |
| 998 | |
| 999 | std::cout << "\n" << "\n"; |
| 1000 | std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size())); |
| 1001 | std::cout << "\n"; |
| 1002 | std::cout << std::string(categoryHeader.size(), '=') << "\n"; |
| 1003 | |
| 1004 | std::cout << categoryHeader; |
| 1005 | |
| 1006 | std::cout << std::string(categoryBody.size(), '-') << "\n"; |
| 1007 | |
| 1008 | std::cout << categoryBody; |
| 1009 | |
| 1010 | std::string counterHeader; |
| 1011 | |
| 1012 | counterHeader.append(CentreAlignFormatting("Counter Name", 20)); |
| 1013 | counterHeader.append(" | "); |
| 1014 | counterHeader.append(CentreAlignFormatting("Description", 50)); |
| 1015 | counterHeader.append(" | "); |
| 1016 | counterHeader.append(CentreAlignFormatting("Units", 14)); |
| 1017 | counterHeader.append(" | "); |
| 1018 | counterHeader.append(CentreAlignFormatting("UID", 6)); |
| 1019 | counterHeader.append(" | "); |
| 1020 | counterHeader.append(CentreAlignFormatting("Max UID", 10)); |
| 1021 | counterHeader.append(" | "); |
| 1022 | counterHeader.append(CentreAlignFormatting("Class", 8)); |
| 1023 | counterHeader.append(" | "); |
| 1024 | counterHeader.append(CentreAlignFormatting("Interpolation", 14)); |
| 1025 | counterHeader.append(" | "); |
| 1026 | counterHeader.append(CentreAlignFormatting("Multiplier", 20)); |
| 1027 | counterHeader.append(" | "); |
| 1028 | counterHeader.append(CentreAlignFormatting("Counter set UID", 16)); |
| 1029 | counterHeader.append(" | "); |
| 1030 | counterHeader.append(CentreAlignFormatting("Device UID", 14)); |
| 1031 | counterHeader.append("\n"); |
| 1032 | |
| 1033 | std::cout << "\n" << "\n"; |
| 1034 | std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name, |
| 1035 | static_cast<int>(counterHeader.size())); |
| 1036 | std::cout << "\n"; |
| 1037 | std::cout << std::string(counterHeader.size(), '=') << "\n"; |
| 1038 | std::cout << counterHeader; |
| 1039 | for (auto& it: category->m_Counters) { |
| 1040 | auto search = counterMap.find(it); |
| 1041 | if(search != counterMap.end()) { |
| 1042 | PrintCounterDetails(search->second); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void PrintCounterDirectory(ICounterDirectory& counterDirectory) |
| 1048 | { |
| 1049 | std::string devicesHeader; |
| 1050 | |
| 1051 | devicesHeader.append(CentreAlignFormatting("Device name", 20)); |
| 1052 | devicesHeader.append(" | "); |
| 1053 | devicesHeader.append(CentreAlignFormatting("UID", 13)); |
| 1054 | devicesHeader.append(" | "); |
| 1055 | devicesHeader.append(CentreAlignFormatting("Cores", 10)); |
| 1056 | devicesHeader.append("\n"); |
| 1057 | |
| 1058 | std::cout << "\n" << "\n"; |
| 1059 | std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size())); |
| 1060 | std::cout << "\n"; |
| 1061 | std::cout << std::string(devicesHeader.size(), '=') << "\n"; |
| 1062 | std::cout << devicesHeader; |
| 1063 | for (auto& it: counterDirectory.GetDevices()) { |
| 1064 | PrintDeviceDetails(it); |
| 1065 | } |
| 1066 | |
| 1067 | std::string counterSetHeader; |
| 1068 | |
| 1069 | counterSetHeader.append(CentreAlignFormatting("Counter set name", 20)); |
| 1070 | counterSetHeader.append(" | "); |
| 1071 | counterSetHeader.append(CentreAlignFormatting("UID", 13)); |
| 1072 | counterSetHeader.append(" | "); |
| 1073 | counterSetHeader.append(CentreAlignFormatting("Count", 10)); |
| 1074 | counterSetHeader.append("\n"); |
| 1075 | |
| 1076 | std::cout << "\n" << "\n"; |
| 1077 | std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size())); |
| 1078 | std::cout << "\n"; |
| 1079 | std::cout << std::string(counterSetHeader.size(), '=') << "\n"; |
| 1080 | |
| 1081 | std::cout << counterSetHeader; |
| 1082 | |
| 1083 | for (auto& it: counterDirectory.GetCounterSets()) { |
| 1084 | PrintCounterSetDetails(it); |
| 1085 | } |
| 1086 | |
| 1087 | auto counters = counterDirectory.GetCounters(); |
| 1088 | for (auto& it: counterDirectory.GetCategories()) { |
| 1089 | PrintCategoryDetails(it, counters); |
| 1090 | } |
| 1091 | std::cout << "\n"; |
| 1092 | } |
| 1093 | |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 1094 | uint64_t GetTimestamp() |
| 1095 | { |
| 1096 | #if USE_CLOCK_MONOTONIC_RAW |
| 1097 | using clock = MonotonicClockRaw; |
| 1098 | #else |
| 1099 | using clock = std::chrono::steady_clock; |
| 1100 | #endif |
| 1101 | |
| 1102 | // Take a timestamp |
| 1103 | auto timestamp = clock::now(); |
| 1104 | |
| 1105 | return static_cast<uint64_t>(timestamp.time_since_epoch().count()); |
| 1106 | } |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 1107 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 1108 | } // namespace profiling |
| 1109 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 1110 | } // namespace armnn |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 1111 | |
| 1112 | namespace std |
| 1113 | { |
| 1114 | |
| 1115 | bool operator==(const std::vector<uint8_t>& left, std::thread::id right) |
| 1116 | { |
| 1117 | return std::memcmp(left.data(), &right, left.size()) == 0; |
| 1118 | } |
| 1119 | |
| 1120 | } // namespace std |