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