blob: 78ac43e0b3a63776e9adab273fa2014c75b8635b [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Francis Murtagh3a161982019-09-04 15:25:02 +01008#include <armnn/Exceptions.hpp>
9
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010010#include "IPacketBuffer.hpp"
11
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <boost/numeric/conversion/cast.hpp>
13
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014#include <algorithm>
15#include <cstring>
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010016#include <memory>
17#include <string>
18#include <vector>
Ferran Balaguer73882172019-09-02 16:39:42 +010019
20namespace armnn
21{
22
23namespace profiling
24{
25
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010026struct SwTraceMessage
27{
28 uint32_t id;
29 std::string name;
30 std::string uiName;
31 std::vector<char> argTypes;
32 std::vector<std::string> argNames;
33};
34
Matteo Martincigh6db5f202019-09-05 12:02:04 +010035struct SwTraceCharPolicy
36{
37 static bool IsValidChar(unsigned char c)
38 {
39 // Check that the given character has ASCII 7-bit encoding
40 return c < 128;
41 }
42};
43
44struct SwTraceNameCharPolicy
45{
46 static bool IsValidChar(unsigned char c)
47 {
48 // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only
49 return c < 128 && (std::isalnum(c) || c == '_');
50 }
51};
52
53template <typename SwTracePolicy>
54bool IsValidSwTraceString(const std::string& s)
55{
56 // Check that all the characters in the given string conform to the given policy
57 return std::all_of(s.begin(), s.end(), [](unsigned char c)
58 {
59 return SwTracePolicy::IsValidChar(c);
60 });
61}
62
63template <typename SwTracePolicy>
64bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer)
65{
66 // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into
67 // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary
68
69 // Clear the output buffer
70 outputBuffer.clear();
71
72 // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars")
73 if (!IsValidSwTraceString<SwTracePolicy>(s))
74 {
75 return false;
76 }
77
78 // Prepare the output buffer
79 size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator
80 size_t uint32_t_size = sizeof(uint32_t);
81 size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0);
82 outputBuffer.resize(outBufferSize, '\0');
83
84 // Write the SWTrace string to the output buffer
85 outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size);
86 std::memcpy(outputBuffer.data() + 1, s.data(), s_size);
87
88 return true;
89}
90
91uint16_t GetNextUid(bool peekOnly = false);
92
93std::vector<uint16_t> GetNextCounterUids(uint16_t cores);
Matteo Martincighab173e92019-09-05 12:02:04 +010094
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000095void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010096
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000097void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010098
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000099void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100100
Francis Murtagh3a161982019-09-04 15:25:02 +0100101void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
102
Ferran Balaguer73882172019-09-02 16:39:42 +0100103void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value);
104
105void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value);
106
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000107uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset);
Francis Murtagh3a161982019-09-04 15:25:02 +0100108
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000109uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset);
Ferran Balaguer73882172019-09-02 16:39:42 +0100110
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000111uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset);
Ferran Balaguer73882172019-09-02 16:39:42 +0100112
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000113uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100114
115uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset);
116
117uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset);
118
119uint16_t ReadUint16(unsigned const char* buffer, unsigned int offset);
120
121uint8_t ReadUint8(unsigned const char* buffer, unsigned int offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100122
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100123std::string GetSoftwareInfo();
124
125std::string GetSoftwareVersion();
126
127std::string GetHardwareVersion();
128
129std::string GetProcessName();
130
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100131enum class TimelinePacketStatus
132{
133 Ok,
134 Error,
135 BufferExhaustion
136};
137
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100138enum class ProfilingRelationshipType
139{
140 RetentionLink, /// Head retains(parents) Tail
141 ExecutionLink, /// Head execution start depends on Tail execution completion
142 DataLink, /// Head uses data of Tail
143 LabelLink /// Head uses label Tail (Tail MUST be a guid of a label).
144};
145
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100146uint32_t CalculateSizeOfPaddedSwString(const std::string& str);
147
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000148SwTraceMessage ReadSwTraceMessage(const IPacketBufferPtr& packetBuffer, unsigned int& offset);
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100149
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100150TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
151 const std::string& label,
152 unsigned char* buffer,
153 unsigned int bufferSize,
154 unsigned int& numberOfBytesWritten);
155
David Monahanf21f6062019-10-07 15:11:15 +0100156TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100157 unsigned char* buffer,
158 unsigned int bufferSize,
159 unsigned int& numberOfBytesWritten);
David Monahanf21f6062019-10-07 15:11:15 +0100160
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100161TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
162 uint64_t relationshipGuid,
163 uint64_t headGuid,
164 uint64_t tailGuid,
165 unsigned char* buffer,
166 unsigned int bufferSize,
167 unsigned int& numberOfBytesWritten);
168
Sadik Armagan784db772019-10-08 15:05:38 +0100169TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
170 unsigned int bufferSize,
171 unsigned int& numberOfBytesWritten);
172
Jan Eilers92fa15b2019-10-15 15:23:25 +0100173TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
174 unsigned char* buffer,
175 unsigned int bufferSize,
176 unsigned int& numberOfBytesWritten);
177
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100178TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
179 uint32_t threadId,
180 uint64_t profilingGuid,
181 unsigned char* buffer,
182 unsigned int bufferSize,
183 unsigned int& numberOfBytesWritten);
184
Francis Murtagh3a161982019-09-04 15:25:02 +0100185class BufferExhaustion : public armnn::Exception
186{
187 using Exception::Exception;
188};
189
Ferran Balaguer73882172019-09-02 16:39:42 +0100190} // namespace profiling
191
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100192} // namespace armnn