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> |
| 9 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 10 | #include <boost/assert.hpp> |
| 11 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 12 | #include <fstream> |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 13 | #include <limits> |
| 14 | #include <mutex> |
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 | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 22 | uint16_t GetNextUid() |
| 23 | { |
| 24 | // Static mutex for reading and modifying the global UID a single thread at the time |
| 25 | static std::mutex mutex; |
| 26 | std::unique_lock<std::mutex> lock(mutex); |
| 27 | |
| 28 | // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value |
| 29 | // (it is used to indicate that a record is not associated with any device) |
| 30 | static uint16_t uid{ 0 }; |
| 31 | |
| 32 | // Check that it is possible to generate the next UID without causing an overflow |
| 33 | if (uid == std::numeric_limits<uint16_t>::max()) |
| 34 | { |
| 35 | throw RuntimeException("Generating the next UID for profiling would result in an overflow"); |
| 36 | } |
| 37 | |
| 38 | // Thread safe increment, the value that is incremented is the value checked for overflow, |
| 39 | // as this whole function is mutexed |
| 40 | return ++uid; |
| 41 | } |
| 42 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 43 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value) |
| 44 | { |
| 45 | BOOST_ASSERT(buffer); |
| 46 | |
| 47 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
| 48 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 49 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 50 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 51 | buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF); |
| 52 | buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF); |
| 53 | buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF); |
| 54 | buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF); |
| 55 | } |
| 56 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 57 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value) |
| 58 | { |
| 59 | BOOST_ASSERT(buffer); |
| 60 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 61 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 62 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 63 | buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 64 | buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 65 | } |
| 66 | |
| 67 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value) |
| 68 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 69 | BOOST_ASSERT(buffer); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 70 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 71 | buffer[offset] = static_cast<unsigned char>(value & 0xFF); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 72 | buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 73 | } |
| 74 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 75 | uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset) |
| 76 | { |
| 77 | BOOST_ASSERT(buffer); |
| 78 | |
| 79 | uint64_t value = 0; |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 80 | value = static_cast<uint64_t>(buffer[offset]); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 81 | value |= static_cast<uint64_t>(buffer[offset + 1]) << 8; |
| 82 | value |= static_cast<uint64_t>(buffer[offset + 2]) << 16; |
| 83 | value |= static_cast<uint64_t>(buffer[offset + 3]) << 24; |
| 84 | value |= static_cast<uint64_t>(buffer[offset + 4]) << 32; |
| 85 | value |= static_cast<uint64_t>(buffer[offset + 5]) << 40; |
| 86 | value |= static_cast<uint64_t>(buffer[offset + 6]) << 48; |
| 87 | value |= static_cast<uint64_t>(buffer[offset + 7]) << 56; |
| 88 | |
| 89 | return value; |
| 90 | } |
| 91 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 92 | uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset) |
| 93 | { |
| 94 | BOOST_ASSERT(buffer); |
| 95 | |
| 96 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 97 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 98 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 99 | value |= static_cast<uint32_t>(buffer[offset + 2]) << 16; |
| 100 | value |= static_cast<uint32_t>(buffer[offset + 3]) << 24; |
| 101 | return value; |
| 102 | } |
| 103 | |
| 104 | uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset) |
| 105 | { |
| 106 | BOOST_ASSERT(buffer); |
| 107 | |
| 108 | uint32_t value = 0; |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 109 | value = static_cast<uint32_t>(buffer[offset]); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 110 | value |= static_cast<uint32_t>(buffer[offset + 1]) << 8; |
| 111 | return static_cast<uint16_t>(value); |
| 112 | } |
| 113 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 114 | std::string GetSoftwareInfo() |
| 115 | { |
| 116 | return std::string("ArmNN"); |
| 117 | } |
| 118 | |
| 119 | std::string GetHardwareVersion() |
| 120 | { |
| 121 | return std::string(); |
| 122 | } |
| 123 | |
| 124 | std::string GetSoftwareVersion() |
| 125 | { |
| 126 | std::string armnnVersion(ARMNN_VERSION); |
| 127 | std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2); |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | std::string GetProcessName() |
| 132 | { |
| 133 | std::ifstream comm("/proc/self/comm"); |
| 134 | std::string name; |
| 135 | getline(comm, name); |
| 136 | return name; |
| 137 | } |
| 138 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 139 | } // namespace profiling |
| 140 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 141 | } // namespace armnn |