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