2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ProfilingUtils.hpp"
8 #include <armnn/Version.hpp>
9 #include <armnn/Conversion.hpp>
11 #include <boost/assert.hpp>
25 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
27 // Check that it is possible to generate the next UID without causing an overflow
32 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
33 // running multiple parallel workloads and will not provide multiple streams of data for each event)
34 if (uid == std::numeric_limits<uint16_t>::max())
36 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
40 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
41 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
42 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
44 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
50 } // Anonymous namespace
52 uint16_t GetNextUid(bool peekOnly)
54 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
55 static uint16_t uid = 1;
57 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
58 ThrowIfCantGenerateNextUid(uid);
72 std::vector<uint16_t> GetNextCounterUids(uint16_t cores)
74 // The UID used for counters only. The first valid UID is 0
75 static uint16_t counterUid = 0;
77 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
78 ThrowIfCantGenerateNextUid(counterUid, cores);
80 // Get the next counter UIDs
81 size_t counterUidsSize = cores == 0 ? 1 : cores;
82 std::vector<uint16_t> counterUids(counterUidsSize, 0);
83 for (size_t i = 0; i < counterUidsSize; i++)
85 counterUids[i] = counterUid++;
90 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
92 BOOST_ASSERT(packetBuffer);
94 WriteUint64(packetBuffer->GetWritableData(), offset, value);
97 void WriteUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint32_t value)
99 BOOST_ASSERT(packetBuffer);
101 WriteUint32(packetBuffer->GetWritableData(), offset, value);
104 void WriteUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint16_t value)
106 BOOST_ASSERT(packetBuffer);
108 WriteUint16(packetBuffer->GetWritableData(), offset, value);
111 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
113 BOOST_ASSERT(buffer);
115 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
116 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
117 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
118 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
119 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
120 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
121 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
122 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
125 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
127 BOOST_ASSERT(buffer);
129 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
130 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
131 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
132 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
135 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
137 BOOST_ASSERT(buffer);
139 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
140 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
143 uint64_t ReadUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
145 BOOST_ASSERT(packetBuffer);
147 return ReadUint64(packetBuffer->GetReadableData(), offset);
150 uint32_t ReadUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
152 BOOST_ASSERT(packetBuffer);
154 return ReadUint32(packetBuffer->GetReadableData(), offset);
157 uint16_t ReadUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
159 BOOST_ASSERT(packetBuffer);
161 return ReadUint16(packetBuffer->GetReadableData(), offset);
164 uint8_t ReadUint8(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
166 BOOST_ASSERT(packetBuffer);
168 return ReadUint8(packetBuffer->GetReadableData(), offset);
171 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
173 BOOST_ASSERT(buffer);
176 value = static_cast<uint64_t>(buffer[offset]);
177 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
178 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
179 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
180 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
181 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
182 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
183 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
188 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
190 BOOST_ASSERT(buffer);
193 value = static_cast<uint32_t>(buffer[offset]);
194 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
195 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
196 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
200 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
202 BOOST_ASSERT(buffer);
205 value = static_cast<uint32_t>(buffer[offset]);
206 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
207 return static_cast<uint16_t>(value);
210 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
212 BOOST_ASSERT(buffer);
214 return buffer[offset];
217 std::string GetSoftwareInfo()
219 return std::string("ArmNN");
222 std::string GetHardwareVersion()
224 return std::string();
227 std::string GetSoftwareVersion()
229 std::string armnnVersion(ARMNN_VERSION);
230 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
234 std::string GetProcessName()
236 std::ifstream comm("/proc/self/comm");
242 } // namespace profiling