blob: 582f7c42de1172a48334326d386c89ea5c072438 [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
Matteo Martincigh6db5f202019-09-05 12:02:04 +010026struct SwTraceCharPolicy
27{
28 static bool IsValidChar(unsigned char c)
29 {
30 // Check that the given character has ASCII 7-bit encoding
31 return c < 128;
32 }
33};
34
35struct SwTraceNameCharPolicy
36{
37 static bool IsValidChar(unsigned char c)
38 {
39 // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only
40 return c < 128 && (std::isalnum(c) || c == '_');
41 }
42};
43
44template <typename SwTracePolicy>
45bool IsValidSwTraceString(const std::string& s)
46{
47 // Check that all the characters in the given string conform to the given policy
48 return std::all_of(s.begin(), s.end(), [](unsigned char c)
49 {
50 return SwTracePolicy::IsValidChar(c);
51 });
52}
53
54template <typename SwTracePolicy>
55bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer)
56{
57 // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into
58 // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary
59
60 // Clear the output buffer
61 outputBuffer.clear();
62
63 // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars")
64 if (!IsValidSwTraceString<SwTracePolicy>(s))
65 {
66 return false;
67 }
68
69 // Prepare the output buffer
70 size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator
71 size_t uint32_t_size = sizeof(uint32_t);
72 size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0);
73 outputBuffer.resize(outBufferSize, '\0');
74
75 // Write the SWTrace string to the output buffer
76 outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size);
77 std::memcpy(outputBuffer.data() + 1, s.data(), s_size);
78
79 return true;
80}
81
82uint16_t GetNextUid(bool peekOnly = false);
83
84std::vector<uint16_t> GetNextCounterUids(uint16_t cores);
Matteo Martincighab173e92019-09-05 12:02:04 +010085
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010086void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value);
87
88void WriteUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint32_t value);
89
90void WriteUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint16_t value);
91
Francis Murtagh3a161982019-09-04 15:25:02 +010092void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
93
Ferran Balaguer73882172019-09-02 16:39:42 +010094void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value);
95
96void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value);
97
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010098uint64_t ReadUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset);
Francis Murtagh3a161982019-09-04 15:25:02 +010099
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100100uint32_t ReadUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset);
Ferran Balaguer73882172019-09-02 16:39:42 +0100101
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100102uint16_t ReadUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset);
Ferran Balaguer73882172019-09-02 16:39:42 +0100103
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100104uint8_t ReadUint8(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset);
105
106uint64_t ReadUint64(unsigned const char* buffer, unsigned int offset);
107
108uint32_t ReadUint32(unsigned const char* buffer, unsigned int offset);
109
110uint16_t ReadUint16(unsigned const char* buffer, unsigned int offset);
111
112uint8_t ReadUint8(unsigned const char* buffer, unsigned int offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100113
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100114std::string GetSoftwareInfo();
115
116std::string GetSoftwareVersion();
117
118std::string GetHardwareVersion();
119
120std::string GetProcessName();
121
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100122enum class TimelinePacketStatus
123{
124 Ok,
125 Error,
126 BufferExhaustion
127};
128
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100129enum class ProfilingRelationshipType
130{
131 RetentionLink, /// Head retains(parents) Tail
132 ExecutionLink, /// Head execution start depends on Tail execution completion
133 DataLink, /// Head uses data of Tail
134 LabelLink /// Head uses label Tail (Tail MUST be a guid of a label).
135};
136
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100137TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
138 const std::string& label,
139 unsigned char* buffer,
140 unsigned int bufferSize,
141 unsigned int& numberOfBytesWritten);
142
David Monahanf21f6062019-10-07 15:11:15 +0100143TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100144 unsigned char* buffer,
145 unsigned int bufferSize,
146 unsigned int& numberOfBytesWritten);
David Monahanf21f6062019-10-07 15:11:15 +0100147
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100148TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
149 uint64_t relationshipGuid,
150 uint64_t headGuid,
151 uint64_t tailGuid,
152 unsigned char* buffer,
153 unsigned int bufferSize,
154 unsigned int& numberOfBytesWritten);
155
Sadik Armagan784db772019-10-08 15:05:38 +0100156TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
157 unsigned int bufferSize,
158 unsigned int& numberOfBytesWritten);
159
Jan Eilers92fa15b2019-10-15 15:23:25 +0100160TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
161 unsigned char* buffer,
162 unsigned int bufferSize,
163 unsigned int& numberOfBytesWritten);
164
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100165TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
166 uint32_t threadId,
167 uint64_t profilingGuid,
168 unsigned char* buffer,
169 unsigned int bufferSize,
170 unsigned int& numberOfBytesWritten);
171
Francis Murtagh3a161982019-09-04 15:25:02 +0100172class BufferExhaustion : public armnn::Exception
173{
174 using Exception::Exception;
175};
176
Ferran Balaguer73882172019-09-02 16:39:42 +0100177} // namespace profiling
178
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100179} // namespace armnn