2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ProfilingUtils.hpp"
8 #include "common/include/ProfilingException.hpp"
10 #include <armnn/Version.hpp>
12 #include <WallClockTimer.hpp>
14 #include <armnn/utility/Assert.hpp>
29 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
31 // Check that it is possible to generate the next UID without causing an overflow
36 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
37 // running multiple parallel workloads and will not provide multiple streams of data for each event)
38 if (uid == std::numeric_limits<uint16_t>::max())
40 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
44 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
45 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
46 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
48 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
54 } // Anonymous namespace
56 uint16_t GetNextUid(bool peekOnly)
58 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
59 static uint16_t uid = 1;
61 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
62 ThrowIfCantGenerateNextUid(uid);
76 std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores)
78 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
79 ThrowIfCantGenerateNextUid(firstUid, cores);
81 // Get the next counter UIDs
82 size_t counterUidsSize = cores == 0 ? 1 : cores;
83 std::vector<uint16_t> counterUids(counterUidsSize, 0);
84 for (size_t i = 0; i < counterUidsSize; i++)
86 counterUids[i] = firstUid++;
91 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
93 ARMNN_ASSERT(packetBuffer);
95 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
98 uint32_t ConstructHeader(uint32_t packetFamily,
101 return (( packetFamily & 0x0000003F ) << 26 )|
102 (( packetId & 0x000003FF ) << 16 );
105 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType)
107 return ((packetFamily & 0x0000003F) << 26) |
108 ((packetClass & 0x0000007F) << 19) |
109 ((packetType & 0x00000007) << 16);
112 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
114 ARMNN_ASSERT(packetBuffer);
116 WriteUint64(packetBuffer->GetWritableData(), offset, value);
119 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
121 ARMNN_ASSERT(packetBuffer);
123 WriteUint32(packetBuffer->GetWritableData(), offset, value);
126 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
128 ARMNN_ASSERT(packetBuffer);
130 WriteUint16(packetBuffer->GetWritableData(), offset, value);
133 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
135 ARMNN_ASSERT(packetBuffer);
137 WriteUint8(packetBuffer->GetWritableData(), offset, value);
140 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
142 ARMNN_ASSERT(buffer);
145 for (unsigned int i = 0; i < valueSize; i++, offset++)
147 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
151 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
153 ARMNN_ASSERT(buffer);
155 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
156 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
157 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
158 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
159 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
160 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
161 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
162 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
165 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
167 ARMNN_ASSERT(buffer);
169 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
170 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
171 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
172 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
175 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
177 ARMNN_ASSERT(buffer);
179 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
180 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
183 void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
185 ARMNN_ASSERT(buffer);
187 buffer[offset] = static_cast<unsigned char>(value);
190 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
192 ARMNN_ASSERT(packetBuffer);
194 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
197 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
199 ARMNN_ASSERT(packetBuffer);
201 return ReadUint64(packetBuffer->GetReadableData(), offset);
204 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
206 ARMNN_ASSERT(packetBuffer);
208 return ReadUint32(packetBuffer->GetReadableData(), offset);
211 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
213 ARMNN_ASSERT(packetBuffer);
215 return ReadUint16(packetBuffer->GetReadableData(), offset);
218 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
220 ARMNN_ASSERT(packetBuffer);
222 return ReadUint8(packetBuffer->GetReadableData(), offset);
225 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
227 ARMNN_ASSERT(buffer);
228 ARMNN_ASSERT(outValue);
230 for (unsigned int i = 0; i < valueSize; i++, offset++)
232 outValue[i] = static_cast<uint8_t>(buffer[offset]);
236 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
238 ARMNN_ASSERT(buffer);
241 value = static_cast<uint64_t>(buffer[offset]);
242 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
243 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
244 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
245 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
246 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
247 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
248 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
253 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
255 ARMNN_ASSERT(buffer);
258 value = static_cast<uint32_t>(buffer[offset]);
259 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
260 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
261 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
265 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
267 ARMNN_ASSERT(buffer);
270 value = static_cast<uint32_t>(buffer[offset]);
271 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
272 return static_cast<uint16_t>(value);
275 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
277 ARMNN_ASSERT(buffer);
279 return buffer[offset];
282 std::string GetSoftwareInfo()
284 return std::string("ArmNN");
287 std::string GetHardwareVersion()
289 return std::string();
292 std::string GetSoftwareVersion()
294 std::string result = "Armnn " + std::to_string(ARMNN_MAJOR_VERSION) + "." + std::to_string(ARMNN_MINOR_VERSION);
298 std::string GetProcessName()
300 std::ifstream comm("/proc/self/comm");
306 // Calculate the actual length an SwString will be including the terminating null character
307 // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
308 // the size to allow the offset to be correctly updated when decoding a binary packet.
309 uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
311 std::vector<uint32_t> swTraceString;
312 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
313 unsigned int uint32_t_size = sizeof(uint32_t);
314 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
318 // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
319 SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
321 ARMNN_ASSERT(packetBuffer);
323 unsigned int uint32_t_size = sizeof(uint32_t);
325 SwTraceMessage swTraceMessage;
328 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
329 swTraceMessage.m_Id = readDeclId;
331 // SWTrace "namestring" format
332 // length of the string (first 4 bytes) + string + null terminator
334 // Check the decl_name
335 offset += uint32_t_size;
336 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
338 offset += uint32_t_size;
339 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
340 std::memcpy(swTraceStringBuffer.data(),
341 packetBuffer + offset, swTraceStringBuffer.size());
343 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
346 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
347 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
349 offset += uint32_t_size;
350 swTraceStringBuffer.resize(swTraceUINameLength - 1);
351 std::memcpy(swTraceStringBuffer.data(),
352 packetBuffer + offset, swTraceStringBuffer.size());
354 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
357 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
358 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
360 offset += uint32_t_size;
361 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
362 std::memcpy(swTraceStringBuffer.data(),
363 packetBuffer + offset, swTraceStringBuffer.size());
365 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
367 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
370 offset += CalculateSizeOfPaddedSwString(swTraceString);
371 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
373 offset += uint32_t_size;
374 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
375 std::memcpy(swTraceStringBuffer.data(),
376 packetBuffer + offset, swTraceStringBuffer.size());
378 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
379 std::stringstream stringStream(swTraceString);
381 while (std::getline(stringStream, argName, ','))
383 swTraceMessage.m_ArgNames.push_back(argName);
386 offset += CalculateSizeOfPaddedSwString(swTraceString);
388 return swTraceMessage;
391 /// Creates a timeline packet header
394 /// packetFamiliy Timeline Packet Family
395 /// packetClass Timeline Packet Class
396 /// packetType Timeline Packet Type
397 /// streamId Stream identifier
398 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
399 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
402 /// Pair of uint32_t containing word0 and word1 of the header
403 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
404 uint32_t packetClass,
407 uint32_t sequenceNumbered,
410 // Packet header word 0:
411 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
412 // 19:25 [7] packet_class: packet class
413 // 16:18 [3] packet_type: packet type
414 // 8:15 [8] reserved: all zeros
415 // 0:7 [8] stream_id: stream identifier
416 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
417 ((packetClass & 0x0000007F) << 19) |
418 ((packetType & 0x00000007) << 16) |
419 ((streamId & 0x00000007) << 0);
421 // Packet header word 1:
422 // 25:31 [7] reserved: all zeros
423 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
424 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
425 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
426 ((dataLength & 0x00FFFFFF) << 0);
428 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
431 /// Creates a packet header for the timeline messages:
434 /// * declareEventClass
435 /// * declareRelationship
439 /// dataLength The length of the message body in bytes
442 /// Pair of uint32_t containing word0 and word1 of the header
443 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
445 return CreateTimelinePacketHeader(1, // Packet family
449 0, // Sequence number
450 dataLength); // Data length
453 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
454 const std::string& label,
455 unsigned char* buffer,
456 unsigned int remainingBufferSize,
457 unsigned int& numberOfBytesWritten)
459 // Initialize the output value
460 numberOfBytesWritten = 0;
462 // Check that the given buffer is valid
463 if (buffer == nullptr || remainingBufferSize == 0)
465 return TimelinePacketStatus::BufferExhaustion;
469 unsigned int uint32_t_size = sizeof(uint32_t);
470 unsigned int uint64_t_size = sizeof(uint64_t);
472 // Convert the label into a SWTrace string
473 std::vector<uint32_t> swTraceLabel;
474 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
477 return TimelinePacketStatus::Error;
480 // Calculate the size of the SWTrace string label (in bytes)
481 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
483 // Calculate the length of the data (in bytes)
484 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
485 uint64_t_size + // Profiling GUID
486 swTraceLabelSize; // Label
488 // Check whether the timeline binary packet fits in the given buffer
489 if (timelineLabelPacketDataLength > remainingBufferSize)
491 return TimelinePacketStatus::BufferExhaustion;
494 // Initialize the offset for writing in the buffer
495 unsigned int offset = 0;
497 // Write decl_Id to the buffer
498 WriteUint32(buffer, offset, 0u);
499 offset += uint32_t_size;
501 // Write the timeline binary packet payload to the buffer
502 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
503 offset += uint64_t_size;
504 for (uint32_t swTraceLabelWord : swTraceLabel)
506 WriteUint32(buffer, offset, swTraceLabelWord); // Label
507 offset += uint32_t_size;
510 // Update the number of bytes written
511 numberOfBytesWritten = timelineLabelPacketDataLength;
513 return TimelinePacketStatus::Ok;
516 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
517 unsigned char* buffer,
518 unsigned int remainingBufferSize,
519 unsigned int& numberOfBytesWritten)
521 // Initialize the output value
522 numberOfBytesWritten = 0;
524 // Check that the given buffer is valid
525 if (buffer == nullptr || remainingBufferSize == 0)
527 return TimelinePacketStatus::BufferExhaustion;
531 unsigned int uint32_t_size = sizeof(uint32_t);
532 unsigned int uint64_t_size = sizeof(uint64_t);
534 // Calculate the length of the data (in bytes)
535 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
537 // Check whether the timeline binary packet fits in the given buffer
538 if (timelineEntityDataLength > remainingBufferSize)
540 return TimelinePacketStatus::BufferExhaustion;
543 // Initialize the offset for writing in the buffer
544 unsigned int offset = 0;
546 // Write the decl_Id to the buffer
547 WriteUint32(buffer, offset, 1u);
548 offset += uint32_t_size;
550 // Write the timeline binary packet payload to the buffer
551 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
553 // Update the number of bytes written
554 numberOfBytesWritten = timelineEntityDataLength;
556 return TimelinePacketStatus::Ok;
559 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
560 uint64_t relationshipGuid,
563 uint64_t attributeGuid,
564 unsigned char* buffer,
565 unsigned int remainingBufferSize,
566 unsigned int& numberOfBytesWritten)
568 // Initialize the output value
569 numberOfBytesWritten = 0;
571 // Check that the given buffer is valid
572 if (buffer == nullptr || remainingBufferSize == 0)
574 return TimelinePacketStatus::BufferExhaustion;
578 unsigned int uint32_t_size = sizeof(uint32_t);
579 unsigned int uint64_t_size = sizeof(uint64_t);
581 // Calculate the length of the data (in bytes)
582 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
583 uint64_t_size * 4; // Relationship GUID + Head GUID +
584 // tail GUID + attributeGuid
586 // Check whether the timeline binary fits in the given buffer
587 if (timelineRelationshipDataLength > remainingBufferSize)
589 return TimelinePacketStatus::BufferExhaustion;
592 // Initialize the offset for writing in the buffer
593 unsigned int offset = 0;
595 uint32_t relationshipTypeUint = 0;
597 switch (relationshipType)
599 case ProfilingRelationshipType::RetentionLink:
600 relationshipTypeUint = 0;
602 case ProfilingRelationshipType::ExecutionLink:
603 relationshipTypeUint = 1;
605 case ProfilingRelationshipType::DataLink:
606 relationshipTypeUint = 2;
608 case ProfilingRelationshipType::LabelLink:
609 relationshipTypeUint = 3;
612 throw InvalidArgumentException("Unknown relationship type given.");
615 // Write the timeline binary payload to the buffer
616 // decl_id of the timeline message
618 WriteUint32(buffer, offset, declId); // decl_id
619 offset += uint32_t_size;
620 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
621 offset += uint32_t_size;
622 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
623 offset += uint64_t_size;
624 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
625 offset += uint64_t_size;
626 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
627 offset += uint64_t_size;
628 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
631 // Update the number of bytes written
632 numberOfBytesWritten = timelineRelationshipDataLength;
634 return TimelinePacketStatus::Ok;
637 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
638 unsigned int remainingBufferSize,
639 unsigned int& numberOfBytesWritten)
641 // Initialize the output value
642 numberOfBytesWritten = 0;
644 // Check that the given buffer is valid
645 if (buffer == nullptr || remainingBufferSize == 0)
647 return TimelinePacketStatus::BufferExhaustion;
651 unsigned int uint8_t_size = sizeof(uint8_t);
652 unsigned int uint32_t_size = sizeof(uint32_t);
653 unsigned int uint64_t_size = sizeof(uint64_t);
655 // The payload/data of the packet consists of swtrace event definitions encoded according
656 // to the swtrace directory specification. The messages being the five defined below:
658 // | decl_id | decl_name | ui_name | arg_types | arg_names |
659 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
660 // | 0 | declareLabel | declare label | ps | guid,value |
661 // | 1 | declareEntity | declare entity | p | guid |
662 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
663 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
664 // | | | | | headGuid,tailGuid,attributeGuid |
665 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
666 std::vector<std::vector<std::string>> timelineDirectoryMessages
668 { "0", "declareLabel", "declare label", "ps", "guid,value" },
669 { "1", "declareEntity", "declare entity", "p", "guid" },
670 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
671 { "3", "declareRelationship", "declare relationship", "Ipppp",
672 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
673 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
676 // Build the message declarations
677 std::vector<uint32_t> swTraceBuffer;
678 for (const auto& directoryComponent : timelineDirectoryMessages)
684 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
686 catch (const std::exception&)
688 return TimelinePacketStatus::Error;
690 swTraceBuffer.push_back(declId);
693 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
694 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
695 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
696 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
699 return TimelinePacketStatus::Error;
703 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
704 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
705 uint32_t_size; // Trace directory (5 messages)
707 // Calculate the timeline directory binary packet size (in bytes)
708 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
709 dataLength; // Payload
711 // Check whether the timeline directory binary packet fits in the given buffer
712 if (timelineDirectoryPacketSize > remainingBufferSize)
714 return TimelinePacketStatus::BufferExhaustion;
717 // Create packet header
718 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
720 // Initialize the offset for writing in the buffer
721 unsigned int offset = 0;
723 // Write the timeline binary packet header to the buffer
724 WriteUint32(buffer, offset, packetHeader.first);
725 offset += uint32_t_size;
726 WriteUint32(buffer, offset, packetHeader.second);
727 offset += uint32_t_size;
729 // Write the stream header
730 uint8_t streamVersion = 4;
731 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
732 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
733 switch (threadIdBytes)
735 case 4: // Typically Windows and Android
736 case 8: // Typically Linux
737 break; // Valid values
739 return TimelinePacketStatus::Error; // Invalid value
741 WriteUint8(buffer, offset, streamVersion);
742 offset += uint8_t_size;
743 WriteUint8(buffer, offset, pointerBytes);
744 offset += uint8_t_size;
745 WriteUint8(buffer, offset, threadIdBytes);
746 offset += uint8_t_size;
748 // Write the SWTrace directory
749 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
750 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
751 offset += uint32_t_size;
752 for (uint32_t i : swTraceBuffer)
754 WriteUint32(buffer, offset, i); // Message declarations
755 offset += uint32_t_size;
758 // Update the number of bytes written
759 numberOfBytesWritten = timelineDirectoryPacketSize;
761 return TimelinePacketStatus::Ok;
764 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
766 unsigned char* buffer,
767 unsigned int remainingBufferSize,
768 unsigned int& numberOfBytesWritten)
770 // Initialize the output value
771 numberOfBytesWritten = 0;
773 // Check that the given buffer is valid
774 if (buffer == nullptr || remainingBufferSize == 0)
776 return TimelinePacketStatus::BufferExhaustion;
780 unsigned int uint32_t_size = sizeof(uint32_t);
781 unsigned int uint64_t_size = sizeof(uint64_t);
783 // decl_id of the timeline message
786 // Calculate the length of the data (in bytes)
787 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
789 // Check whether the timeline binary fits in the given buffer
790 if (dataSize > remainingBufferSize)
792 return TimelinePacketStatus::BufferExhaustion;
795 // Initialize the offset for writing in the buffer
796 unsigned int offset = 0;
798 // Write the timeline binary payload to the buffer
799 WriteUint32(buffer, offset, declId); // decl_id
800 offset += uint32_t_size;
801 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
802 offset += uint64_t_size;
803 WriteUint64(buffer, offset, nameGuid); // Name GUID
805 // Update the number of bytes written
806 numberOfBytesWritten = dataSize;
808 return TimelinePacketStatus::Ok;
811 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
813 uint64_t profilingGuid,
814 unsigned char* buffer,
815 unsigned int remainingBufferSize,
816 unsigned int& numberOfBytesWritten)
818 // Initialize the output value
819 numberOfBytesWritten = 0;
820 // Check that the given buffer is valid
821 if (buffer == nullptr || remainingBufferSize == 0)
823 return TimelinePacketStatus::BufferExhaustion;
827 unsigned int uint32_t_size = sizeof(uint32_t);
828 unsigned int uint64_t_size = sizeof(uint64_t);
830 // decl_id of the timeline message
833 // Calculate the length of the data (in bytes)
834 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
835 uint64_t_size + // Timestamp
836 ThreadIdSize + // Thread id
837 uint64_t_size; // Profiling GUID
839 // Check whether the timeline binary packet fits in the given buffer
840 if (timelineEventDataLength > remainingBufferSize)
842 return TimelinePacketStatus::BufferExhaustion;
845 // Initialize the offset for writing in the buffer
846 unsigned int offset = 0;
848 // Write the timeline binary payload to the buffer
849 WriteUint32(buffer, offset, declId); // decl_id
850 offset += uint32_t_size;
851 WriteUint64(buffer, offset, timestamp); // Timestamp
852 offset += uint64_t_size;
853 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
854 offset += ThreadIdSize;
855 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
856 offset += uint64_t_size;
857 // Update the number of bytes written
858 numberOfBytesWritten = timelineEventDataLength;
860 return TimelinePacketStatus::Ok;
863 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
865 std::stringstream outputStream, centrePadding;
866 int padding = spacingWidth - static_cast<int>(stringToPass.size());
868 for (int i = 0; i < padding / 2; ++i)
870 centrePadding << " ";
873 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
875 if (padding > 0 && padding %2 != 0)
880 return outputStream.str();
883 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
887 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
889 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
891 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
894 std::cout << std::string(body.size(), '-') << "\n";
898 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
902 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
904 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
906 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
909 std::cout << std::string(body.size(), '-') << "\n";
914 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
918 body.append(CentreAlignFormatting(counter->m_Name, 20));
920 body.append(CentreAlignFormatting(counter->m_Description, 50));
922 body.append(CentreAlignFormatting(counter->m_Units, 14));
924 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
926 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
928 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
930 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
932 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
934 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
936 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
940 std::cout << std::string(body.size(), '-') << "\n";
945 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
946 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
948 std::string categoryBody;
949 std::string categoryHeader;
951 categoryHeader.append(CentreAlignFormatting("Name", 20));
952 categoryHeader.append(" | ");
953 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
954 categoryHeader.append("\n");
956 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
957 categoryBody.append(" | ");
958 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
960 std::cout << "\n" << "\n";
961 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
963 std::cout << std::string(categoryHeader.size(), '=') << "\n";
965 std::cout << categoryHeader;
967 std::cout << std::string(categoryBody.size(), '-') << "\n";
969 std::cout << categoryBody;
971 std::string counterHeader;
973 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
974 counterHeader.append(" | ");
975 counterHeader.append(CentreAlignFormatting("Description", 50));
976 counterHeader.append(" | ");
977 counterHeader.append(CentreAlignFormatting("Units", 14));
978 counterHeader.append(" | ");
979 counterHeader.append(CentreAlignFormatting("UID", 6));
980 counterHeader.append(" | ");
981 counterHeader.append(CentreAlignFormatting("Max UID", 10));
982 counterHeader.append(" | ");
983 counterHeader.append(CentreAlignFormatting("Class", 8));
984 counterHeader.append(" | ");
985 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
986 counterHeader.append(" | ");
987 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
988 counterHeader.append(" | ");
989 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
990 counterHeader.append(" | ");
991 counterHeader.append(CentreAlignFormatting("Device UID", 14));
992 counterHeader.append("\n");
994 std::cout << "\n" << "\n";
995 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
996 static_cast<int>(counterHeader.size()));
998 std::cout << std::string(counterHeader.size(), '=') << "\n";
999 std::cout << counterHeader;
1000 for (auto& it: category->m_Counters) {
1001 auto search = counterMap.find(it);
1002 if(search != counterMap.end()) {
1003 PrintCounterDetails(search->second);
1008 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1010 std::string devicesHeader;
1012 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1013 devicesHeader.append(" | ");
1014 devicesHeader.append(CentreAlignFormatting("UID", 13));
1015 devicesHeader.append(" | ");
1016 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1017 devicesHeader.append("\n");
1019 std::cout << "\n" << "\n";
1020 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1022 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1023 std::cout << devicesHeader;
1024 for (auto& it: counterDirectory.GetDevices()) {
1025 PrintDeviceDetails(it);
1028 std::string counterSetHeader;
1030 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1031 counterSetHeader.append(" | ");
1032 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1033 counterSetHeader.append(" | ");
1034 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1035 counterSetHeader.append("\n");
1037 std::cout << "\n" << "\n";
1038 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1040 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1042 std::cout << counterSetHeader;
1044 for (auto& it: counterDirectory.GetCounterSets()) {
1045 PrintCounterSetDetails(it);
1048 auto counters = counterDirectory.GetCounters();
1049 for (auto& it: counterDirectory.GetCategories()) {
1050 PrintCategoryDetails(it, counters);
1055 uint64_t GetTimestamp()
1057 #if USE_CLOCK_MONOTONIC_RAW
1058 using clock = MonotonicClockRaw;
1060 using clock = std::chrono::steady_clock;
1064 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
1066 return static_cast<uint64_t>(timestamp.count());
1069 Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1071 if (buffer == nullptr)
1073 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1077 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1080 uint32_t metadataIdentifier = 0;
1081 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1083 uint32_t dataLength = 0;
1084 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1086 std::unique_ptr<unsigned char[]> packetData;
1089 packetData = std::make_unique<unsigned char[]>(dataLength);
1090 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1093 return Packet(metadataIdentifier, dataLength, packetData);
1096 } // namespace profiling
1098 } // namespace armnn
1103 bool operator==(const std::vector<uint8_t>& left, int right)
1105 return std::memcmp(left.data(), &right, left.size()) == 0;