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> |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 14 | #include <limits> |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 15 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | namespace profiling |
| 20 | { |
| 21 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 22 | namespace |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 23 | { |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 24 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 25 | void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0) |
| 26 | { |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 27 | // 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] | 28 | switch (cores) |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 29 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 30 | case 0: |
| 31 | case 1: |
| 32 | // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of |
| 33 | // running multiple parallel workloads and will not provide multiple streams of data for each event) |
| 34 | if (uid == std::numeric_limits<uint16_t>::max()) |
| 35 | { |
| 36 | throw RuntimeException("Generating the next UID for profiling would result in an overflow"); |
| 37 | } |
| 38 | break; |
| 39 | default: // cores > 1 |
| 40 | // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum |
| 41 | // allowed value for a counter UID is consequently: uint16_t_max - cores + 1 |
| 42 | if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1) |
| 43 | { |
| 44 | throw RuntimeException("Generating the next UID for profiling would result in an overflow"); |
| 45 | } |
| 46 | break; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 47 | } |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 48 | } |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 49 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 50 | } // Anonymous namespace |
| 51 | |
| 52 | uint16_t GetNextUid(bool peekOnly) |
| 53 | { |
| 54 | // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value |
| 55 | static uint16_t uid = 1; |
| 56 | |
| 57 | // Check that it is possible to generate the next UID without causing an overflow (throws in case of error) |
| 58 | ThrowIfCantGenerateNextUid(uid); |
| 59 | |
| 60 | if (peekOnly) |
| 61 | { |
| 62 | // Peek only |
| 63 | return uid; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | // Get the next UID |
| 68 | return uid++; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | std::vector<uint16_t> GetNextCounterUids(uint16_t cores) |
| 73 | { |
| 74 | // The UID used for counters only. The first valid UID is 0 |
| 75 | static uint16_t counterUid = 0; |
| 76 | |
| 77 | // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error) |
| 78 | ThrowIfCantGenerateNextUid(counterUid, cores); |
| 79 | |
| 80 | // Get the next counter UIDs |
| 81 | size_t counterUidsSize = cores == 0 ? 1 : cores; |
| 82 | std::vector<uint16_t> counterUids(counterUidsSize, 0); |
| 83 | for (size_t i = 0; i < counterUidsSize; i++) |
| 84 | { |
| 85 | counterUids[i] = counterUid++; |
| 86 | } |
| 87 | return counterUids; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame^] | 90 | void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value) |
| 91 | { |
| 92 | BOOST_ASSERT(packetBuffer); |
| 93 | |
| 94 | WriteUint64(packetBuffer->GetWritableData(), offset, value); |
| 95 | } |
| 96 | |
| 97 | void WriteUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint32_t value) |
| 98 | { |
| 99 | BOOST_ASSERT(packetBuffer); |
| 100 | |
| 101 | WriteUint32(packetBuffer->GetWritableData(), offset, value); |
| 102 | } |
| 103 | |
| 104 | void WriteUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint16_t value) |
| 105 | { |
| 106 | BOOST_ASSERT(packetBuffer); |
| 107 | |
| 108 | WriteUint16(packetBuffer->GetWritableData(), offset, value); |
| 109 | } |
| 110 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 111 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value) |
| 112 | { |
| 113 | BOOST_ASSERT(buffer); |
| 114 | |
| 115 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
| 116 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 117 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 118 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 119 | buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF); |
| 120 | buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF); |
| 121 | buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF); |
| 122 | buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF); |
| 123 | } |
| 124 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 125 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value) |
| 126 | { |
| 127 | BOOST_ASSERT(buffer); |
| 128 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 129 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 130 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 131 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 132 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 133 | } |
| 134 | |
| 135 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value) |
| 136 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 137 | BOOST_ASSERT(buffer); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 138 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 139 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 140 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 141 | } |
| 142 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame^] | 143 | uint64_t ReadUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset) |
| 144 | { |
| 145 | BOOST_ASSERT(packetBuffer); |
| 146 | |
| 147 | return ReadUint64(packetBuffer->GetReadableData(), offset); |
| 148 | } |
| 149 | |
| 150 | uint32_t ReadUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset) |
| 151 | { |
| 152 | BOOST_ASSERT(packetBuffer); |
| 153 | |
| 154 | return ReadUint32(packetBuffer->GetReadableData(), offset); |
| 155 | } |
| 156 | |
| 157 | uint16_t ReadUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset) |
| 158 | { |
| 159 | BOOST_ASSERT(packetBuffer); |
| 160 | |
| 161 | return ReadUint16(packetBuffer->GetReadableData(), offset); |
| 162 | } |
| 163 | |
| 164 | uint8_t ReadUint8(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset) |
| 165 | { |
| 166 | BOOST_ASSERT(packetBuffer); |
| 167 | |
| 168 | return ReadUint8(packetBuffer->GetReadableData(), offset); |
| 169 | } |
| 170 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 171 | uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset) |
| 172 | { |
| 173 | BOOST_ASSERT(buffer); |
| 174 | |
| 175 | uint64_t value = 0; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 176 | value = static_cast<uint64_t>(buffer[offset]); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 177 | value |= static_cast<uint64_t>(buffer[offset + 1]) << 8; |
| 178 | value |= static_cast<uint64_t>(buffer[offset + 2]) << 16; |
| 179 | value |= static_cast<uint64_t>(buffer[offset + 3]) << 24; |
| 180 | value |= static_cast<uint64_t>(buffer[offset + 4]) << 32; |
| 181 | value |= static_cast<uint64_t>(buffer[offset + 5]) << 40; |
| 182 | value |= static_cast<uint64_t>(buffer[offset + 6]) << 48; |
| 183 | value |= static_cast<uint64_t>(buffer[offset + 7]) << 56; |
| 184 | |
| 185 | return value; |
| 186 | } |
| 187 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 188 | uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset) |
| 189 | { |
| 190 | BOOST_ASSERT(buffer); |
| 191 | |
| 192 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 193 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 194 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 195 | value |= static_cast<uint32_t>(buffer[offset + 2]) << 16; |
| 196 | value |= static_cast<uint32_t>(buffer[offset + 3]) << 24; |
| 197 | return value; |
| 198 | } |
| 199 | |
| 200 | uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset) |
| 201 | { |
| 202 | BOOST_ASSERT(buffer); |
| 203 | |
| 204 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 205 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 206 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 207 | return static_cast<uint16_t>(value); |
| 208 | } |
| 209 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 210 | uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset) |
| 211 | { |
| 212 | BOOST_ASSERT(buffer); |
| 213 | |
| 214 | return buffer[offset]; |
| 215 | } |
| 216 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 217 | std::string GetSoftwareInfo() |
| 218 | { |
| 219 | return std::string("ArmNN"); |
| 220 | } |
| 221 | |
| 222 | std::string GetHardwareVersion() |
| 223 | { |
| 224 | return std::string(); |
| 225 | } |
| 226 | |
| 227 | std::string GetSoftwareVersion() |
| 228 | { |
| 229 | std::string armnnVersion(ARMNN_VERSION); |
| 230 | std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2); |
| 231 | return result; |
| 232 | } |
| 233 | |
| 234 | std::string GetProcessName() |
| 235 | { |
| 236 | std::ifstream comm("/proc/self/comm"); |
| 237 | std::string name; |
| 238 | getline(comm, name); |
| 239 | return name; |
| 240 | } |
| 241 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 242 | } // namespace profiling |
| 243 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 244 | } // namespace armnn |