2 // Copyright © 2017 Arm Ltd. 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 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
107 ARMNN_ASSERT(packetBuffer);
109 WriteUint64(packetBuffer->GetWritableData(), offset, value);
112 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
114 ARMNN_ASSERT(packetBuffer);
116 WriteUint32(packetBuffer->GetWritableData(), offset, value);
119 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
121 ARMNN_ASSERT(packetBuffer);
123 WriteUint16(packetBuffer->GetWritableData(), offset, value);
126 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
128 ARMNN_ASSERT(packetBuffer);
130 WriteUint8(packetBuffer->GetWritableData(), offset, value);
133 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
135 ARMNN_ASSERT(buffer);
138 for (unsigned int i = 0; i < valueSize; i++, offset++)
140 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
144 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
146 ARMNN_ASSERT(buffer);
148 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
149 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
150 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
151 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
152 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
153 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
154 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
155 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
158 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
160 ARMNN_ASSERT(buffer);
162 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
163 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
164 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
165 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
168 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
170 ARMNN_ASSERT(buffer);
172 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
173 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
176 void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
178 ARMNN_ASSERT(buffer);
180 buffer[offset] = static_cast<unsigned char>(value);
183 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
185 ARMNN_ASSERT(packetBuffer);
187 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
190 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
192 ARMNN_ASSERT(packetBuffer);
194 return ReadUint64(packetBuffer->GetReadableData(), offset);
197 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
199 ARMNN_ASSERT(packetBuffer);
201 return ReadUint32(packetBuffer->GetReadableData(), offset);
204 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
206 ARMNN_ASSERT(packetBuffer);
208 return ReadUint16(packetBuffer->GetReadableData(), offset);
211 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
213 ARMNN_ASSERT(packetBuffer);
215 return ReadUint8(packetBuffer->GetReadableData(), offset);
218 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
220 ARMNN_ASSERT(buffer);
221 ARMNN_ASSERT(outValue);
223 for (unsigned int i = 0; i < valueSize; i++, offset++)
225 outValue[i] = static_cast<uint8_t>(buffer[offset]);
229 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
231 ARMNN_ASSERT(buffer);
234 value = static_cast<uint64_t>(buffer[offset]);
235 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
236 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
237 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
238 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
239 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
240 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
241 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
246 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
248 ARMNN_ASSERT(buffer);
251 value = static_cast<uint32_t>(buffer[offset]);
252 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
253 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
254 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
258 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
260 ARMNN_ASSERT(buffer);
263 value = static_cast<uint32_t>(buffer[offset]);
264 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
265 return static_cast<uint16_t>(value);
268 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
270 ARMNN_ASSERT(buffer);
272 return buffer[offset];
275 std::string GetSoftwareInfo()
277 return std::string("ArmNN");
280 std::string GetHardwareVersion()
282 return std::string();
285 std::string GetSoftwareVersion()
287 std::string result = "Armnn " + std::to_string(ARMNN_MAJOR_VERSION) + "." + std::to_string(ARMNN_MINOR_VERSION);
291 std::string GetProcessName()
293 std::ifstream comm("/proc/self/comm");
299 // Calculate the actual length an SwString will be including the terminating null character
300 // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
301 // the size to allow the offset to be correctly updated when decoding a binary packet.
302 uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
304 std::vector<uint32_t> swTraceString;
305 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
306 unsigned int uint32_t_size = sizeof(uint32_t);
307 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
311 // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
312 SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
314 ARMNN_ASSERT(packetBuffer);
316 unsigned int uint32_t_size = sizeof(uint32_t);
318 SwTraceMessage swTraceMessage;
321 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
322 swTraceMessage.m_Id = readDeclId;
324 // SWTrace "namestring" format
325 // length of the string (first 4 bytes) + string + null terminator
327 // Check the decl_name
328 offset += uint32_t_size;
329 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
331 offset += uint32_t_size;
332 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
333 std::memcpy(swTraceStringBuffer.data(),
334 packetBuffer + offset, swTraceStringBuffer.size());
336 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
339 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
340 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
342 offset += uint32_t_size;
343 swTraceStringBuffer.resize(swTraceUINameLength - 1);
344 std::memcpy(swTraceStringBuffer.data(),
345 packetBuffer + offset, swTraceStringBuffer.size());
347 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
350 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
351 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
353 offset += uint32_t_size;
354 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
355 std::memcpy(swTraceStringBuffer.data(),
356 packetBuffer + offset, swTraceStringBuffer.size());
358 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
360 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
363 offset += CalculateSizeOfPaddedSwString(swTraceString);
364 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
366 offset += uint32_t_size;
367 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
368 std::memcpy(swTraceStringBuffer.data(),
369 packetBuffer + offset, swTraceStringBuffer.size());
371 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
372 std::stringstream stringStream(swTraceString);
374 while (std::getline(stringStream, argName, ','))
376 swTraceMessage.m_ArgNames.push_back(argName);
379 offset += CalculateSizeOfPaddedSwString(swTraceString);
381 return swTraceMessage;
384 /// Creates a timeline packet header
387 /// packetFamiliy Timeline Packet Family
388 /// packetClass Timeline Packet Class
389 /// packetType Timeline Packet Type
390 /// streamId Stream identifier
391 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
392 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
395 /// Pair of uint32_t containing word0 and word1 of the header
396 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
397 uint32_t packetClass,
400 uint32_t sequenceNumbered,
403 // Packet header word 0:
404 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
405 // 19:25 [7] packet_class: packet class
406 // 16:18 [3] packet_type: packet type
407 // 8:15 [8] reserved: all zeros
408 // 0:7 [8] stream_id: stream identifier
409 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
410 ((packetClass & 0x0000007F) << 19) |
411 ((packetType & 0x00000007) << 16) |
412 ((streamId & 0x00000007) << 0);
414 // Packet header word 1:
415 // 25:31 [7] reserved: all zeros
416 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
417 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
418 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
419 ((dataLength & 0x00FFFFFF) << 0);
421 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
424 /// Creates a packet header for the timeline messages:
427 /// * declareEventClass
428 /// * declareRelationship
432 /// dataLength The length of the message body in bytes
435 /// Pair of uint32_t containing word0 and word1 of the header
436 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
438 return CreateTimelinePacketHeader(1, // Packet family
442 0, // Sequence number
443 dataLength); // Data length
446 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
447 const std::string& label,
448 unsigned char* buffer,
449 unsigned int remainingBufferSize,
450 unsigned int& numberOfBytesWritten)
452 // Initialize the output value
453 numberOfBytesWritten = 0;
455 // Check that the given buffer is valid
456 if (buffer == nullptr || remainingBufferSize == 0)
458 return TimelinePacketStatus::BufferExhaustion;
462 unsigned int uint32_t_size = sizeof(uint32_t);
463 unsigned int uint64_t_size = sizeof(uint64_t);
465 // Convert the label into a SWTrace string
466 std::vector<uint32_t> swTraceLabel;
467 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
470 return TimelinePacketStatus::Error;
473 // Calculate the size of the SWTrace string label (in bytes)
474 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
476 // Calculate the length of the data (in bytes)
477 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
478 uint64_t_size + // Profiling GUID
479 swTraceLabelSize; // Label
481 // Check whether the timeline binary packet fits in the given buffer
482 if (timelineLabelPacketDataLength > remainingBufferSize)
484 return TimelinePacketStatus::BufferExhaustion;
487 // Initialize the offset for writing in the buffer
488 unsigned int offset = 0;
490 // Write decl_Id to the buffer
491 WriteUint32(buffer, offset, 0u);
492 offset += uint32_t_size;
494 // Write the timeline binary packet payload to the buffer
495 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
496 offset += uint64_t_size;
497 for (uint32_t swTraceLabelWord : swTraceLabel)
499 WriteUint32(buffer, offset, swTraceLabelWord); // Label
500 offset += uint32_t_size;
503 // Update the number of bytes written
504 numberOfBytesWritten = timelineLabelPacketDataLength;
506 return TimelinePacketStatus::Ok;
509 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
510 unsigned char* buffer,
511 unsigned int remainingBufferSize,
512 unsigned int& numberOfBytesWritten)
514 // Initialize the output value
515 numberOfBytesWritten = 0;
517 // Check that the given buffer is valid
518 if (buffer == nullptr || remainingBufferSize == 0)
520 return TimelinePacketStatus::BufferExhaustion;
524 unsigned int uint32_t_size = sizeof(uint32_t);
525 unsigned int uint64_t_size = sizeof(uint64_t);
527 // Calculate the length of the data (in bytes)
528 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
530 // Check whether the timeline binary packet fits in the given buffer
531 if (timelineEntityDataLength > remainingBufferSize)
533 return TimelinePacketStatus::BufferExhaustion;
536 // Initialize the offset for writing in the buffer
537 unsigned int offset = 0;
539 // Write the decl_Id to the buffer
540 WriteUint32(buffer, offset, 1u);
541 offset += uint32_t_size;
543 // Write the timeline binary packet payload to the buffer
544 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
546 // Update the number of bytes written
547 numberOfBytesWritten = timelineEntityDataLength;
549 return TimelinePacketStatus::Ok;
552 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
553 uint64_t relationshipGuid,
556 uint64_t attributeGuid,
557 unsigned char* buffer,
558 unsigned int remainingBufferSize,
559 unsigned int& numberOfBytesWritten)
561 // Initialize the output value
562 numberOfBytesWritten = 0;
564 // Check that the given buffer is valid
565 if (buffer == nullptr || remainingBufferSize == 0)
567 return TimelinePacketStatus::BufferExhaustion;
571 unsigned int uint32_t_size = sizeof(uint32_t);
572 unsigned int uint64_t_size = sizeof(uint64_t);
574 // Calculate the length of the data (in bytes)
575 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
576 uint64_t_size * 4; // Relationship GUID + Head GUID +
577 // tail GUID + attributeGuid
579 // Check whether the timeline binary fits in the given buffer
580 if (timelineRelationshipDataLength > remainingBufferSize)
582 return TimelinePacketStatus::BufferExhaustion;
585 // Initialize the offset for writing in the buffer
586 unsigned int offset = 0;
588 uint32_t relationshipTypeUint = 0;
590 switch (relationshipType)
592 case ProfilingRelationshipType::RetentionLink:
593 relationshipTypeUint = 0;
595 case ProfilingRelationshipType::ExecutionLink:
596 relationshipTypeUint = 1;
598 case ProfilingRelationshipType::DataLink:
599 relationshipTypeUint = 2;
601 case ProfilingRelationshipType::LabelLink:
602 relationshipTypeUint = 3;
605 throw InvalidArgumentException("Unknown relationship type given.");
608 // Write the timeline binary payload to the buffer
609 // decl_id of the timeline message
611 WriteUint32(buffer, offset, declId); // decl_id
612 offset += uint32_t_size;
613 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
614 offset += uint32_t_size;
615 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
616 offset += uint64_t_size;
617 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
618 offset += uint64_t_size;
619 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
620 offset += uint64_t_size;
621 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
624 // Update the number of bytes written
625 numberOfBytesWritten = timelineRelationshipDataLength;
627 return TimelinePacketStatus::Ok;
630 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
631 unsigned int remainingBufferSize,
632 unsigned int& numberOfBytesWritten)
634 // Initialize the output value
635 numberOfBytesWritten = 0;
637 // Check that the given buffer is valid
638 if (buffer == nullptr || remainingBufferSize == 0)
640 return TimelinePacketStatus::BufferExhaustion;
644 unsigned int uint8_t_size = sizeof(uint8_t);
645 unsigned int uint32_t_size = sizeof(uint32_t);
646 unsigned int uint64_t_size = sizeof(uint64_t);
648 // The payload/data of the packet consists of swtrace event definitions encoded according
649 // to the swtrace directory specification. The messages being the five defined below:
651 // | decl_id | decl_name | ui_name | arg_types | arg_names |
652 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
653 // | 0 | declareLabel | declare label | ps | guid,value |
654 // | 1 | declareEntity | declare entity | p | guid |
655 // | 2 | declareEventClass | declare event class | p | guid |
656 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
657 // | | | | | headGuid,tailGuid |
658 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
659 std::vector<std::vector<std::string>> timelineDirectoryMessages
661 { "0", "declareLabel", "declare label", "ps", "guid,value" },
662 { "1", "declareEntity", "declare entity", "p", "guid" },
663 { "2", "declareEventClass", "declare event class", "p", "guid" },
664 { "3", "declareRelationship", "declare relationship", "Ippp",
665 "relationshipType,relationshipGuid,headGuid,tailGuid" },
666 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
669 // Build the message declarations
670 std::vector<uint32_t> swTraceBuffer;
671 for (const auto& directoryComponent : timelineDirectoryMessages)
677 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
679 catch (const std::exception&)
681 return TimelinePacketStatus::Error;
683 swTraceBuffer.push_back(declId);
686 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
687 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
688 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
689 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
692 return TimelinePacketStatus::Error;
696 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
697 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
698 uint32_t_size; // Trace directory (5 messages)
700 // Calculate the timeline directory binary packet size (in bytes)
701 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
702 dataLength; // Payload
704 // Check whether the timeline directory binary packet fits in the given buffer
705 if (timelineDirectoryPacketSize > remainingBufferSize)
707 return TimelinePacketStatus::BufferExhaustion;
710 // Create packet header
711 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
713 // Initialize the offset for writing in the buffer
714 unsigned int offset = 0;
716 // Write the timeline binary packet header to the buffer
717 WriteUint32(buffer, offset, packetHeader.first);
718 offset += uint32_t_size;
719 WriteUint32(buffer, offset, packetHeader.second);
720 offset += uint32_t_size;
722 // Write the stream header
723 uint8_t streamVersion = 4;
724 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
725 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
726 switch (threadIdBytes)
728 case 4: // Typically Windows and Android
729 case 8: // Typically Linux
730 break; // Valid values
732 return TimelinePacketStatus::Error; // Invalid value
734 WriteUint8(buffer, offset, streamVersion);
735 offset += uint8_t_size;
736 WriteUint8(buffer, offset, pointerBytes);
737 offset += uint8_t_size;
738 WriteUint8(buffer, offset, threadIdBytes);
739 offset += uint8_t_size;
741 // Write the SWTrace directory
742 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
743 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
744 offset += uint32_t_size;
745 for (uint32_t i : swTraceBuffer)
747 WriteUint32(buffer, offset, i); // Message declarations
748 offset += uint32_t_size;
751 // Update the number of bytes written
752 numberOfBytesWritten = timelineDirectoryPacketSize;
754 return TimelinePacketStatus::Ok;
757 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
758 unsigned char* buffer,
759 unsigned int remainingBufferSize,
760 unsigned int& numberOfBytesWritten)
762 // Initialize the output value
763 numberOfBytesWritten = 0;
765 // Check that the given buffer is valid
766 if (buffer == nullptr || remainingBufferSize == 0)
768 return TimelinePacketStatus::BufferExhaustion;
772 unsigned int uint32_t_size = sizeof(uint32_t);
773 unsigned int uint64_t_size = sizeof(uint64_t);
775 // decl_id of the timeline message
778 // Calculate the length of the data (in bytes)
779 unsigned int dataSize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
781 // Check whether the timeline binary fits in the given buffer
782 if (dataSize > remainingBufferSize)
784 return TimelinePacketStatus::BufferExhaustion;
787 // Initialize the offset for writing in the buffer
788 unsigned int offset = 0;
790 // Write the timeline binary payload to the buffer
791 WriteUint32(buffer, offset, declId); // decl_id
792 offset += uint32_t_size;
793 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
795 // Update the number of bytes written
796 numberOfBytesWritten = dataSize;
798 return TimelinePacketStatus::Ok;
801 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
802 std::thread::id threadId,
803 uint64_t profilingGuid,
804 unsigned char* buffer,
805 unsigned int remainingBufferSize,
806 unsigned int& numberOfBytesWritten)
808 // Initialize the output value
809 numberOfBytesWritten = 0;
810 // Check that the given buffer is valid
811 if (buffer == nullptr || remainingBufferSize == 0)
813 return TimelinePacketStatus::BufferExhaustion;
817 unsigned int uint32_t_size = sizeof(uint32_t);
818 unsigned int uint64_t_size = sizeof(uint64_t);
820 // decl_id of the timeline message
823 // Calculate the length of the data (in bytes)
824 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
825 uint64_t_size + // Timestamp
826 ThreadIdSize + // Thread id
827 uint64_t_size; // Profiling GUID
829 // Check whether the timeline binary packet fits in the given buffer
830 if (timelineEventDataLength > remainingBufferSize)
832 return TimelinePacketStatus::BufferExhaustion;
835 // Initialize the offset for writing in the buffer
836 unsigned int offset = 0;
838 // Write the timeline binary payload to the buffer
839 WriteUint32(buffer, offset, declId); // decl_id
840 offset += uint32_t_size;
841 WriteUint64(buffer, offset, timestamp); // Timestamp
842 offset += uint64_t_size;
843 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
844 offset += ThreadIdSize;
845 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
846 offset += uint64_t_size;
847 // Update the number of bytes written
848 numberOfBytesWritten = timelineEventDataLength;
850 return TimelinePacketStatus::Ok;
853 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
855 std::stringstream outputStream, centrePadding;
856 int padding = spacingWidth - static_cast<int>(stringToPass.size());
858 for (int i = 0; i < padding / 2; ++i)
860 centrePadding << " ";
863 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
865 if (padding > 0 && padding %2 != 0)
870 return outputStream.str();
873 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
877 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
879 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
881 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
884 std::cout << std::string(body.size(), '-') << "\n";
888 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
892 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
894 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
896 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
899 std::cout << std::string(body.size(), '-') << "\n";
904 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
908 body.append(CentreAlignFormatting(counter->m_Name, 20));
910 body.append(CentreAlignFormatting(counter->m_Description, 50));
912 body.append(CentreAlignFormatting(counter->m_Units, 14));
914 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
916 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
918 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
920 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
922 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
924 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
926 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
930 std::cout << std::string(body.size(), '-') << "\n";
935 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
936 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
938 std::string categoryBody;
939 std::string categoryHeader;
941 categoryHeader.append(CentreAlignFormatting("Name", 20));
942 categoryHeader.append(" | ");
943 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
944 categoryHeader.append("\n");
946 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
947 categoryBody.append(" | ");
948 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
950 std::cout << "\n" << "\n";
951 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
953 std::cout << std::string(categoryHeader.size(), '=') << "\n";
955 std::cout << categoryHeader;
957 std::cout << std::string(categoryBody.size(), '-') << "\n";
959 std::cout << categoryBody;
961 std::string counterHeader;
963 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
964 counterHeader.append(" | ");
965 counterHeader.append(CentreAlignFormatting("Description", 50));
966 counterHeader.append(" | ");
967 counterHeader.append(CentreAlignFormatting("Units", 14));
968 counterHeader.append(" | ");
969 counterHeader.append(CentreAlignFormatting("UID", 6));
970 counterHeader.append(" | ");
971 counterHeader.append(CentreAlignFormatting("Max UID", 10));
972 counterHeader.append(" | ");
973 counterHeader.append(CentreAlignFormatting("Class", 8));
974 counterHeader.append(" | ");
975 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
976 counterHeader.append(" | ");
977 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
978 counterHeader.append(" | ");
979 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
980 counterHeader.append(" | ");
981 counterHeader.append(CentreAlignFormatting("Device UID", 14));
982 counterHeader.append("\n");
984 std::cout << "\n" << "\n";
985 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
986 static_cast<int>(counterHeader.size()));
988 std::cout << std::string(counterHeader.size(), '=') << "\n";
989 std::cout << counterHeader;
990 for (auto& it: category->m_Counters) {
991 auto search = counterMap.find(it);
992 if(search != counterMap.end()) {
993 PrintCounterDetails(search->second);
998 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1000 std::string devicesHeader;
1002 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1003 devicesHeader.append(" | ");
1004 devicesHeader.append(CentreAlignFormatting("UID", 13));
1005 devicesHeader.append(" | ");
1006 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1007 devicesHeader.append("\n");
1009 std::cout << "\n" << "\n";
1010 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1012 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1013 std::cout << devicesHeader;
1014 for (auto& it: counterDirectory.GetDevices()) {
1015 PrintDeviceDetails(it);
1018 std::string counterSetHeader;
1020 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1021 counterSetHeader.append(" | ");
1022 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1023 counterSetHeader.append(" | ");
1024 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1025 counterSetHeader.append("\n");
1027 std::cout << "\n" << "\n";
1028 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1030 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1032 std::cout << counterSetHeader;
1034 for (auto& it: counterDirectory.GetCounterSets()) {
1035 PrintCounterSetDetails(it);
1038 auto counters = counterDirectory.GetCounters();
1039 for (auto& it: counterDirectory.GetCategories()) {
1040 PrintCategoryDetails(it, counters);
1045 uint64_t GetTimestamp()
1047 #if USE_CLOCK_MONOTONIC_RAW
1048 using clock = MonotonicClockRaw;
1050 using clock = std::chrono::steady_clock;
1054 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
1056 return static_cast<uint64_t>(timestamp.count());
1059 Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1061 if (buffer == nullptr)
1063 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1067 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1070 uint32_t metadataIdentifier = 0;
1071 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1073 uint32_t dataLength = 0;
1074 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1076 std::unique_ptr<unsigned char[]> packetData;
1079 packetData = std::make_unique<unsigned char[]>(dataLength);
1080 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1083 return Packet(metadataIdentifier, dataLength, packetData);
1086 } // namespace profiling
1088 } // namespace armnn
1093 bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
1095 return std::memcmp(left.data(), &right, left.size()) == 0;