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