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 | #pragma once |
| 7 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 8 | #include <armnn/Exceptions.hpp> |
| 9 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 10 | #include <boost/numeric/conversion/cast.hpp> |
| 11 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 12 | #include <string> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 13 | #include <vector> |
| 14 | #include <algorithm> |
| 15 | #include <cstring> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | namespace profiling |
| 21 | { |
| 22 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 23 | struct SwTraceCharPolicy |
| 24 | { |
| 25 | static bool IsValidChar(unsigned char c) |
| 26 | { |
| 27 | // Check that the given character has ASCII 7-bit encoding |
| 28 | return c < 128; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | struct SwTraceNameCharPolicy |
| 33 | { |
| 34 | static bool IsValidChar(unsigned char c) |
| 35 | { |
| 36 | // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only |
| 37 | return c < 128 && (std::isalnum(c) || c == '_'); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | template <typename SwTracePolicy> |
| 42 | bool IsValidSwTraceString(const std::string& s) |
| 43 | { |
| 44 | // Check that all the characters in the given string conform to the given policy |
| 45 | return std::all_of(s.begin(), s.end(), [](unsigned char c) |
| 46 | { |
| 47 | return SwTracePolicy::IsValidChar(c); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | template <typename SwTracePolicy> |
| 52 | bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer) |
| 53 | { |
| 54 | // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into |
| 55 | // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary |
| 56 | |
| 57 | // Clear the output buffer |
| 58 | outputBuffer.clear(); |
| 59 | |
| 60 | // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars") |
| 61 | if (!IsValidSwTraceString<SwTracePolicy>(s)) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // Prepare the output buffer |
| 67 | size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator |
| 68 | size_t uint32_t_size = sizeof(uint32_t); |
| 69 | size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0); |
| 70 | outputBuffer.resize(outBufferSize, '\0'); |
| 71 | |
| 72 | // Write the SWTrace string to the output buffer |
| 73 | outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size); |
| 74 | std::memcpy(outputBuffer.data() + 1, s.data(), s_size); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | uint16_t GetNextUid(bool peekOnly = false); |
| 80 | |
| 81 | std::vector<uint16_t> GetNextCounterUids(uint16_t cores); |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 82 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 83 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value); |
| 84 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 85 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value); |
| 86 | |
| 87 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value); |
| 88 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 89 | uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset); |
| 90 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 91 | uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset); |
| 92 | |
| 93 | uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset); |
| 94 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame^] | 95 | uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset); |
| 96 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 97 | std::string GetSoftwareInfo(); |
| 98 | |
| 99 | std::string GetSoftwareVersion(); |
| 100 | |
| 101 | std::string GetHardwareVersion(); |
| 102 | |
| 103 | std::string GetProcessName(); |
| 104 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 105 | class BufferExhaustion : public armnn::Exception |
| 106 | { |
| 107 | using Exception::Exception; |
| 108 | }; |
| 109 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 110 | } // namespace profiling |
| 111 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 112 | } // namespace armnn |