blob: 793f94d91b09e1d5db57459579c878fdd42e01ce [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
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <boost/numeric/conversion/cast.hpp>
11
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010012#include <string>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010013#include <vector>
14#include <algorithm>
15#include <cstring>
Ferran Balaguer73882172019-09-02 16:39:42 +010016
17namespace armnn
18{
19
20namespace profiling
21{
22
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023struct SwTraceCharPolicy
24{
25 static bool IsValidChar(unsigned char c)
26 {
27 // Check that the given character has ASCII 7-bit encoding
28 return c < 128;
29 }
30};
31
32struct SwTraceNameCharPolicy
33{
34 static bool IsValidChar(unsigned char c)
35 {
36 // Check that the given character has ASCII 7-bit encoding, alpha-numeric and underscore only
37 return c < 128 && (std::isalnum(c) || c == '_');
38 }
39};
40
41template <typename SwTracePolicy>
42bool IsValidSwTraceString(const std::string& s)
43{
44 // Check that all the characters in the given string conform to the given policy
45 return std::all_of(s.begin(), s.end(), [](unsigned char c)
46 {
47 return SwTracePolicy::IsValidChar(c);
48 });
49}
50
51template <typename SwTracePolicy>
52bool StringToSwTraceString(const std::string& s, std::vector<uint32_t>& outputBuffer)
53{
54 // Converts the given string to an SWTrace "string" (i.e. a string of "chars"), and writes it into
55 // the given buffer including the null-terminator. It also pads it to the next uint32_t if necessary
56
57 // Clear the output buffer
58 outputBuffer.clear();
59
60 // Check that the given string is a valid SWTrace "string" (i.e. a string of "chars")
61 if (!IsValidSwTraceString<SwTracePolicy>(s))
62 {
63 return false;
64 }
65
66 // Prepare the output buffer
67 size_t s_size = s.size() + 1; // The size of the string (in chars) plus the null-terminator
68 size_t uint32_t_size = sizeof(uint32_t);
69 size_t outBufferSize = 1 + s_size / uint32_t_size + (s_size % uint32_t_size != 0 ? 1 : 0);
70 outputBuffer.resize(outBufferSize, '\0');
71
72 // Write the SWTrace string to the output buffer
73 outputBuffer[0] = boost::numeric_cast<uint32_t>(s_size);
74 std::memcpy(outputBuffer.data() + 1, s.data(), s_size);
75
76 return true;
77}
78
79uint16_t GetNextUid(bool peekOnly = false);
80
81std::vector<uint16_t> GetNextCounterUids(uint16_t cores);
Matteo Martincighab173e92019-09-05 12:02:04 +010082
Francis Murtagh3a161982019-09-04 15:25:02 +010083void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value);
84
Ferran Balaguer73882172019-09-02 16:39:42 +010085void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value);
86
87void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value);
88
Francis Murtagh3a161982019-09-04 15:25:02 +010089uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset);
90
Ferran Balaguer73882172019-09-02 16:39:42 +010091uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset);
92
93uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset);
94
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010095std::string GetSoftwareInfo();
96
97std::string GetSoftwareVersion();
98
99std::string GetHardwareVersion();
100
101std::string GetProcessName();
102
Francis Murtagh3a161982019-09-04 15:25:02 +0100103class BufferExhaustion : public armnn::Exception
104{
105 using Exception::Exception;
106};
107
Ferran Balaguer73882172019-09-02 16:39:42 +0100108} // namespace profiling
109
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100110} // namespace armnn