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 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
243 const std::string& label,
244 unsigned char* buffer,
245 unsigned int bufferSize,
246 unsigned int& numberOfBytesWritten)
248 // Initialize the ouput value
249 numberOfBytesWritten = 0;
251 // Check that the given buffer is valid
252 if (buffer == nullptr || bufferSize == 0)
254 return TimelinePacketStatus::BufferExhaustion;
258 unsigned int uint32_t_size = sizeof(uint32_t);
259 unsigned int uint64_t_size = sizeof(uint64_t);
261 // Convert the label into a SWTrace string
262 std::vector<uint32_t> swTraceLabel;
263 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
266 return TimelinePacketStatus::Error;
269 // Calculate the size of the SWTrace string label (in bytes)
270 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
272 // Calculate the length of the data (in bytes)
273 unsigned int timelineLabelPacketDataLength = uint64_t_size + // Profiling GUID
274 swTraceLabelSize; // Label
276 // Calculate the timeline binary packet size (in bytes)
277 unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words)
278 timelineLabelPacketDataLength; // Profiling GUID + label
280 // Check whether the timeline binary packet fits in the given buffer
281 if (timelineLabelPacketSize > bufferSize)
283 return TimelinePacketStatus::BufferExhaustion;
286 // Packet header word 0:
287 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
288 // 19:25 [7] packet_class: packet class
289 // 16:18 [3] packet_type: packet type
290 // 8:15 [8] reserved: all zeros
291 // 0:7 [8] stream_id: stream identifier
292 uint32_t packetFamily = 1;
293 uint32_t packetClass = 0;
294 uint32_t packetType = 1;
295 uint32_t streamId = 0;
296 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
297 ((packetClass & 0x0000007F) << 19) |
298 ((packetType & 0x00000007) << 16) |
299 ((streamId & 0x00000007) << 0);
301 // Packet header word 1:
302 // 25:31 [7] reserved: all zeros
303 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
304 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
305 uint32_t sequenceNumbered = 0;
306 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineLabelPacketDataLength); // Profiling GUID + label
307 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
308 ((dataLength & 0x00FFFFFF) << 0);
310 // Initialize the offset for writing in the buffer
311 unsigned int offset = 0;
313 // Write the timeline binary packet header to the buffer
314 WriteUint32(buffer, offset, packetHeaderWord0);
315 offset += uint32_t_size;
316 WriteUint32(buffer, offset, packetHeaderWord1);
317 offset += uint32_t_size;
319 // Write the timeline binary packet payload to the buffer
320 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
321 offset += uint64_t_size;
322 for (uint32_t swTraceLabelWord : swTraceLabel)
324 WriteUint32(buffer, offset, swTraceLabelWord); // Label
325 offset += uint32_t_size;
328 // Update the number of bytes written
329 numberOfBytesWritten = timelineLabelPacketSize;
331 return TimelinePacketStatus::Ok;
334 TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
335 unsigned char* buffer,
336 unsigned int bufferSize,
337 unsigned int& numberOfBytesWritten)
339 // Initialize the ouput value
340 numberOfBytesWritten = 0;
342 // Check that the given buffer is valid
343 if (buffer == nullptr || bufferSize == 0)
345 return TimelinePacketStatus::BufferExhaustion;
349 unsigned int uint32_t_size = sizeof(uint32_t);
350 unsigned int uint64_t_size = sizeof(uint64_t);
352 // Calculate the length of the data (in bytes)
353 unsigned int timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID
356 // Calculate the timeline binary packet size (in bytes)
357 unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words)
358 timelineEntityPacketDataLength; // Profiling GUID
360 // Check whether the timeline binary packet fits in the given buffer
361 if (timelineEntityPacketSize > bufferSize)
363 return TimelinePacketStatus::BufferExhaustion;
366 // Packet header word 0:
367 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
368 // 19:25 [7] packet_class: packet class
369 // 16:18 [3] packet_type: packet type
370 // 8:15 [8] reserved: all zeros
371 // 0:7 [8] stream_id: stream identifier
372 uint32_t packetFamily = 1;
373 uint32_t packetClass = 0;
374 uint32_t packetType = 1;
375 uint32_t streamId = 0;
376 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
377 ((packetClass & 0x0000007F) << 19) |
378 ((packetType & 0x00000007) << 16) |
379 ((streamId & 0x00000007) << 0);
381 // Packet header word 1:
382 // 25:31 [7] reserved: all zeros
383 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
384 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
385 uint32_t sequenceNumbered = 0;
386 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineEntityPacketDataLength); // Profiling GUID
387 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
388 ((dataLength & 0x00FFFFFF) << 0);
390 // Initialize the offset for writing in the buffer
391 unsigned int offset = 0;
393 // Write the timeline binary packet header to the buffer
394 WriteUint32(buffer, offset, packetHeaderWord0);
395 offset += uint32_t_size;
396 WriteUint32(buffer, offset, packetHeaderWord1);
397 offset += uint32_t_size;
399 // Write the timeline binary packet payload to the buffer
400 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
402 // Update the number of bytes written
403 numberOfBytesWritten = timelineEntityPacketSize;
405 return TimelinePacketStatus::Ok;
408 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
409 unsigned int bufferSize,
410 unsigned int& numberOfBytesWritten)
412 // Initialize the output value
413 numberOfBytesWritten = 0;
415 // Check that the given buffer is valid
416 if (buffer == nullptr || bufferSize == 0)
418 return TimelinePacketStatus::BufferExhaustion;
422 unsigned int uint32_t_size = sizeof(uint32_t);
424 // Packet header word 0:
425 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
426 // 19:25 [7] packet_class: packet class
427 // 16:18 [3] packet_type: packet type
428 // 8:15 [8] reserved: all zeros
429 // 0:7 [8] stream_id: stream identifier
430 uint32_t packetFamily = 1;
431 uint32_t packetClass = 0;
432 uint32_t packetType = 0;
433 uint32_t streamId = 0;
434 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
435 ((packetClass & 0x0000007F) << 19) |
436 ((packetType & 0x00000007) << 16) |
437 ((streamId & 0x00000007) << 0);
439 // the payload/data of the packet consists of swtrace event definitions encoded according
440 // to the swtrace directory specification. The messages being the five defined below:
441 // | decl_id | decl_name | ui_name | arg_types | arg_names |
442 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
443 // | 0 | declareLabel | declare label | ps | guid,value |
444 // | 1 | declareEntity | declare entity | p | guid |
445 // | 2 | declareEventClass | declare event class | p | guid |
446 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid,
447 // headGuid,tailGuid |
448 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
450 std::vector<std::vector<std::string>> timelineDirectoryMessages =
451 { {"declareLabel", "declare label", "ps", "guid,value"},
452 {"declareEntity", "declare entity", "p", "guid"},
453 {"declareEventClass", "declare event class", "p", "guid"},
454 {"declareRelationship", "declare relationship",
455 "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
456 {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"} };
458 unsigned int messagesDataLength = 0u;
459 std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
461 for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
463 messagesDataLength += uint32_t_size; // decl_id
465 std::vector<std::vector<uint32_t>> swTraceStringsVector;
466 for (const auto& label : timelineDirectoryMessage)
468 std::vector<uint32_t> swTraceString;
469 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
472 return TimelinePacketStatus::Error;
475 messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
476 swTraceStringsVector.push_back(swTraceString);
478 swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
481 // Calculate the timeline directory binary packet size (in bytes)
482 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
483 messagesDataLength; // 5 messages length
485 // Check whether the timeline directory binary packet fits in the given buffer
486 if (timelineDirectoryPacketSize > bufferSize)
488 return TimelinePacketStatus::BufferExhaustion;
491 // Packet header word 1:
492 // 25:31 [7] reserved: all zeros
493 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
494 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
495 uint32_t sequenceNumbered = 0;
496 uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
497 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
498 ((dataLength & 0x00FFFFFF) << 0);
500 // Initialize the offset for writing in the buffer
501 unsigned int offset = 0;
503 // Write the timeline binary packet header to the buffer
504 WriteUint32(buffer, offset, packetHeaderWord0);
505 offset += uint32_t_size;
506 WriteUint32(buffer, offset, packetHeaderWord1);
507 offset += uint32_t_size;
509 for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
511 // Write the timeline binary packet payload to the buffer
512 WriteUint32(buffer, offset, i); // decl_id
513 offset += uint32_t_size;
515 for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
517 for (uint32_t swTraceDeclStringWord : swTraceString)
519 WriteUint32(buffer, offset, swTraceDeclStringWord);
520 offset += uint32_t_size;
525 // Update the number of bytes written
526 numberOfBytesWritten = timelineDirectoryPacketSize;
528 return TimelinePacketStatus::Ok;
531 } // namespace profiling