2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ProfilingUtils.hpp"
8 #include <armnn/Version.hpp>
9 #include <armnn/Conversion.hpp>
11 #include <boost/assert.hpp>
26 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
28 // Check that it is possible to generate the next UID without causing an overflow
33 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
34 // running multiple parallel workloads and will not provide multiple streams of data for each event)
35 if (uid == std::numeric_limits<uint16_t>::max())
37 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
41 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
42 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
43 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
45 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
51 } // Anonymous namespace
53 uint16_t GetNextUid(bool peekOnly)
55 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
56 static uint16_t uid = 1;
58 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
59 ThrowIfCantGenerateNextUid(uid);
73 std::vector<uint16_t> GetNextCounterUids(uint16_t cores)
75 // The UID used for counters only. The first valid UID is 0
76 static uint16_t counterUid = 0;
78 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
79 ThrowIfCantGenerateNextUid(counterUid, 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] = counterUid++;
91 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
93 BOOST_ASSERT(packetBuffer);
95 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
98 uint32_t ConstructHeader(uint32_t packetFamily,
101 return ((packetFamily & 0x3F) << 26)|
102 ((packetId & 0x3FF) << 16);
105 uint32_t ConstructHeader(uint32_t packetFamily,
106 uint32_t packetClass,
109 return ((packetFamily & 0x3F) << 26)|
110 ((packetClass & 0x3FF) << 19)|
111 ((packetType & 0x3FFF) << 16);
114 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
116 BOOST_ASSERT(packetBuffer);
118 WriteUint64(packetBuffer->GetWritableData(), offset, value);
121 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
123 BOOST_ASSERT(packetBuffer);
125 WriteUint32(packetBuffer->GetWritableData(), offset, value);
128 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
130 BOOST_ASSERT(packetBuffer);
132 WriteUint16(packetBuffer->GetWritableData(), offset, value);
135 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
137 BOOST_ASSERT(buffer);
140 for (unsigned int i = 0; i < valueSize; i++, offset++)
142 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
146 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
148 BOOST_ASSERT(buffer);
150 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
151 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
152 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
153 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
154 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
155 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
156 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
157 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
160 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
162 BOOST_ASSERT(buffer);
164 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
165 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
166 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
167 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
170 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
172 BOOST_ASSERT(buffer);
174 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
175 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
178 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
180 BOOST_ASSERT(packetBuffer);
182 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
185 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
187 BOOST_ASSERT(packetBuffer);
189 return ReadUint64(packetBuffer->GetReadableData(), offset);
192 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
194 BOOST_ASSERT(packetBuffer);
196 return ReadUint32(packetBuffer->GetReadableData(), offset);
199 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
201 BOOST_ASSERT(packetBuffer);
203 return ReadUint16(packetBuffer->GetReadableData(), offset);
206 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
208 BOOST_ASSERT(packetBuffer);
210 return ReadUint8(packetBuffer->GetReadableData(), offset);
213 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
215 BOOST_ASSERT(buffer);
216 BOOST_ASSERT(outValue);
218 for (unsigned int i = 0; i < valueSize; i++, offset++)
220 outValue[i] = static_cast<uint8_t>(buffer[offset]);
224 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
226 BOOST_ASSERT(buffer);
229 value = static_cast<uint64_t>(buffer[offset]);
230 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
231 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
232 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
233 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
234 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
235 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
236 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
241 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
243 BOOST_ASSERT(buffer);
246 value = static_cast<uint32_t>(buffer[offset]);
247 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
248 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
249 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
253 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
255 BOOST_ASSERT(buffer);
258 value = static_cast<uint32_t>(buffer[offset]);
259 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
260 return static_cast<uint16_t>(value);
263 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
265 BOOST_ASSERT(buffer);
267 return buffer[offset];
270 std::string GetSoftwareInfo()
272 return std::string("ArmNN");
275 std::string GetHardwareVersion()
277 return std::string();
280 std::string GetSoftwareVersion()
282 std::string armnnVersion(ARMNN_VERSION);
283 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
287 std::string GetProcessName()
289 std::ifstream comm("/proc/self/comm");
295 /// Creates a timeline packet header
298 /// packetFamiliy Timeline Packet Family
299 /// packetClass Timeline Packet Class
300 /// packetType Timeline Packet Type
301 /// streamId Stream identifier
302 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
303 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
306 /// Pair of uint32_t containing word0 and word1 of the header
307 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
308 uint32_t packetClass,
311 uint32_t sequenceNumbered,
314 // Packet header word 0:
315 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
316 // 19:25 [7] packet_class: packet class
317 // 16:18 [3] packet_type: packet type
318 // 8:15 [8] reserved: all zeros
319 // 0:7 [8] stream_id: stream identifier
320 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
321 ((packetClass & 0x0000007F) << 19) |
322 ((packetType & 0x00000007) << 16) |
323 ((streamId & 0x00000007) << 0);
325 // Packet header word 1:
326 // 25:31 [7] reserved: all zeros
327 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
328 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
329 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
330 ((dataLength & 0x00FFFFFF) << 0);
332 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
335 // Calculate the actual length an SwString will be including the terminating null character
336 // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
337 // the size to allow the offset to be correctly updated when decoding a binary packet.
338 uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
340 std::vector<uint32_t> swTraceString;
341 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
342 unsigned int uint32_t_size = sizeof(uint32_t);
343 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
347 // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
348 SwTraceMessage ReadSwTraceMessage(const IPacketBufferPtr& packetBuffer, unsigned int& offset)
350 BOOST_ASSERT(packetBuffer);
352 unsigned int uint32_t_size = sizeof(uint32_t);
354 SwTraceMessage swTraceMessage;
357 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
358 swTraceMessage.id = readDeclId;
360 // SWTrace "namestring" format
361 // length of the string (first 4 bytes) + string + null terminator
363 // Check the decl_name
364 offset += uint32_t_size;
365 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
367 offset += uint32_t_size;
368 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
369 std::memcpy(swTraceStringBuffer.data(),
370 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
372 swTraceMessage.name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
375 offset += CalculateSizeOfPaddedSwString(swTraceMessage.name);
376 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
378 offset += uint32_t_size;
379 swTraceStringBuffer.resize(swTraceUINameLength - 1);
380 std::memcpy(swTraceStringBuffer.data(),
381 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
383 swTraceMessage.uiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
386 offset += CalculateSizeOfPaddedSwString(swTraceMessage.uiName);
387 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
389 offset += uint32_t_size;
390 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
391 std::memcpy(swTraceStringBuffer.data(),
392 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
394 swTraceMessage.argTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
396 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
399 offset += CalculateSizeOfPaddedSwString(swTraceString);
400 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
402 offset += uint32_t_size;
403 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
404 std::memcpy(swTraceStringBuffer.data(),
405 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
407 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
408 std::stringstream stringStream(swTraceString);
410 while (std::getline(stringStream, argName, ','))
412 swTraceMessage.argNames.push_back(argName);
415 offset += CalculateSizeOfPaddedSwString(swTraceString);
417 return swTraceMessage;
420 /// Creates a packet header for the timeline messages:
423 /// * declareEventClass
424 /// * declareRelationship
428 /// dataLength The length of the message body in bytes
431 /// Pair of uint32_t containing word0 and word1 of the header
432 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
434 return CreateTimelinePacketHeader(1, // Packet family
438 0, // Sequence number
439 dataLength); // Data length
442 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
443 const std::string& label,
444 unsigned char* buffer,
445 unsigned int bufferSize,
446 unsigned int& numberOfBytesWritten)
448 // Initialize the output value
449 numberOfBytesWritten = 0;
451 // Check that the given buffer is valid
452 if (buffer == nullptr || bufferSize == 0)
454 return TimelinePacketStatus::BufferExhaustion;
458 unsigned int uint32_t_size = sizeof(uint32_t);
459 unsigned int uint64_t_size = sizeof(uint64_t);
461 // Convert the label into a SWTrace string
462 std::vector<uint32_t> swTraceLabel;
463 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
466 return TimelinePacketStatus::Error;
469 // Calculate the size of the SWTrace string label (in bytes)
470 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
472 // Calculate the length of the data (in bytes)
473 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
474 uint64_t_size + // Profiling GUID
475 swTraceLabelSize; // Label
477 // Calculate the timeline binary packet size (in bytes)
478 unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words)
479 timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label
481 // Check whether the timeline binary packet fits in the given buffer
482 if (timelineLabelPacketSize > bufferSize)
484 return TimelinePacketStatus::BufferExhaustion;
487 // Create packet header
488 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineLabelPacketDataLength);
490 // Initialize the offset for writing in the buffer
491 unsigned int offset = 0;
493 // Write the timeline binary packet header to the buffer
494 WriteUint32(buffer, offset, packetHeader.first);
495 offset += uint32_t_size;
496 WriteUint32(buffer, offset, packetHeader.second);
497 offset += uint32_t_size;
499 // Write decl_Id to the buffer
500 WriteUint32(buffer, offset, 0u);
501 offset += uint32_t_size;
503 // Write the timeline binary packet payload to the buffer
504 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
505 offset += uint64_t_size;
506 for (uint32_t swTraceLabelWord : swTraceLabel)
508 WriteUint32(buffer, offset, swTraceLabelWord); // Label
509 offset += uint32_t_size;
512 // Update the number of bytes written
513 numberOfBytesWritten = timelineLabelPacketSize;
515 return TimelinePacketStatus::Ok;
518 TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
519 unsigned char* buffer,
520 unsigned int bufferSize,
521 unsigned int& numberOfBytesWritten)
523 // Initialize the output value
524 numberOfBytesWritten = 0;
526 // Check that the given buffer is valid
527 if (buffer == nullptr || bufferSize == 0)
529 return TimelinePacketStatus::BufferExhaustion;
533 unsigned int uint32_t_size = sizeof(uint32_t);
534 unsigned int uint64_t_size = sizeof(uint64_t);
536 // Calculate the length of the data (in bytes)
537 unsigned int timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID
540 // Calculate the timeline binary packet size (in bytes)
541 unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words)
542 uint32_t_size + // decl_Id
543 timelineEntityPacketDataLength; // Profiling GUID
545 // Check whether the timeline binary packet fits in the given buffer
546 if (timelineEntityPacketSize > bufferSize)
548 return TimelinePacketStatus::BufferExhaustion;
551 // Create packet header
552 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEntityPacketDataLength);
554 // Initialize the offset for writing in the buffer
555 unsigned int offset = 0;
557 // Write the timeline binary packet header to the buffer
558 WriteUint32(buffer, offset, packetHeader.first);
559 offset += uint32_t_size;
560 WriteUint32(buffer, offset, packetHeader.second);
561 offset += uint32_t_size;
563 // Write the decl_Id to the buffer
564 WriteUint32(buffer, offset, 1u);
565 offset += uint32_t_size;
567 // Write the timeline binary packet payload to the buffer
568 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
570 // Update the number of bytes written
571 numberOfBytesWritten = timelineEntityPacketSize;
573 return TimelinePacketStatus::Ok;
576 TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
577 uint64_t relationshipGuid,
580 unsigned char* buffer,
581 unsigned int bufferSize,
582 unsigned int& numberOfBytesWritten)
584 // Initialize the output value
585 numberOfBytesWritten = 0;
587 // Check that the given buffer is valid
588 if (buffer == nullptr || bufferSize == 0)
590 return TimelinePacketStatus::BufferExhaustion;
594 unsigned int uint32_t_size = sizeof(uint32_t);
595 unsigned int uint64_t_size = sizeof(uint64_t);
597 // Calculate the length of the data (in bytes)
598 unsigned int timelineRelationshipPacketDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
599 uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID
601 // Calculate the timeline binary packet size (in bytes)
602 unsigned int timelineRelationshipPacketSize = 2 * uint32_t_size + // Header (2 words)
603 timelineRelationshipPacketDataLength;
605 // Check whether the timeline binary packet fits in the given buffer
606 if (timelineRelationshipPacketSize > bufferSize)
608 return TimelinePacketStatus::BufferExhaustion;
611 // Create packet header
612 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineRelationshipPacketDataLength);
613 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
615 // Initialize the offset for writing in the buffer
616 unsigned int offset = 0;
618 // Write the timeline binary packet header to the buffer
619 WriteUint32(buffer, offset, packetHeader.first);
620 offset += uint32_t_size;
621 WriteUint32(buffer, offset, packetHeader.second);
622 offset += uint32_t_size;
624 uint32_t relationshipTypeUint = 0;
626 switch (relationshipType)
628 case ProfilingRelationshipType::RetentionLink:
629 relationshipTypeUint = 0;
631 case ProfilingRelationshipType::ExecutionLink:
632 relationshipTypeUint = 1;
634 case ProfilingRelationshipType::DataLink:
635 relationshipTypeUint = 2;
637 case ProfilingRelationshipType::LabelLink:
638 relationshipTypeUint = 3;
641 throw InvalidArgumentException("Unknown relationship type given.");
644 // Write the timeline binary packet payload to the buffer
645 // decl_id of the timeline message
647 WriteUint32(buffer, offset, declId); // decl_id
648 offset += uint32_t_size;
649 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
650 offset += uint32_t_size;
651 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
652 offset += uint64_t_size;
653 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
654 offset += uint64_t_size;
655 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
657 // Update the number of bytes written
658 numberOfBytesWritten = timelineRelationshipPacketSize;
660 return TimelinePacketStatus::Ok;
663 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
664 unsigned int bufferSize,
665 unsigned int& numberOfBytesWritten)
667 // Initialize the output value
668 numberOfBytesWritten = 0;
670 // Check that the given buffer is valid
671 if (buffer == nullptr || bufferSize == 0)
673 return TimelinePacketStatus::BufferExhaustion;
677 unsigned int uint32_t_size = sizeof(uint32_t);
679 // The payload/data of the packet consists of swtrace event definitions encoded according
680 // to the swtrace directory specification. The messages being the five defined below:
681 // | decl_id | decl_name | ui_name | arg_types | arg_names |
682 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
683 // | 0 | declareLabel | declare label | ps | guid,value |
684 // | 1 | declareEntity | declare entity | p | guid |
685 // | 2 | declareEventClass | declare event class | p | guid |
686 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
687 // | | | | | headGuid,tailGuid |
688 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
690 std::vector<std::vector<std::string>> timelineDirectoryMessages
692 {"declareLabel", "declare label", "ps", "guid,value"},
693 {"declareEntity", "declare entity", "p", "guid"},
694 {"declareEventClass", "declare event class", "p", "guid"},
695 {"declareRelationship", "declare relationship", "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
696 {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"}
699 unsigned int messagesDataLength = 0u;
700 std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
702 for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
704 messagesDataLength += uint32_t_size; // decl_id
706 std::vector<std::vector<uint32_t>> swTraceStringsVector;
707 for (const auto& label : timelineDirectoryMessage)
709 std::vector<uint32_t> swTraceString;
710 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
713 return TimelinePacketStatus::Error;
716 messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
717 swTraceStringsVector.push_back(swTraceString);
719 swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
722 // Calculate the timeline directory binary packet size (in bytes)
723 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
724 messagesDataLength; // 5 messages length
726 // Check whether the timeline directory binary packet fits in the given buffer
727 if (timelineDirectoryPacketSize > bufferSize)
729 return TimelinePacketStatus::BufferExhaustion;
732 // Create packet header
733 uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
734 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, dataLength);
736 // Initialize the offset for writing in the buffer
737 unsigned int offset = 0;
739 // Write the timeline binary packet header to the buffer
740 WriteUint32(buffer, offset, packetHeader.first);
741 offset += uint32_t_size;
742 WriteUint32(buffer, offset, packetHeader.second);
743 offset += uint32_t_size;
745 for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
747 // Write the timeline binary packet payload to the buffer
748 WriteUint32(buffer, offset, i); // decl_id
749 offset += uint32_t_size;
751 for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
753 for (uint32_t swTraceDeclStringWord : swTraceString)
755 WriteUint32(buffer, offset, swTraceDeclStringWord);
756 offset += uint32_t_size;
761 // Update the number of bytes written
762 numberOfBytesWritten = timelineDirectoryPacketSize;
764 return TimelinePacketStatus::Ok;
767 TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
768 unsigned char* buffer,
769 unsigned int bufferSize,
770 unsigned int& numberOfBytesWritten)
772 // Initialize the output value
773 numberOfBytesWritten = 0;
775 // Check that the given buffer is valid
776 if (buffer == nullptr || bufferSize == 0)
778 return TimelinePacketStatus::BufferExhaustion;
782 unsigned int uint32_t_size = sizeof(uint32_t);
783 unsigned int uint64_t_size = sizeof(uint64_t);
785 // decl_id of the timeline message
788 // Calculate the length of the data (in bytes)
789 unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
791 // Calculate the timeline binary packet size (in bytes)
792 unsigned int packetSize = 2 * uint32_t_size + // Header (2 words)
793 packetBodySize; // Body
795 // Check whether the timeline binary packet fits in the given buffer
796 if (packetSize > bufferSize)
798 return TimelinePacketStatus::BufferExhaustion;
801 // Create packet header
802 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(packetBodySize);
804 // Initialize the offset for writing in the buffer
805 unsigned int offset = 0;
807 // Write the timeline binary packet header to the buffer
808 WriteUint32(buffer, offset, packetHeader.first);
809 offset += uint32_t_size;
810 WriteUint32(buffer, offset, packetHeader.second);
811 offset += uint32_t_size;
813 // Write the timeline binary packet payload to the buffer
814 WriteUint32(buffer, offset, declId); // decl_id
815 offset += uint32_t_size;
816 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
818 // Update the number of bytes written
819 numberOfBytesWritten = packetSize;
821 return TimelinePacketStatus::Ok;
824 TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
825 std::thread::id threadId,
826 uint64_t profilingGuid,
827 unsigned char* buffer,
828 unsigned int bufferSize,
829 unsigned int& numberOfBytesWritten)
831 // Initialize the output value
832 numberOfBytesWritten = 0;
834 // Check that the given buffer is valid
835 if (buffer == nullptr || bufferSize == 0)
837 return TimelinePacketStatus::BufferExhaustion;
841 unsigned int uint32_t_size = sizeof(uint32_t);
842 unsigned int uint64_t_size = sizeof(uint64_t);
843 unsigned int threadId_size = sizeof(std::thread::id);
845 // decl_id of the timeline message
848 // Calculate the length of the data (in bytes)
849 unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id
850 uint64_t_size + // Timestamp
851 threadId_size + // Thread id
852 uint64_t_size; // Profiling GUID
854 // Calculate the timeline binary packet size (in bytes)
855 unsigned int timelineEventPacketSize = 2 * uint32_t_size + // Header (2 words)
856 timelineEventPacketDataLength; // Timestamp + thread id + profiling GUID
858 // Check whether the timeline binary packet fits in the given buffer
859 if (timelineEventPacketSize > bufferSize)
861 return TimelinePacketStatus::BufferExhaustion;
864 // Create packet header
865 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEventPacketDataLength);
867 // Initialize the offset for writing in the buffer
868 unsigned int offset = 0;
870 // Write the timeline binary packet header to the buffer
871 WriteUint32(buffer, offset, packetHeader.first);
872 offset += uint32_t_size;
873 WriteUint32(buffer, offset, packetHeader.second);
874 offset += uint32_t_size;
876 // Write the timeline binary packet payload to the buffer
877 WriteUint32(buffer, offset, declId); // decl_id
878 offset += uint32_t_size;
879 WriteUint64(buffer, offset, timestamp); // Timestamp
880 offset += uint64_t_size;
881 WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id
882 offset += threadId_size;
883 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
884 offset += uint64_t_size;
886 // Update the number of bytes written
887 numberOfBytesWritten = timelineEventPacketSize;
889 return TimelinePacketStatus::Ok;
893 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
895 std::stringstream outputStream, centrePadding;
896 int padding = spacingWidth - static_cast<int>(stringToPass.size());
898 for (int i = 0; i < padding / 2; ++i)
900 centrePadding << " ";
903 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
905 if (padding > 0 && padding %2 != 0)
910 return outputStream.str();
913 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
917 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
919 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
921 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
924 std::cout << std::string(body.size(), '-') << "\n";
928 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
932 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
934 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
936 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
939 std::cout << std::string(body.size(), '-') << "\n";
944 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
948 body.append(CentreAlignFormatting(counter->m_Name, 20));
950 body.append(CentreAlignFormatting(counter->m_Description, 50));
952 body.append(CentreAlignFormatting(counter->m_Units, 14));
954 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
956 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
958 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
960 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
962 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
964 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
966 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
970 std::cout << std::string(body.size(), '-') << "\n";
975 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
976 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
978 std::string categoryBody;
979 std::string categoryHeader;
981 categoryHeader.append(CentreAlignFormatting("Name", 20));
982 categoryHeader.append(" | ");
983 categoryHeader.append(CentreAlignFormatting("Device", 12));
984 categoryHeader.append(" | ");
985 categoryHeader.append(CentreAlignFormatting("Counter set UID:", 16));
986 categoryHeader.append(" | ");
987 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
988 categoryHeader.append("\n");
990 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
991 categoryBody.append(" | ");
992 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_DeviceUid), 12));
993 categoryBody.append(" | ");
994 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_CounterSetUid), 16));
995 categoryBody.append(" | ");
996 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
998 std::cout << "\n" << "\n";
999 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
1001 std::cout << std::string(categoryHeader.size(), '=') << "\n";
1003 std::cout << categoryHeader;
1005 std::cout << std::string(categoryBody.size(), '-') << "\n";
1007 std::cout << categoryBody;
1009 std::string counterHeader;
1011 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
1012 counterHeader.append(" | ");
1013 counterHeader.append(CentreAlignFormatting("Description", 50));
1014 counterHeader.append(" | ");
1015 counterHeader.append(CentreAlignFormatting("Units", 14));
1016 counterHeader.append(" | ");
1017 counterHeader.append(CentreAlignFormatting("UID", 6));
1018 counterHeader.append(" | ");
1019 counterHeader.append(CentreAlignFormatting("Max UID", 10));
1020 counterHeader.append(" | ");
1021 counterHeader.append(CentreAlignFormatting("Class", 8));
1022 counterHeader.append(" | ");
1023 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
1024 counterHeader.append(" | ");
1025 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
1026 counterHeader.append(" | ");
1027 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
1028 counterHeader.append(" | ");
1029 counterHeader.append(CentreAlignFormatting("Device UID", 14));
1030 counterHeader.append("\n");
1032 std::cout << "\n" << "\n";
1033 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
1034 static_cast<int>(counterHeader.size()));
1036 std::cout << std::string(counterHeader.size(), '=') << "\n";
1037 std::cout << counterHeader;
1038 for (auto& it: category->m_Counters) {
1039 auto search = counterMap.find(it);
1040 if(search != counterMap.end()) {
1041 PrintCounterDetails(search->second);
1046 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1048 std::string devicesHeader;
1050 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1051 devicesHeader.append(" | ");
1052 devicesHeader.append(CentreAlignFormatting("UID", 13));
1053 devicesHeader.append(" | ");
1054 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1055 devicesHeader.append("\n");
1057 std::cout << "\n" << "\n";
1058 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1060 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1061 std::cout << devicesHeader;
1062 for (auto& it: counterDirectory.GetDevices()) {
1063 PrintDeviceDetails(it);
1066 std::string counterSetHeader;
1068 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1069 counterSetHeader.append(" | ");
1070 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1071 counterSetHeader.append(" | ");
1072 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1073 counterSetHeader.append("\n");
1075 std::cout << "\n" << "\n";
1076 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1078 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1080 std::cout << counterSetHeader;
1082 for (auto& it: counterDirectory.GetCounterSets()) {
1083 PrintCounterSetDetails(it);
1086 auto counters = counterDirectory.GetCounters();
1087 for (auto& it: counterDirectory.GetCategories()) {
1088 PrintCategoryDetails(it, counters);
1094 } // namespace profiling
1096 } // namespace armnn
1101 bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
1103 return std::memcmp(left.data(), &right, left.size()) == 0;