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