ArmNN
 20.05
ProfilingUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/Exceptions.hpp>
10 
11 #include "ICounterDirectory.hpp"
12 #include "IPacketBuffer.hpp"
13 
14 #include <Packet.hpp>
15 
16 #include <boost/numeric/conversion/cast.hpp>
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <memory>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 namespace armnn
26 {
27 
28 namespace profiling
29 {
30 
31 constexpr unsigned int ThreadIdSize = sizeof(std::thread::id); // Is platform dependent
32 
34 {
35  uint8_t m_StreamVersion;
36  uint8_t m_PointerBytes;
37  uint8_t m_ThreadIdBytes;
38 };
39 
41 {
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;
47 };
48 
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 
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 
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 
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
94  return std::all_of(s.begin(), s.end(), [](unsigned char c) { return SwTracePolicy::IsValidChar(c); });
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
113  size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator
114  size_t uint32_t_size = sizeof(uint32_t);
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);
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 
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 
145 uint16_t GetNextUid(bool peekOnly = false);
146 
147 std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores);
148 
149 void WriteBytes(const IPacketBuffer& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize);
150 
151 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId);
152 
153 void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value);
154 
155 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value);
156 
157 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value);
158 
159 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value);
160 
161 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize);
162 
163 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
164 
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 
169 void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value);
170 
171 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
172 
173 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset);
174 
175 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset);
176 
177 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset);
178 
179 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset);
180 
181 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
182 
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);
190 
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 
198 std::string GetSoftwareInfo();
199 
200 std::string GetSoftwareVersion();
201 
202 std::string GetHardwareVersion();
203 
204 std::string GetProcessName();
205 
207 {
208  Ok,
209  Error,
211 };
212 
213 uint32_t CalculateSizeOfPaddedSwString(const std::string& str);
214 
215 SwTraceMessage ReadSwTraceMessage(const unsigned char*, unsigned int& offset);
216 
218  const std::string& label,
219  unsigned char* buffer,
220  unsigned int bufferSize,
221  unsigned int& numberOfBytesWritten);
222 
223 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
224  unsigned char* buffer,
225  unsigned int bufferSize,
226  unsigned int& numberOfBytesWritten);
227 
229  uint64_t relationshipGuid,
230  uint64_t headGuid,
231  uint64_t tailGuid,
232  unsigned char* buffer,
233  unsigned int bufferSize,
234  unsigned int& numberOfBytesWritten);
235 
237  unsigned int bufferSize,
238  unsigned int& numberOfBytesWritten);
239 
241  unsigned char* buffer,
242  unsigned int bufferSize,
243  unsigned int& numberOfBytesWritten);
244 
246  std::thread::id threadId,
247  uint64_t profilingGuid,
248  unsigned char* buffer,
249  unsigned int bufferSize,
250  unsigned int& numberOfBytesWritten);
251 
252 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth);
253 
254 void PrintCounterDirectory(ICounterDirectory& counterDirectory);
255 
256 class BufferExhaustion : public armnn::Exception
257 {
258  using Exception::Exception;
259 };
260 
261 uint64_t GetTimestamp();
262 
263 Packet ReceivePacket(const unsigned char* buffer, uint32_t length);
264 
265 } // namespace profiling
266 
267 } // namespace armnn
268 
269 namespace std
270 {
271 
272 bool operator==(const std::vector<uint8_t>& left, std::thread::id right);
273 
274 } // namespace std
bool StringToSwTraceString(const std::string &s, std::vector< uint32_t > &outputBuffer)
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::string GetHardwareVersion()
void WriteUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint16_t value)
void ReadBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)
uint64_t ReadUint64(const IPacketBufferPtr &packetBuffer, unsigned int offset)
std::pair< uint32_t, uint32_t > CreateTimelinePacketHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType, uint32_t streamId, uint32_t sequenceNumbered, uint32_t dataLength)
Creates a timeline packet header.
std::string GetProcessName()
Exception(const std::string &message)
Definition: Exceptions.cpp:12
uint16_t ReadUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset)
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2020 ARM Limited.
std::string GetSoftwareInfo()
TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
uint16_t GetNextUid(bool peekOnly)
SwTraceMessage ReadSwTraceMessage(const unsigned char *packetBuffer, unsigned int &offset)
bool operator==(const armnn::DataLayout &dataLayout, const DataLayoutIndexed &indexed)
Equality methods.
std::vector< std::string > m_ArgNames
bool IsValidSwTraceString(const std::string &s)
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
void WriteBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, const void *value, unsigned int valueSize)
uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
constexpr unsigned int ThreadIdSize
void WriteUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint8_t value)
void WriteUint64(const std::unique_ptr< IPacketBuffer > &packetBuffer, unsigned int offset, uint64_t value)
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
static bool IsValidChar(unsigned char c)
std::string GetSoftwareVersion()
Packet ReceivePacket(const unsigned char *buffer, uint32_t length)
TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
uint32_t CalculateSizeOfPaddedSwString(const std::string &str)
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
static bool IsValidChar(unsigned char c)
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::string CentreAlignFormatting(const std::string &stringToPass, const int spacingWidth)
bool ConvertDirectoryComponent(const std::string &directoryComponent, SwTraceBuffer &swTraceBuffer)
TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void PrintCounterDirectory(ICounterDirectory &counterDirectory)
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
static bool IsValidChar(unsigned char c)