2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ProfilingUtils.hpp"
8 #include <common/include/CommonProfilingUtils.hpp>
9 #include <common/include/ProfilingException.hpp>
10 #include <common/include/SwTrace.hpp>
12 #include <armnn/Version.hpp>
14 #include <WallClockTimer.hpp>
16 #include <armnn/utility/Assert.hpp>
31 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
33 // Check that it is possible to generate the next UID without causing an overflow
38 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
39 // running multiple parallel workloads and will not provide multiple streams of data for each event)
40 if (uid == std::numeric_limits<uint16_t>::max())
42 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
46 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
47 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
48 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
50 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
56 } // Anonymous namespace
58 uint16_t GetNextUid(bool peekOnly)
60 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
61 static uint16_t uid = 1;
63 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
64 ThrowIfCantGenerateNextUid(uid);
78 std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores)
80 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
81 ThrowIfCantGenerateNextUid(firstUid, cores);
83 // Get the next counter UIDs
84 size_t counterUidsSize = cores == 0 ? 1 : cores;
85 std::vector<uint16_t> counterUids(counterUidsSize, 0);
86 for (size_t i = 0; i < counterUidsSize; i++)
88 counterUids[i] = firstUid++;
93 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
95 ARMNN_ASSERT(packetBuffer);
97 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
100 uint32_t ConstructHeader(uint32_t packetFamily,
103 return (( packetFamily & 0x0000003F ) << 26 )|
104 (( packetId & 0x000003FF ) << 16 );
107 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType)
109 return ((packetFamily & 0x0000003F) << 26) |
110 ((packetClass & 0x0000007F) << 19) |
111 ((packetType & 0x00000007) << 16);
114 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
116 ARMNN_ASSERT(packetBuffer);
118 WriteUint64(packetBuffer->GetWritableData(), offset, value);
121 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
123 ARMNN_ASSERT(packetBuffer);
125 WriteUint32(packetBuffer->GetWritableData(), offset, value);
128 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
130 ARMNN_ASSERT(packetBuffer);
132 WriteUint16(packetBuffer->GetWritableData(), offset, value);
135 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
137 ARMNN_ASSERT(packetBuffer);
139 WriteUint8(packetBuffer->GetWritableData(), offset, value);
142 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
144 arm::pipe::WriteBytes(buffer, offset, value, valueSize);
147 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
149 arm::pipe::WriteUint64(buffer, offset, value);
152 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
154 arm::pipe::WriteUint32(buffer, offset, value);
157 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
159 arm::pipe::WriteUint16(buffer, offset, value);
162 void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
164 arm::pipe::WriteUint8(buffer, offset, value);
167 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
169 ARMNN_ASSERT(packetBuffer);
171 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
174 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
176 ARMNN_ASSERT(packetBuffer);
178 return ReadUint64(packetBuffer->GetReadableData(), offset);
181 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
183 ARMNN_ASSERT(packetBuffer);
185 return ReadUint32(packetBuffer->GetReadableData(), offset);
188 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
190 ARMNN_ASSERT(packetBuffer);
192 return ReadUint16(packetBuffer->GetReadableData(), offset);
195 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
197 ARMNN_ASSERT(packetBuffer);
199 return ReadUint8(packetBuffer->GetReadableData(), offset);
202 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
204 arm::pipe::ReadBytes(buffer, offset, valueSize, outValue);
207 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
209 return arm::pipe::ReadUint64(buffer, offset);
212 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
214 return arm::pipe::ReadUint32(buffer, offset);
217 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
219 return arm::pipe::ReadUint16(buffer, offset);
222 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
224 return arm::pipe::ReadUint8(buffer, offset);
227 std::string GetSoftwareInfo()
229 return std::string("ArmNN");
232 std::string GetHardwareVersion()
234 return std::string();
237 std::string GetSoftwareVersion()
239 std::string result = "Armnn " + std::to_string(ARMNN_MAJOR_VERSION) + "." + std::to_string(ARMNN_MINOR_VERSION);
243 std::string GetProcessName()
245 std::ifstream comm("/proc/self/comm");
251 /// Creates a timeline packet header
254 /// packetFamiliy Timeline Packet Family
255 /// packetClass Timeline Packet Class
256 /// packetType Timeline Packet Type
257 /// streamId Stream identifier
258 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
259 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
262 /// Pair of uint32_t containing word0 and word1 of the header
263 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
264 uint32_t packetClass,
267 uint32_t sequenceNumbered,
270 // Packet header word 0:
271 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
272 // 19:25 [7] packet_class: packet class
273 // 16:18 [3] packet_type: packet type
274 // 8:15 [8] reserved: all zeros
275 // 0:7 [8] stream_id: stream identifier
276 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
277 ((packetClass & 0x0000007F) << 19) |
278 ((packetType & 0x00000007) << 16) |
279 ((streamId & 0x00000007) << 0);
281 // Packet header word 1:
282 // 25:31 [7] reserved: all zeros
283 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
284 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
285 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
286 ((dataLength & 0x00FFFFFF) << 0);
288 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
291 /// Creates a packet header for the timeline messages:
294 /// * declareEventClass
295 /// * declareRelationship
299 /// dataLength The length of the message body in bytes
302 /// Pair of uint32_t containing word0 and word1 of the header
303 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
305 return CreateTimelinePacketHeader(1, // Packet family
309 0, // Sequence number
310 dataLength); // Data length
313 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
314 const std::string& label,
315 unsigned char* buffer,
316 unsigned int remainingBufferSize,
317 unsigned int& numberOfBytesWritten)
319 // Initialize the output value
320 numberOfBytesWritten = 0;
322 // Check that the given buffer is valid
323 if (buffer == nullptr || remainingBufferSize == 0)
325 return TimelinePacketStatus::BufferExhaustion;
329 unsigned int uint32_t_size = sizeof(uint32_t);
330 unsigned int uint64_t_size = sizeof(uint64_t);
332 // Convert the label into a SWTrace string
333 std::vector<uint32_t> swTraceLabel;
334 bool result = arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(label, swTraceLabel);
337 return TimelinePacketStatus::Error;
340 // Calculate the size of the SWTrace string label (in bytes)
341 unsigned int swTraceLabelSize = armnn::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
343 // Calculate the length of the data (in bytes)
344 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
345 uint64_t_size + // Profiling GUID
346 swTraceLabelSize; // Label
348 // Check whether the timeline binary packet fits in the given buffer
349 if (timelineLabelPacketDataLength > remainingBufferSize)
351 return TimelinePacketStatus::BufferExhaustion;
354 // Initialize the offset for writing in the buffer
355 unsigned int offset = 0;
357 // Write decl_Id to the buffer
358 WriteUint32(buffer, offset, 0u);
359 offset += uint32_t_size;
361 // Write the timeline binary packet payload to the buffer
362 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
363 offset += uint64_t_size;
364 for (uint32_t swTraceLabelWord : swTraceLabel)
366 WriteUint32(buffer, offset, swTraceLabelWord); // Label
367 offset += uint32_t_size;
370 // Update the number of bytes written
371 numberOfBytesWritten = timelineLabelPacketDataLength;
373 return TimelinePacketStatus::Ok;
376 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
377 unsigned char* buffer,
378 unsigned int remainingBufferSize,
379 unsigned int& numberOfBytesWritten)
381 // Initialize the output value
382 numberOfBytesWritten = 0;
384 // Check that the given buffer is valid
385 if (buffer == nullptr || remainingBufferSize == 0)
387 return TimelinePacketStatus::BufferExhaustion;
391 unsigned int uint32_t_size = sizeof(uint32_t);
392 unsigned int uint64_t_size = sizeof(uint64_t);
394 // Calculate the length of the data (in bytes)
395 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
397 // Check whether the timeline binary packet fits in the given buffer
398 if (timelineEntityDataLength > remainingBufferSize)
400 return TimelinePacketStatus::BufferExhaustion;
403 // Initialize the offset for writing in the buffer
404 unsigned int offset = 0;
406 // Write the decl_Id to the buffer
407 WriteUint32(buffer, offset, 1u);
408 offset += uint32_t_size;
410 // Write the timeline binary packet payload to the buffer
411 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
413 // Update the number of bytes written
414 numberOfBytesWritten = timelineEntityDataLength;
416 return TimelinePacketStatus::Ok;
419 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
420 uint64_t relationshipGuid,
423 uint64_t attributeGuid,
424 unsigned char* buffer,
425 unsigned int remainingBufferSize,
426 unsigned int& numberOfBytesWritten)
428 // Initialize the output value
429 numberOfBytesWritten = 0;
431 // Check that the given buffer is valid
432 if (buffer == nullptr || remainingBufferSize == 0)
434 return TimelinePacketStatus::BufferExhaustion;
438 unsigned int uint32_t_size = sizeof(uint32_t);
439 unsigned int uint64_t_size = sizeof(uint64_t);
441 // Calculate the length of the data (in bytes)
442 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
443 uint64_t_size * 4; // Relationship GUID + Head GUID +
444 // tail GUID + attributeGuid
446 // Check whether the timeline binary fits in the given buffer
447 if (timelineRelationshipDataLength > remainingBufferSize)
449 return TimelinePacketStatus::BufferExhaustion;
452 // Initialize the offset for writing in the buffer
453 unsigned int offset = 0;
455 uint32_t relationshipTypeUint = 0;
457 switch (relationshipType)
459 case ProfilingRelationshipType::RetentionLink:
460 relationshipTypeUint = 0;
462 case ProfilingRelationshipType::ExecutionLink:
463 relationshipTypeUint = 1;
465 case ProfilingRelationshipType::DataLink:
466 relationshipTypeUint = 2;
468 case ProfilingRelationshipType::LabelLink:
469 relationshipTypeUint = 3;
472 throw InvalidArgumentException("Unknown relationship type given.");
475 // Write the timeline binary payload to the buffer
476 // decl_id of the timeline message
478 WriteUint32(buffer, offset, declId); // decl_id
479 offset += uint32_t_size;
480 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
481 offset += uint32_t_size;
482 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
483 offset += uint64_t_size;
484 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
485 offset += uint64_t_size;
486 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
487 offset += uint64_t_size;
488 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
491 // Update the number of bytes written
492 numberOfBytesWritten = timelineRelationshipDataLength;
494 return TimelinePacketStatus::Ok;
497 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
498 unsigned int remainingBufferSize,
499 unsigned int& numberOfBytesWritten)
501 // Initialize the output value
502 numberOfBytesWritten = 0;
504 // Check that the given buffer is valid
505 if (buffer == nullptr || remainingBufferSize == 0)
507 return TimelinePacketStatus::BufferExhaustion;
511 unsigned int uint8_t_size = sizeof(uint8_t);
512 unsigned int uint32_t_size = sizeof(uint32_t);
513 unsigned int uint64_t_size = sizeof(uint64_t);
515 // The payload/data of the packet consists of swtrace event definitions encoded according
516 // to the swtrace directory specification. The messages being the five defined below:
518 // | decl_id | decl_name | ui_name | arg_types | arg_names |
519 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
520 // | 0 | declareLabel | declare label | ps | guid,value |
521 // | 1 | declareEntity | declare entity | p | guid |
522 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
523 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
524 // | | | | | headGuid,tailGuid,attributeGuid |
525 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
526 std::vector<std::vector<std::string>> timelineDirectoryMessages
528 { "0", "declareLabel", "declare label", "ps", "guid,value" },
529 { "1", "declareEntity", "declare entity", "p", "guid" },
530 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
531 { "3", "declareRelationship", "declare relationship", "Ipppp",
532 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
533 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
536 // Build the message declarations
537 std::vector<uint32_t> swTraceBuffer;
538 for (const auto& directoryComponent : timelineDirectoryMessages)
544 declId = armnn::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
546 catch (const std::exception&)
548 return TimelinePacketStatus::Error;
550 swTraceBuffer.push_back(declId);
553 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceNameCharPolicy>(
554 directoryComponent[1], swTraceBuffer); // decl_name
555 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
556 directoryComponent[2], swTraceBuffer); // ui_name
557 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceTypeCharPolicy>(
558 directoryComponent[3], swTraceBuffer); // arg_types
559 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
560 directoryComponent[4], swTraceBuffer); // arg_names
563 return TimelinePacketStatus::Error;
567 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
568 armnn::numeric_cast<unsigned int>(swTraceBuffer.size()) *
569 uint32_t_size; // Trace directory (5 messages)
571 // Calculate the timeline directory binary packet size (in bytes)
572 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
573 dataLength; // Payload
575 // Check whether the timeline directory binary packet fits in the given buffer
576 if (timelineDirectoryPacketSize > remainingBufferSize)
578 return TimelinePacketStatus::BufferExhaustion;
581 // Create packet header
582 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, armnn::numeric_cast<uint32_t>(dataLength));
584 // Initialize the offset for writing in the buffer
585 unsigned int offset = 0;
587 // Write the timeline binary packet header to the buffer
588 WriteUint32(buffer, offset, packetHeader.first);
589 offset += uint32_t_size;
590 WriteUint32(buffer, offset, packetHeader.second);
591 offset += uint32_t_size;
593 // Write the stream header
594 uint8_t streamVersion = 4;
595 uint8_t pointerBytes = armnn::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
596 uint8_t threadIdBytes = armnn::numeric_cast<uint8_t>(ThreadIdSize);
597 switch (threadIdBytes)
599 case 4: // Typically Windows and Android
600 case 8: // Typically Linux
601 break; // Valid values
603 return TimelinePacketStatus::Error; // Invalid value
605 WriteUint8(buffer, offset, streamVersion);
606 offset += uint8_t_size;
607 WriteUint8(buffer, offset, pointerBytes);
608 offset += uint8_t_size;
609 WriteUint8(buffer, offset, threadIdBytes);
610 offset += uint8_t_size;
612 // Write the SWTrace directory
613 uint32_t numberOfDeclarations = armnn::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
614 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
615 offset += uint32_t_size;
616 for (uint32_t i : swTraceBuffer)
618 WriteUint32(buffer, offset, i); // Message declarations
619 offset += uint32_t_size;
622 // Update the number of bytes written
623 numberOfBytesWritten = timelineDirectoryPacketSize;
625 return TimelinePacketStatus::Ok;
628 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
630 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 uint32_t_size = sizeof(uint32_t);
645 unsigned int uint64_t_size = sizeof(uint64_t);
647 // decl_id of the timeline message
650 // Calculate the length of the data (in bytes)
651 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
653 // Check whether the timeline binary fits in the given buffer
654 if (dataSize > remainingBufferSize)
656 return TimelinePacketStatus::BufferExhaustion;
659 // Initialize the offset for writing in the buffer
660 unsigned int offset = 0;
662 // Write the timeline binary payload to the buffer
663 WriteUint32(buffer, offset, declId); // decl_id
664 offset += uint32_t_size;
665 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
666 offset += uint64_t_size;
667 WriteUint64(buffer, offset, nameGuid); // Name GUID
669 // Update the number of bytes written
670 numberOfBytesWritten = dataSize;
672 return TimelinePacketStatus::Ok;
675 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
677 uint64_t profilingGuid,
678 unsigned char* buffer,
679 unsigned int remainingBufferSize,
680 unsigned int& numberOfBytesWritten)
682 // Initialize the output value
683 numberOfBytesWritten = 0;
684 // Check that the given buffer is valid
685 if (buffer == nullptr || remainingBufferSize == 0)
687 return TimelinePacketStatus::BufferExhaustion;
691 unsigned int uint32_t_size = sizeof(uint32_t);
692 unsigned int uint64_t_size = sizeof(uint64_t);
694 // decl_id of the timeline message
697 // Calculate the length of the data (in bytes)
698 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
699 uint64_t_size + // Timestamp
700 ThreadIdSize + // Thread id
701 uint64_t_size; // Profiling GUID
703 // Check whether the timeline binary packet fits in the given buffer
704 if (timelineEventDataLength > remainingBufferSize)
706 return TimelinePacketStatus::BufferExhaustion;
709 // Initialize the offset for writing in the buffer
710 unsigned int offset = 0;
712 // Write the timeline binary payload to the buffer
713 WriteUint32(buffer, offset, declId); // decl_id
714 offset += uint32_t_size;
715 WriteUint64(buffer, offset, timestamp); // Timestamp
716 offset += uint64_t_size;
717 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
718 offset += ThreadIdSize;
719 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
720 offset += uint64_t_size;
721 // Update the number of bytes written
722 numberOfBytesWritten = timelineEventDataLength;
724 return TimelinePacketStatus::Ok;
727 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
729 return arm::pipe::CentreAlignFormatting(stringToPass, spacingWidth);
732 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
736 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
738 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
740 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
743 std::cout << std::string(body.size(), '-') << "\n";
747 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
751 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
753 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
755 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
758 std::cout << std::string(body.size(), '-') << "\n";
763 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
767 body.append(CentreAlignFormatting(counter->m_Name, 20));
769 body.append(CentreAlignFormatting(counter->m_Description, 50));
771 body.append(CentreAlignFormatting(counter->m_Units, 14));
773 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
775 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
777 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
779 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
781 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
783 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
785 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
789 std::cout << std::string(body.size(), '-') << "\n";
794 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
795 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
797 std::string categoryBody;
798 std::string categoryHeader;
800 categoryHeader.append(CentreAlignFormatting("Name", 20));
801 categoryHeader.append(" | ");
802 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
803 categoryHeader.append("\n");
805 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
806 categoryBody.append(" | ");
807 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
809 std::cout << "\n" << "\n";
810 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
812 std::cout << std::string(categoryHeader.size(), '=') << "\n";
814 std::cout << categoryHeader;
816 std::cout << std::string(categoryBody.size(), '-') << "\n";
818 std::cout << categoryBody;
820 std::string counterHeader;
822 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
823 counterHeader.append(" | ");
824 counterHeader.append(CentreAlignFormatting("Description", 50));
825 counterHeader.append(" | ");
826 counterHeader.append(CentreAlignFormatting("Units", 14));
827 counterHeader.append(" | ");
828 counterHeader.append(CentreAlignFormatting("UID", 6));
829 counterHeader.append(" | ");
830 counterHeader.append(CentreAlignFormatting("Max UID", 10));
831 counterHeader.append(" | ");
832 counterHeader.append(CentreAlignFormatting("Class", 8));
833 counterHeader.append(" | ");
834 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
835 counterHeader.append(" | ");
836 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
837 counterHeader.append(" | ");
838 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
839 counterHeader.append(" | ");
840 counterHeader.append(CentreAlignFormatting("Device UID", 14));
841 counterHeader.append("\n");
843 std::cout << "\n" << "\n";
844 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
845 static_cast<int>(counterHeader.size()));
847 std::cout << std::string(counterHeader.size(), '=') << "\n";
848 std::cout << counterHeader;
849 for (auto& it: category->m_Counters) {
850 auto search = counterMap.find(it);
851 if(search != counterMap.end()) {
852 PrintCounterDetails(search->second);
857 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
859 std::string devicesHeader;
861 devicesHeader.append(CentreAlignFormatting("Device name", 20));
862 devicesHeader.append(" | ");
863 devicesHeader.append(CentreAlignFormatting("UID", 13));
864 devicesHeader.append(" | ");
865 devicesHeader.append(CentreAlignFormatting("Cores", 10));
866 devicesHeader.append("\n");
868 std::cout << "\n" << "\n";
869 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
871 std::cout << std::string(devicesHeader.size(), '=') << "\n";
872 std::cout << devicesHeader;
873 for (auto& it: counterDirectory.GetDevices()) {
874 PrintDeviceDetails(it);
877 std::string counterSetHeader;
879 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
880 counterSetHeader.append(" | ");
881 counterSetHeader.append(CentreAlignFormatting("UID", 13));
882 counterSetHeader.append(" | ");
883 counterSetHeader.append(CentreAlignFormatting("Count", 10));
884 counterSetHeader.append("\n");
886 std::cout << "\n" << "\n";
887 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
889 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
891 std::cout << counterSetHeader;
893 for (auto& it: counterDirectory.GetCounterSets()) {
894 PrintCounterSetDetails(it);
897 auto counters = counterDirectory.GetCounters();
898 for (auto& it: counterDirectory.GetCategories()) {
899 PrintCategoryDetails(it, counters);
904 uint64_t GetTimestamp()
906 #if USE_CLOCK_MONOTONIC_RAW
907 using clock = MonotonicClockRaw;
909 using clock = std::chrono::steady_clock;
913 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
915 return static_cast<uint64_t>(timestamp.count());
918 arm::pipe::Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
920 if (buffer == nullptr)
922 throw arm::pipe::ProfilingException("data buffer is nullptr");
926 throw arm::pipe::ProfilingException("length of data buffer is less than 8");
929 uint32_t metadataIdentifier = 0;
930 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
932 uint32_t dataLength = 0;
933 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
935 std::unique_ptr<unsigned char[]> packetData;
938 packetData = std::make_unique<unsigned char[]>(dataLength);
939 std::memcpy(packetData.get(), buffer + 8u, dataLength);
942 return arm::pipe::Packet(metadataIdentifier, dataLength, packetData);
945 } // namespace profiling
952 bool operator==(const std::vector<uint8_t>& left, int right)
954 return std::memcmp(left.data(), &right, left.size()) == 0;