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