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> |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 19 | #include <thread> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 20 | |
| 21 | namespace armnn |
| 22 | { |
| 23 | |
| 24 | namespace profiling |
| 25 | { |
| 26 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 27 | struct SwTraceMessage |
| 28 | { |
| 29 | uint32_t id; |
| 30 | std::string name; |
| 31 | std::string uiName; |
| 32 | std::vector<char> argTypes; |
| 33 | std::vector<std::string> argNames; |
| 34 | }; |
| 35 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 36 | struct SwTraceCharPolicy |
| 37 | { |
| 38 | static bool IsValidChar(unsigned char c) |
| 39 | { |
| 40 | // Check that the given character has ASCII 7-bit encoding |
| 41 | return c < 128; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | struct SwTraceNameCharPolicy |
| 46 | { |
| 47 | static bool IsValidChar(unsigned char c) |
| 48 | { |
| 49 | // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only |
| 50 | return c < 128 && (std::isalnum(c) || c == '_'); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | template <typename SwTracePolicy> |
| 55 | bool IsValidSwTraceString(const std::string& s) |
| 56 | { |
| 57 | // Check that all the characters in the given string conform to the given policy |
| 58 | return std::all_of(s.begin(), s.end(), [](unsigned char c) |
| 59 | { |
| 60 | return SwTracePolicy::IsValidChar(c); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | template <typename SwTracePolicy> |
| 65 | bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer) |
| 66 | { |
| 67 | // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into |
| 68 | // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary |
| 69 | |
| 70 | // Clear the output buffer |
| 71 | outputBuffer.clear(); |
| 72 | |
| 73 | // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars") |
| 74 | if (!IsValidSwTraceString<SwTracePolicy>(s)) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Prepare the output buffer |
| 80 | size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator |
| 81 | size_t uint32_t_size = sizeof(uint32_t); |
| 82 | size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0); |
| 83 | outputBuffer.resize(outBufferSize, '\0'); |
| 84 | |
| 85 | // Write the SWTrace string to the output buffer |
| 86 | outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size); |
| 87 | std::memcpy(outputBuffer.data() + 1, s.data(), s_size); |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | uint16_t GetNextUid(bool peekOnly = false); |
| 93 | |
| 94 | std::vector<uint16_t> GetNextCounterUids(uint16_t cores); |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 95 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 96 | void WriteBytes(const IPacketBuffer& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize); |
| 97 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 98 | void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 99 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 100 | void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 101 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 102 | void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 103 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 104 | void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize); |
| 105 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 106 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value); |
| 107 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 108 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value); |
| 109 | |
| 110 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value); |
| 111 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 112 | void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]); |
| 113 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 114 | uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 115 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 116 | uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 117 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 118 | uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 119 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 120 | uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 121 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 122 | void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]); |
| 123 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 124 | uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset); |
| 125 | |
| 126 | uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset); |
| 127 | |
| 128 | uint16_t ReadUint16(unsigned const char* buffer, unsigned int offset); |
| 129 | |
| 130 | uint8_t ReadUint8(unsigned const char* buffer, unsigned int offset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 131 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 132 | std::string GetSoftwareInfo(); |
| 133 | |
| 134 | std::string GetSoftwareVersion(); |
| 135 | |
| 136 | std::string GetHardwareVersion(); |
| 137 | |
| 138 | std::string GetProcessName(); |
| 139 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 140 | enum class TimelinePacketStatus |
| 141 | { |
| 142 | Ok, |
| 143 | Error, |
| 144 | BufferExhaustion |
| 145 | }; |
| 146 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 147 | enum class ProfilingRelationshipType |
| 148 | { |
| 149 | RetentionLink, /// Head retains(parents) Tail |
| 150 | ExecutionLink, /// Head execution start depends on Tail execution completion |
| 151 | DataLink, /// Head uses data of Tail |
| 152 | LabelLink /// Head uses label Tail (Tail MUST be a guid of a label). |
| 153 | }; |
| 154 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 155 | uint32_t CalculateSizeOfPaddedSwString(const std::string& str); |
| 156 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 157 | SwTraceMessage ReadSwTraceMessage(const IPacketBufferPtr& packetBuffer, unsigned int& offset); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 158 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 159 | TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, |
| 160 | const std::string& label, |
| 161 | unsigned char* buffer, |
| 162 | unsigned int bufferSize, |
| 163 | unsigned int& numberOfBytesWritten); |
| 164 | |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 165 | TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 166 | unsigned char* buffer, |
| 167 | unsigned int bufferSize, |
| 168 | unsigned int& numberOfBytesWritten); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 169 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 170 | TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 171 | uint64_t relationshipGuid, |
| 172 | uint64_t headGuid, |
| 173 | uint64_t tailGuid, |
| 174 | unsigned char* buffer, |
| 175 | unsigned int bufferSize, |
| 176 | unsigned int& numberOfBytesWritten); |
| 177 | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 178 | TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, |
| 179 | unsigned int bufferSize, |
| 180 | unsigned int& numberOfBytesWritten); |
| 181 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 182 | TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, |
| 183 | unsigned char* buffer, |
| 184 | unsigned int bufferSize, |
| 185 | unsigned int& numberOfBytesWritten); |
| 186 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 187 | TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp, |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 188 | std::thread::id threadId, |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 189 | uint64_t profilingGuid, |
| 190 | unsigned char* buffer, |
| 191 | unsigned int bufferSize, |
| 192 | unsigned int& numberOfBytesWritten); |
| 193 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 194 | class BufferExhaustion : public armnn::Exception |
| 195 | { |
| 196 | using Exception::Exception; |
| 197 | }; |
| 198 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 199 | } // namespace profiling |
| 200 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 201 | } // namespace armnn |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame^] | 202 | |
| 203 | namespace std |
| 204 | { |
| 205 | |
| 206 | bool operator==(const std::vector<uint8_t>& left, std::thread::id right); |
| 207 | |
| 208 | } // namespace std |