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