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,
320 unsigned int& offset,
321 const unsigned int& packetLength)
323 ARMNN_ASSERT(packetBuffer);
325 unsigned int uint32_t_size = sizeof(uint32_t);
327 SwTraceMessage swTraceMessage;
330 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
331 swTraceMessage.m_Id = readDeclId;
333 // SWTrace "namestring" format
334 // length of the string (first 4 bytes) + string + null terminator
336 // Check the decl_name
337 offset += uint32_t_size;
338 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
340 if (swTraceDeclNameLength == 0 || swTraceDeclNameLength > packetLength)
342 throw RuntimeException("Error swTraceDeclNameLength is an invalid size", CHECK_LOCATION());
345 offset += uint32_t_size;
346 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
347 std::memcpy(swTraceStringBuffer.data(),
348 packetBuffer + offset, swTraceStringBuffer.size());
350 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
353 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
354 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
356 if (swTraceUINameLength == 0 || swTraceUINameLength > packetLength)
358 throw RuntimeException("Error swTraceUINameLength is an invalid size", CHECK_LOCATION());
361 offset += uint32_t_size;
362 swTraceStringBuffer.resize(swTraceUINameLength - 1);
363 std::memcpy(swTraceStringBuffer.data(),
364 packetBuffer + offset, swTraceStringBuffer.size());
366 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
369 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
370 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
372 if (swTraceArgTypesLength == 0 || swTraceArgTypesLength > packetLength)
374 throw RuntimeException("Error swTraceArgTypesLength is an invalid size", CHECK_LOCATION());
377 offset += uint32_t_size;
378 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
379 std::memcpy(swTraceStringBuffer.data(),
380 packetBuffer + offset, swTraceStringBuffer.size());
382 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
384 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
387 offset += CalculateSizeOfPaddedSwString(swTraceString);
388 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
390 if (swTraceArgNamesLength == 0 || swTraceArgNamesLength > packetLength)
392 throw RuntimeException("Error swTraceArgNamesLength is an invalid size", CHECK_LOCATION());
395 offset += uint32_t_size;
396 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
397 std::memcpy(swTraceStringBuffer.data(),
398 packetBuffer + offset, swTraceStringBuffer.size());
400 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
401 std::stringstream stringStream(swTraceString);
403 while (std::getline(stringStream, argName, ','))
405 swTraceMessage.m_ArgNames.push_back(argName);
408 offset += CalculateSizeOfPaddedSwString(swTraceString);
410 return swTraceMessage;
413 /// Creates a timeline packet header
416 /// packetFamiliy Timeline Packet Family
417 /// packetClass Timeline Packet Class
418 /// packetType Timeline Packet Type
419 /// streamId Stream identifier
420 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
421 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
424 /// Pair of uint32_t containing word0 and word1 of the header
425 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
426 uint32_t packetClass,
429 uint32_t sequenceNumbered,
432 // Packet header word 0:
433 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
434 // 19:25 [7] packet_class: packet class
435 // 16:18 [3] packet_type: packet type
436 // 8:15 [8] reserved: all zeros
437 // 0:7 [8] stream_id: stream identifier
438 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
439 ((packetClass & 0x0000007F) << 19) |
440 ((packetType & 0x00000007) << 16) |
441 ((streamId & 0x00000007) << 0);
443 // Packet header word 1:
444 // 25:31 [7] reserved: all zeros
445 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
446 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
447 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
448 ((dataLength & 0x00FFFFFF) << 0);
450 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
453 /// Creates a packet header for the timeline messages:
456 /// * declareEventClass
457 /// * declareRelationship
461 /// dataLength The length of the message body in bytes
464 /// Pair of uint32_t containing word0 and word1 of the header
465 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
467 return CreateTimelinePacketHeader(1, // Packet family
471 0, // Sequence number
472 dataLength); // Data length
475 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
476 const std::string& label,
477 unsigned char* buffer,
478 unsigned int remainingBufferSize,
479 unsigned int& numberOfBytesWritten)
481 // Initialize the output value
482 numberOfBytesWritten = 0;
484 // Check that the given buffer is valid
485 if (buffer == nullptr || remainingBufferSize == 0)
487 return TimelinePacketStatus::BufferExhaustion;
491 unsigned int uint32_t_size = sizeof(uint32_t);
492 unsigned int uint64_t_size = sizeof(uint64_t);
494 // Convert the label into a SWTrace string
495 std::vector<uint32_t> swTraceLabel;
496 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
499 return TimelinePacketStatus::Error;
502 // Calculate the size of the SWTrace string label (in bytes)
503 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
505 // Calculate the length of the data (in bytes)
506 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
507 uint64_t_size + // Profiling GUID
508 swTraceLabelSize; // Label
510 // Check whether the timeline binary packet fits in the given buffer
511 if (timelineLabelPacketDataLength > remainingBufferSize)
513 return TimelinePacketStatus::BufferExhaustion;
516 // Initialize the offset for writing in the buffer
517 unsigned int offset = 0;
519 // Write decl_Id to the buffer
520 WriteUint32(buffer, offset, 0u);
521 offset += uint32_t_size;
523 // Write the timeline binary packet payload to the buffer
524 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
525 offset += uint64_t_size;
526 for (uint32_t swTraceLabelWord : swTraceLabel)
528 WriteUint32(buffer, offset, swTraceLabelWord); // Label
529 offset += uint32_t_size;
532 // Update the number of bytes written
533 numberOfBytesWritten = timelineLabelPacketDataLength;
535 return TimelinePacketStatus::Ok;
538 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
539 unsigned char* buffer,
540 unsigned int remainingBufferSize,
541 unsigned int& numberOfBytesWritten)
543 // Initialize the output value
544 numberOfBytesWritten = 0;
546 // Check that the given buffer is valid
547 if (buffer == nullptr || remainingBufferSize == 0)
549 return TimelinePacketStatus::BufferExhaustion;
553 unsigned int uint32_t_size = sizeof(uint32_t);
554 unsigned int uint64_t_size = sizeof(uint64_t);
556 // Calculate the length of the data (in bytes)
557 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
559 // Check whether the timeline binary packet fits in the given buffer
560 if (timelineEntityDataLength > remainingBufferSize)
562 return TimelinePacketStatus::BufferExhaustion;
565 // Initialize the offset for writing in the buffer
566 unsigned int offset = 0;
568 // Write the decl_Id to the buffer
569 WriteUint32(buffer, offset, 1u);
570 offset += uint32_t_size;
572 // Write the timeline binary packet payload to the buffer
573 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
575 // Update the number of bytes written
576 numberOfBytesWritten = timelineEntityDataLength;
578 return TimelinePacketStatus::Ok;
581 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
582 uint64_t relationshipGuid,
585 uint64_t attributeGuid,
586 unsigned char* buffer,
587 unsigned int remainingBufferSize,
588 unsigned int& numberOfBytesWritten)
590 // Initialize the output value
591 numberOfBytesWritten = 0;
593 // Check that the given buffer is valid
594 if (buffer == nullptr || remainingBufferSize == 0)
596 return TimelinePacketStatus::BufferExhaustion;
600 unsigned int uint32_t_size = sizeof(uint32_t);
601 unsigned int uint64_t_size = sizeof(uint64_t);
603 // Calculate the length of the data (in bytes)
604 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
605 uint64_t_size * 4; // Relationship GUID + Head GUID +
606 // tail GUID + attributeGuid
608 // Check whether the timeline binary fits in the given buffer
609 if (timelineRelationshipDataLength > remainingBufferSize)
611 return TimelinePacketStatus::BufferExhaustion;
614 // Initialize the offset for writing in the buffer
615 unsigned int offset = 0;
617 uint32_t relationshipTypeUint = 0;
619 switch (relationshipType)
621 case ProfilingRelationshipType::RetentionLink:
622 relationshipTypeUint = 0;
624 case ProfilingRelationshipType::ExecutionLink:
625 relationshipTypeUint = 1;
627 case ProfilingRelationshipType::DataLink:
628 relationshipTypeUint = 2;
630 case ProfilingRelationshipType::LabelLink:
631 relationshipTypeUint = 3;
634 throw InvalidArgumentException("Unknown relationship type given.");
637 // Write the timeline binary payload to the buffer
638 // decl_id of the timeline message
640 WriteUint32(buffer, offset, declId); // decl_id
641 offset += uint32_t_size;
642 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
643 offset += uint32_t_size;
644 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
645 offset += uint64_t_size;
646 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
647 offset += uint64_t_size;
648 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
649 offset += uint64_t_size;
650 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
653 // Update the number of bytes written
654 numberOfBytesWritten = timelineRelationshipDataLength;
656 return TimelinePacketStatus::Ok;
659 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
660 unsigned int remainingBufferSize,
661 unsigned int& numberOfBytesWritten)
663 // Initialize the output value
664 numberOfBytesWritten = 0;
666 // Check that the given buffer is valid
667 if (buffer == nullptr || remainingBufferSize == 0)
669 return TimelinePacketStatus::BufferExhaustion;
673 unsigned int uint8_t_size = sizeof(uint8_t);
674 unsigned int uint32_t_size = sizeof(uint32_t);
675 unsigned int uint64_t_size = sizeof(uint64_t);
677 // The payload/data of the packet consists of swtrace event definitions encoded according
678 // to the swtrace directory specification. The messages being the five defined below:
680 // | decl_id | decl_name | ui_name | arg_types | arg_names |
681 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
682 // | 0 | declareLabel | declare label | ps | guid,value |
683 // | 1 | declareEntity | declare entity | p | guid |
684 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
685 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
686 // | | | | | headGuid,tailGuid,attributeGuid |
687 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
688 std::vector<std::vector<std::string>> timelineDirectoryMessages
690 { "0", "declareLabel", "declare label", "ps", "guid,value" },
691 { "1", "declareEntity", "declare entity", "p", "guid" },
692 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
693 { "3", "declareRelationship", "declare relationship", "Ipppp",
694 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
695 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
698 // Build the message declarations
699 std::vector<uint32_t> swTraceBuffer;
700 for (const auto& directoryComponent : timelineDirectoryMessages)
706 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
708 catch (const std::exception&)
710 return TimelinePacketStatus::Error;
712 swTraceBuffer.push_back(declId);
715 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
716 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
717 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
718 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
721 return TimelinePacketStatus::Error;
725 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
726 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
727 uint32_t_size; // Trace directory (5 messages)
729 // Calculate the timeline directory binary packet size (in bytes)
730 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
731 dataLength; // Payload
733 // Check whether the timeline directory binary packet fits in the given buffer
734 if (timelineDirectoryPacketSize > remainingBufferSize)
736 return TimelinePacketStatus::BufferExhaustion;
739 // Create packet header
740 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
742 // Initialize the offset for writing in the buffer
743 unsigned int offset = 0;
745 // Write the timeline binary packet header to the buffer
746 WriteUint32(buffer, offset, packetHeader.first);
747 offset += uint32_t_size;
748 WriteUint32(buffer, offset, packetHeader.second);
749 offset += uint32_t_size;
751 // Write the stream header
752 uint8_t streamVersion = 4;
753 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
754 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
755 switch (threadIdBytes)
757 case 4: // Typically Windows and Android
758 case 8: // Typically Linux
759 break; // Valid values
761 return TimelinePacketStatus::Error; // Invalid value
763 WriteUint8(buffer, offset, streamVersion);
764 offset += uint8_t_size;
765 WriteUint8(buffer, offset, pointerBytes);
766 offset += uint8_t_size;
767 WriteUint8(buffer, offset, threadIdBytes);
768 offset += uint8_t_size;
770 // Write the SWTrace directory
771 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
772 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
773 offset += uint32_t_size;
774 for (uint32_t i : swTraceBuffer)
776 WriteUint32(buffer, offset, i); // Message declarations
777 offset += uint32_t_size;
780 // Update the number of bytes written
781 numberOfBytesWritten = timelineDirectoryPacketSize;
783 return TimelinePacketStatus::Ok;
786 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
788 unsigned char* buffer,
789 unsigned int remainingBufferSize,
790 unsigned int& numberOfBytesWritten)
792 // Initialize the output value
793 numberOfBytesWritten = 0;
795 // Check that the given buffer is valid
796 if (buffer == nullptr || remainingBufferSize == 0)
798 return TimelinePacketStatus::BufferExhaustion;
802 unsigned int uint32_t_size = sizeof(uint32_t);
803 unsigned int uint64_t_size = sizeof(uint64_t);
805 // decl_id of the timeline message
808 // Calculate the length of the data (in bytes)
809 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
811 // Check whether the timeline binary fits in the given buffer
812 if (dataSize > remainingBufferSize)
814 return TimelinePacketStatus::BufferExhaustion;
817 // Initialize the offset for writing in the buffer
818 unsigned int offset = 0;
820 // Write the timeline binary payload to the buffer
821 WriteUint32(buffer, offset, declId); // decl_id
822 offset += uint32_t_size;
823 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
824 offset += uint64_t_size;
825 WriteUint64(buffer, offset, nameGuid); // Name GUID
827 // Update the number of bytes written
828 numberOfBytesWritten = dataSize;
830 return TimelinePacketStatus::Ok;
833 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
835 uint64_t profilingGuid,
836 unsigned char* buffer,
837 unsigned int remainingBufferSize,
838 unsigned int& numberOfBytesWritten)
840 // Initialize the output value
841 numberOfBytesWritten = 0;
842 // Check that the given buffer is valid
843 if (buffer == nullptr || remainingBufferSize == 0)
845 return TimelinePacketStatus::BufferExhaustion;
849 unsigned int uint32_t_size = sizeof(uint32_t);
850 unsigned int uint64_t_size = sizeof(uint64_t);
852 // decl_id of the timeline message
855 // Calculate the length of the data (in bytes)
856 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
857 uint64_t_size + // Timestamp
858 ThreadIdSize + // Thread id
859 uint64_t_size; // Profiling GUID
861 // Check whether the timeline binary packet fits in the given buffer
862 if (timelineEventDataLength > remainingBufferSize)
864 return TimelinePacketStatus::BufferExhaustion;
867 // Initialize the offset for writing in the buffer
868 unsigned int offset = 0;
870 // Write the timeline binary payload to the buffer
871 WriteUint32(buffer, offset, declId); // decl_id
872 offset += uint32_t_size;
873 WriteUint64(buffer, offset, timestamp); // Timestamp
874 offset += uint64_t_size;
875 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
876 offset += ThreadIdSize;
877 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
878 offset += uint64_t_size;
879 // Update the number of bytes written
880 numberOfBytesWritten = timelineEventDataLength;
882 return TimelinePacketStatus::Ok;
885 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
887 std::stringstream outputStream, centrePadding;
888 int padding = spacingWidth - static_cast<int>(stringToPass.size());
890 for (int i = 0; i < padding / 2; ++i)
892 centrePadding << " ";
895 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
897 if (padding > 0 && padding %2 != 0)
902 return outputStream.str();
905 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
909 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
911 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
913 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
916 std::cout << std::string(body.size(), '-') << "\n";
920 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
924 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
926 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
928 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
931 std::cout << std::string(body.size(), '-') << "\n";
936 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
940 body.append(CentreAlignFormatting(counter->m_Name, 20));
942 body.append(CentreAlignFormatting(counter->m_Description, 50));
944 body.append(CentreAlignFormatting(counter->m_Units, 14));
946 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
948 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
950 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
952 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
954 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
956 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
958 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
962 std::cout << std::string(body.size(), '-') << "\n";
967 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
968 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
970 std::string categoryBody;
971 std::string categoryHeader;
973 categoryHeader.append(CentreAlignFormatting("Name", 20));
974 categoryHeader.append(" | ");
975 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
976 categoryHeader.append("\n");
978 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
979 categoryBody.append(" | ");
980 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
982 std::cout << "\n" << "\n";
983 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
985 std::cout << std::string(categoryHeader.size(), '=') << "\n";
987 std::cout << categoryHeader;
989 std::cout << std::string(categoryBody.size(), '-') << "\n";
991 std::cout << categoryBody;
993 std::string counterHeader;
995 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
996 counterHeader.append(" | ");
997 counterHeader.append(CentreAlignFormatting("Description", 50));
998 counterHeader.append(" | ");
999 counterHeader.append(CentreAlignFormatting("Units", 14));
1000 counterHeader.append(" | ");
1001 counterHeader.append(CentreAlignFormatting("UID", 6));
1002 counterHeader.append(" | ");
1003 counterHeader.append(CentreAlignFormatting("Max UID", 10));
1004 counterHeader.append(" | ");
1005 counterHeader.append(CentreAlignFormatting("Class", 8));
1006 counterHeader.append(" | ");
1007 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
1008 counterHeader.append(" | ");
1009 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
1010 counterHeader.append(" | ");
1011 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
1012 counterHeader.append(" | ");
1013 counterHeader.append(CentreAlignFormatting("Device UID", 14));
1014 counterHeader.append("\n");
1016 std::cout << "\n" << "\n";
1017 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
1018 static_cast<int>(counterHeader.size()));
1020 std::cout << std::string(counterHeader.size(), '=') << "\n";
1021 std::cout << counterHeader;
1022 for (auto& it: category->m_Counters) {
1023 auto search = counterMap.find(it);
1024 if(search != counterMap.end()) {
1025 PrintCounterDetails(search->second);
1030 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1032 std::string devicesHeader;
1034 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1035 devicesHeader.append(" | ");
1036 devicesHeader.append(CentreAlignFormatting("UID", 13));
1037 devicesHeader.append(" | ");
1038 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1039 devicesHeader.append("\n");
1041 std::cout << "\n" << "\n";
1042 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1044 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1045 std::cout << devicesHeader;
1046 for (auto& it: counterDirectory.GetDevices()) {
1047 PrintDeviceDetails(it);
1050 std::string counterSetHeader;
1052 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1053 counterSetHeader.append(" | ");
1054 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1055 counterSetHeader.append(" | ");
1056 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1057 counterSetHeader.append("\n");
1059 std::cout << "\n" << "\n";
1060 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1062 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1064 std::cout << counterSetHeader;
1066 for (auto& it: counterDirectory.GetCounterSets()) {
1067 PrintCounterSetDetails(it);
1070 auto counters = counterDirectory.GetCounters();
1071 for (auto& it: counterDirectory.GetCategories()) {
1072 PrintCategoryDetails(it, counters);
1077 uint64_t GetTimestamp()
1079 #if USE_CLOCK_MONOTONIC_RAW
1080 using clock = MonotonicClockRaw;
1082 using clock = std::chrono::steady_clock;
1086 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
1088 return static_cast<uint64_t>(timestamp.count());
1091 Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1093 if (buffer == nullptr)
1095 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1099 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1102 uint32_t metadataIdentifier = 0;
1103 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1105 uint32_t dataLength = 0;
1106 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1108 std::unique_ptr<unsigned char[]> packetData;
1111 packetData = std::make_unique<unsigned char[]>(dataLength);
1112 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1115 return Packet(metadataIdentifier, dataLength, packetData);
1118 } // namespace profiling
1120 } // namespace armnn
1125 bool operator==(const std::vector<uint8_t>& left, int right)
1127 return std::memcmp(left.data(), &right, left.size()) == 0;