blob: 5afe6d8eade4ec702c862147a4c9cf61e278bfd5 [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
129TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
130 const std::string& label,
131 unsigned char* buffer,
132 unsigned int bufferSize,
133 unsigned int& numberOfBytesWritten);
134
Francis Murtagh3a161982019-09-04 15:25:02 +0100135class BufferExhaustion : public armnn::Exception
136{
137 using Exception::Exception;
138};
139
Ferran Balaguer73882172019-09-02 16:39:42 +0100140} // namespace profiling
141
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100142} // namespace armnn