blob: ef67f0324c9d8f9fd2554e3a992bb344c464b193 [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#include "ProfilingUtils.hpp"
7
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01008#include <armnn/Version.hpp>
9
Ferran Balaguer73882172019-09-02 16:39:42 +010010#include <boost/assert.hpp>
11
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010012#include <fstream>
Matteo Martincighab173e92019-09-05 12:02:04 +010013#include <limits>
14#include <mutex>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010015
Ferran Balaguer73882172019-09-02 16:39:42 +010016namespace armnn
17{
18
19namespace profiling
20{
21
Matteo Martincighab173e92019-09-05 12:02:04 +010022uint16_t GetNextUid()
23{
24 // Static mutex for reading and modifying the global UID a single thread at the time
25 static std::mutex mutex;
26 std::unique_lock<std::mutex> lock(mutex);
27
28 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
29 // (it is used to indicate that a record is not associated with any device)
30 static uint16_t uid{ 0 };
31
32 // Check that it is possible to generate the next UID without causing an overflow
33 if (uid == std::numeric_limits<uint16_t>::max())
34 {
35 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
36 }
37
38 // Thread safe increment, the value that is incremented is the value checked for overflow,
39 // as this whole function is mutexed
40 return ++uid;
41}
42
Francis Murtagh3a161982019-09-04 15:25:02 +010043void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
44{
45 BOOST_ASSERT(buffer);
46
47 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
48 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
49 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
50 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
51 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
52 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
53 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
54 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
55}
56
Ferran Balaguer73882172019-09-02 16:39:42 +010057void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
58{
59 BOOST_ASSERT(buffer);
60
Matteo Martincigh149528e2019-09-05 12:02:04 +010061 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +010062 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
63 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
64 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
65}
66
67void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
68{
Matteo Martincigh149528e2019-09-05 12:02:04 +010069 BOOST_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +010070
Matteo Martincigh149528e2019-09-05 12:02:04 +010071 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +010072 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
73}
74
Francis Murtagh3a161982019-09-04 15:25:02 +010075uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
76{
77 BOOST_ASSERT(buffer);
78
79 uint64_t value = 0;
Matteo Martincighab173e92019-09-05 12:02:04 +010080 value = static_cast<uint64_t>(buffer[offset]);
Francis Murtagh3a161982019-09-04 15:25:02 +010081 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
82 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
83 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
84 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
85 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
86 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
87 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
88
89 return value;
90}
91
Ferran Balaguer73882172019-09-02 16:39:42 +010092uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
93{
94 BOOST_ASSERT(buffer);
95
96 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +010097 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +010098 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
99 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
100 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
101 return value;
102}
103
104uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
105{
106 BOOST_ASSERT(buffer);
107
108 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100109 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100110 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
111 return static_cast<uint16_t>(value);
112}
113
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100114std::string GetSoftwareInfo()
115{
116 return std::string("ArmNN");
117}
118
119std::string GetHardwareVersion()
120{
121 return std::string();
122}
123
124std::string GetSoftwareVersion()
125{
126 std::string armnnVersion(ARMNN_VERSION);
127 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
128 return result;
129}
130
131std::string GetProcessName()
132{
133 std::ifstream comm("/proc/self/comm");
134 std::string name;
135 getline(comm, name);
136 return name;
137}
138
Ferran Balaguer73882172019-09-02 16:39:42 +0100139} // namespace profiling
140
Matteo Martincigh149528e2019-09-05 12:02:04 +0100141} // namespace armnn