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