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 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 10 | #include "ICounterDirectory.hpp" |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 11 | #include "IPacketBuffer.hpp" |
| 12 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 13 | #include <boost/numeric/conversion/cast.hpp> |
| 14 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | #include <cstring> |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 20 | #include <thread> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
| 25 | namespace profiling |
| 26 | { |
| 27 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 28 | struct SwTraceHeader |
| 29 | { |
| 30 | uint8_t m_StreamVersion; |
| 31 | uint8_t m_PointerBytes; |
| 32 | uint8_t m_ThreadIdBytes; |
| 33 | }; |
| 34 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 35 | struct SwTraceMessage |
| 36 | { |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 37 | uint32_t m_Id; |
| 38 | std::string m_Name; |
| 39 | std::string m_UiName; |
| 40 | std::vector<char> m_ArgTypes; |
| 41 | std::vector<std::string> m_ArgNames; |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 44 | struct SwTraceCharPolicy |
| 45 | { |
| 46 | static bool IsValidChar(unsigned char c) |
| 47 | { |
| 48 | // Check that the given character has ASCII 7-bit encoding |
| 49 | return c < 128; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | struct SwTraceNameCharPolicy |
| 54 | { |
| 55 | static bool IsValidChar(unsigned char c) |
| 56 | { |
| 57 | // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only |
| 58 | return c < 128 && (std::isalnum(c) || c == '_'); |
| 59 | } |
| 60 | }; |
| 61 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 62 | struct SwTraceTypeCharPolicy |
| 63 | { |
| 64 | static bool IsValidChar(unsigned char c) |
| 65 | { |
| 66 | // Check that the given character is among the allowed ones |
| 67 | switch (c) |
| 68 | { |
| 69 | case '@': |
| 70 | case 't': |
| 71 | case 'i': |
| 72 | case 'I': |
| 73 | case 'l': |
| 74 | case 'L': |
| 75 | case 'F': |
| 76 | case 'p': |
| 77 | case 's': |
| 78 | return true; // Valid char |
| 79 | default: |
| 80 | return false; // Invalid char |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 85 | template <typename SwTracePolicy> |
| 86 | bool IsValidSwTraceString(const std::string& s) |
| 87 | { |
| 88 | // Check that all the characters in the given string conform to the given policy |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 89 | return std::all_of(s.begin(), s.end(), [](unsigned char c) { return SwTracePolicy::IsValidChar(c); }); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | template <typename SwTracePolicy> |
| 93 | bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer) |
| 94 | { |
| 95 | // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into |
| 96 | // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary |
| 97 | |
| 98 | // Clear the output buffer |
| 99 | outputBuffer.clear(); |
| 100 | |
| 101 | // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars") |
| 102 | if (!IsValidSwTraceString<SwTracePolicy>(s)) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Prepare the output buffer |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 108 | size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 109 | size_t uint32_t_size = sizeof(uint32_t); |
| 110 | size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0); |
| 111 | outputBuffer.resize(outBufferSize, '\0'); |
| 112 | |
| 113 | // Write the SWTrace string to the output buffer |
| 114 | outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size); |
| 115 | std::memcpy(outputBuffer.data() + 1, s.data(), s_size); |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 120 | template <typename SwTracePolicy, |
| 121 | typename SwTraceBuffer = std::vector<uint32_t>> |
| 122 | bool ConvertDirectoryComponent(const std::string& directoryComponent, SwTraceBuffer& swTraceBuffer) |
| 123 | { |
| 124 | // Convert the directory component using the given policy |
| 125 | SwTraceBuffer tempSwTraceBuffer; |
| 126 | bool result = StringToSwTraceString<SwTracePolicy>(directoryComponent, tempSwTraceBuffer); |
| 127 | if (!result) |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | swTraceBuffer.insert(swTraceBuffer.end(), tempSwTraceBuffer.begin(), tempSwTraceBuffer.end()); |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 137 | uint16_t GetNextUid(bool peekOnly = false); |
| 138 | |
| 139 | std::vector<uint16_t> GetNextCounterUids(uint16_t cores); |
Matteo Martincigh | ab173e9 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 140 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 141 | void WriteBytes(const IPacketBuffer& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize); |
| 142 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 143 | uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId); |
| 144 | |
| 145 | uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType); |
| 146 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 147 | void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 148 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 149 | void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 150 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 151 | void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 152 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 153 | void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value); |
| 154 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 155 | void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize); |
| 156 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 157 | void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value); |
| 158 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 159 | void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value); |
| 160 | |
| 161 | void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value); |
| 162 | |
Matteo Martincigh | 34a407d | 2019-11-06 15:30:54 +0000 | [diff] [blame] | 163 | void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value); |
| 164 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 165 | void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]); |
| 166 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 167 | uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 168 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 169 | uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 170 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 171 | uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 172 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 173 | uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 174 | |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 175 | void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]); |
| 176 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 177 | uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset); |
| 178 | |
| 179 | uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset); |
| 180 | |
| 181 | uint16_t ReadUint16(unsigned const char* buffer, unsigned int offset); |
| 182 | |
| 183 | uint8_t ReadUint8(unsigned const char* buffer, unsigned int offset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 184 | |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 185 | std::string GetSoftwareInfo(); |
| 186 | |
| 187 | std::string GetSoftwareVersion(); |
| 188 | |
| 189 | std::string GetHardwareVersion(); |
| 190 | |
| 191 | std::string GetProcessName(); |
| 192 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 193 | enum class TimelinePacketStatus |
| 194 | { |
| 195 | Ok, |
| 196 | Error, |
| 197 | BufferExhaustion |
| 198 | }; |
| 199 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 200 | enum class ProfilingRelationshipType |
| 201 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 202 | RetentionLink, /// Head retains(parents) Tail |
| 203 | ExecutionLink, /// Head execution start depends on Tail execution completion |
| 204 | DataLink, /// Head uses data of Tail |
| 205 | LabelLink /// Head uses label Tail (Tail MUST be a guid of a label). |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 206 | }; |
| 207 | |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 208 | uint32_t CalculateSizeOfPaddedSwString(const std::string& str); |
| 209 | |
Finn Williams | e63a026 | 2019-10-22 10:30:49 +0100 | [diff] [blame] | 210 | SwTraceMessage ReadSwTraceMessage(const unsigned char*, unsigned int& offset); |
Sadik Armagan | 7bbdf9d | 2019-10-24 10:26:05 +0100 | [diff] [blame] | 211 | |
Matteo Martincigh | 0aed4f9 | 2019-10-01 14:25:34 +0100 | [diff] [blame] | 212 | TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, |
| 213 | const std::string& label, |
| 214 | unsigned char* buffer, |
| 215 | unsigned int bufferSize, |
| 216 | unsigned int& numberOfBytesWritten); |
| 217 | |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 218 | TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 219 | unsigned char* buffer, |
| 220 | unsigned int bufferSize, |
| 221 | unsigned int& numberOfBytesWritten); |
David Monahan | f21f606 | 2019-10-07 15:11:15 +0100 | [diff] [blame] | 222 | |
Narumol Prangnawarat | 7e5eec7 | 2019-10-16 12:16:26 +0100 | [diff] [blame] | 223 | TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, |
| 224 | uint64_t relationshipGuid, |
| 225 | uint64_t headGuid, |
| 226 | uint64_t tailGuid, |
| 227 | unsigned char* buffer, |
| 228 | unsigned int bufferSize, |
| 229 | unsigned int& numberOfBytesWritten); |
| 230 | |
Sadik Armagan | 784db77 | 2019-10-08 15:05:38 +0100 | [diff] [blame] | 231 | TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer, |
| 232 | unsigned int bufferSize, |
| 233 | unsigned int& numberOfBytesWritten); |
| 234 | |
Jan Eilers | 92fa15b | 2019-10-15 15:23:25 +0100 | [diff] [blame] | 235 | TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, |
| 236 | unsigned char* buffer, |
| 237 | unsigned int bufferSize, |
| 238 | unsigned int& numberOfBytesWritten); |
| 239 | |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 240 | TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp, |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 241 | std::thread::id threadId, |
Matteo Martincigh | 8844c2f | 2019-10-16 10:29:17 +0100 | [diff] [blame] | 242 | uint64_t profilingGuid, |
| 243 | unsigned char* buffer, |
| 244 | unsigned int bufferSize, |
| 245 | unsigned int& numberOfBytesWritten); |
| 246 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 247 | std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth); |
| 248 | |
| 249 | void PrintCounterDirectory(ICounterDirectory& counterDirectory); |
| 250 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 251 | class BufferExhaustion : public armnn::Exception |
| 252 | { |
| 253 | using Exception::Exception; |
| 254 | }; |
| 255 | |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 256 | uint64_t GetTimestamp(); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 257 | |
Matteo Martincigh | 5dc816e | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 258 | } // namespace profiling |
| 259 | |
| 260 | } // namespace armnn |
Matteo Martincigh | 378bbfc | 2019-11-04 14:05:28 +0000 | [diff] [blame] | 261 | |
| 262 | namespace std |
| 263 | { |
| 264 | |
| 265 | bool operator==(const std::vector<uint8_t>& left, std::thread::id right); |
| 266 | |
| 267 | } // namespace std |