ArmNN  NotReleased
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  size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0);
112  outputBuffer.resize(outBufferSize, '\0');
113 
114  // Write the SWTrace string to the output buffer
115  outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size);
116  std::memcpy(outputBuffer.data() + 1, s.data(), s_size);
117 
118  return true;
119 }
120 
121 template <typename SwTracePolicy,
122  typename SwTraceBuffer = std::vector<uint32_t>>
123 bool ConvertDirectoryComponent(const std::string& directoryComponent, SwTraceBuffer& swTraceBuffer)
124 {
125  // Convert the directory component using the given policy
126  SwTraceBuffer tempSwTraceBuffer;
127  bool result = StringToSwTraceString<SwTracePolicy>(directoryComponent, tempSwTraceBuffer);
128  if (!result)
129  {
130  return false;
131  }
132 
133  swTraceBuffer.insert(swTraceBuffer.end(), tempSwTraceBuffer.begin(), tempSwTraceBuffer.end());
134 
135  return true;
136 }
137 
138 uint16_t GetNextUid(bool peekOnly = false);
139 
140 std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores);
141 
142 void WriteBytes(const IPacketBuffer& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize);
143 
144 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId);
145 
146 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType);
147 
148 void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value);
149 
150 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value);
151 
152 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value);
153 
154 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value);
155 
156 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize);
157 
158 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
159 
160 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value);
161 
162 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value);
163 
164 void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value);
165 
166 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
167 
168 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset);
169 
170 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset);
171 
172 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset);
173 
174 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset);
175 
176 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[]);
177 
178 uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset);
179 
180 uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset);
181 
182 uint16_t ReadUint16(unsigned const char* buffer, unsigned int offset);
183 
184 uint8_t ReadUint8(unsigned const char* buffer, unsigned int offset);
185 
186 std::string GetSoftwareInfo();
187 
188 std::string GetSoftwareVersion();
189 
190 std::string GetHardwareVersion();
191 
192 std::string GetProcessName();
193 
195 {
196  Ok,
197  Error,
199 };
200 
201 uint32_t CalculateSizeOfPaddedSwString(const std::string& str);
202 
203 SwTraceMessage ReadSwTraceMessage(const unsigned char*, unsigned int& offset);
204 
206  const std::string& label,
207  unsigned char* buffer,
208  unsigned int bufferSize,
209  unsigned int& numberOfBytesWritten);
210 
212  unsigned char* buffer,
213  unsigned int bufferSize,
214  unsigned int& numberOfBytesWritten);
215 
217  uint64_t relationshipGuid,
218  uint64_t headGuid,
219  uint64_t tailGuid,
220  unsigned char* buffer,
221  unsigned int bufferSize,
222  unsigned int& numberOfBytesWritten);
223 
225  unsigned int bufferSize,
226  unsigned int& numberOfBytesWritten);
227 
229  unsigned char* buffer,
230  unsigned int bufferSize,
231  unsigned int& numberOfBytesWritten);
232 
234  std::thread::id threadId,
235  uint64_t profilingGuid,
236  unsigned char* buffer,
237  unsigned int bufferSize,
238  unsigned int& numberOfBytesWritten);
239 
240 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth);
241 
242 void PrintCounterDirectory(ICounterDirectory& counterDirectory);
243 
244 class BufferExhaustion : public armnn::Exception
245 {
246  using Exception::Exception;
247 };
248 
249 uint64_t GetTimestamp();
250 
251 } // namespace profiling
252 
253 } // namespace armnn
254 
255 namespace std
256 {
257 
258 bool operator==(const std::vector<uint8_t>& left, std::thread::id right);
259 
260 } // namespace std
std::string CentreAlignFormatting(const std::string &stringToPass, const int spacingWidth)
static bool IsValidChar(unsigned char c)
void PrintCounterDirectory(ICounterDirectory &counterDirectory)
uint64_t ReadUint64(const IPacketBufferPtr &packetBuffer, unsigned int offset)
std::string GetSoftwareVersion()
TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
void WriteUint64(const std::unique_ptr< IPacketBuffer > &packetBuffer, unsigned int offset, uint64_t value)
uint16_t GetNextUid(bool peekOnly)
void WriteUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint16_t value)
uint16_t ReadUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset)
std::vector< std::string > m_ArgNames
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
std::string GetSoftwareInfo()
TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
std::string GetProcessName()
static bool IsValidChar(unsigned char c)
std::string GetHardwareVersion()
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
void WriteUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint8_t value)
Exception(const std::string &message)
Definition: Exceptions.cpp:12
bool ConvertDirectoryComponent(const std::string &directoryComponent, SwTraceBuffer &swTraceBuffer)
bool operator==(const armnn::DataLayout &dataLayout, const DataLayoutIndexed &indexed)
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
void WriteBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, const void *value, unsigned int valueSize)
TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
void ReadBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
bool StringToSwTraceString(const std::string &s, std::vector< uint32_t > &outputBuffer)
uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId)
bool IsValidSwTraceString(const std::string &s)
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int bufferSize, unsigned int &numberOfBytesWritten)
SwTraceMessage ReadSwTraceMessage(const unsigned char *packetBuffer, unsigned int &offset)
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)
uint32_t CalculateSizeOfPaddedSwString(const std::string &str)
static bool IsValidChar(unsigned char c)