8 #include <common/include/CommonProfilingUtils.hpp> 9 #include <common/include/ProfilingException.hpp> 10 #include <common/include/SwTrace.hpp> 31 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
40 if (uid == std::numeric_limits<uint16_t>::max())
42 throw RuntimeException(
"Generating the next UID for profiling would result in an overflow");
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");
61 static uint16_t uid = 1;
64 ThrowIfCantGenerateNextUid(uid);
81 ThrowIfCantGenerateNextUid(firstUid, cores);
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++;
97 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
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)
118 WriteUint64(packetBuffer->GetWritableData(), offset, value);
125 WriteUint32(packetBuffer->GetWritableData(), offset, value);
132 WriteUint16(packetBuffer->GetWritableData(), offset, value);
139 WriteUint8(packetBuffer->GetWritableData(), offset, value);
142 void WriteBytes(
unsigned char* buffer,
unsigned int offset,
const void* value,
unsigned int valueSize)
147 void WriteUint64(
unsigned char* buffer,
unsigned int offset, uint64_t value)
152 void WriteUint32(
unsigned char* buffer,
unsigned int offset, uint32_t value)
157 void WriteUint16(
unsigned char* buffer,
unsigned int offset, uint16_t value)
162 void WriteUint8(
unsigned char* buffer,
unsigned int offset, uint8_t value)
171 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
178 return ReadUint64(packetBuffer->GetReadableData(), offset);
185 return ReadUint32(packetBuffer->GetReadableData(), offset);
192 return ReadUint16(packetBuffer->GetReadableData(), offset);
199 return ReadUint8(packetBuffer->GetReadableData(), offset);
202 void ReadBytes(
const unsigned char* buffer,
unsigned int offset,
unsigned int valueSize, uint8_t outValue[])
207 uint64_t
ReadUint64(
const unsigned char* buffer,
unsigned int offset)
212 uint32_t
ReadUint32(
const unsigned char* buffer,
unsigned int offset)
217 uint16_t
ReadUint16(
const unsigned char* buffer,
unsigned int offset)
222 uint8_t
ReadUint8(
const unsigned char* buffer,
unsigned int offset)
229 return std::string(
"ArmNN");
234 return std::string();
245 std::ifstream comm(
"/proc/self/comm");
264 uint32_t packetClass,
267 uint32_t sequenceNumbered,
276 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
277 ((packetClass & 0x0000007F) << 19) |
278 ((packetType & 0x00000007) << 16) |
279 ((streamId & 0x00000007) << 0);
285 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
286 ((dataLength & 0x00FFFFFF) << 0);
288 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
314 const std::string& label,
315 unsigned char* buffer,
316 unsigned int remainingBufferSize,
317 unsigned int& numberOfBytesWritten)
320 numberOfBytesWritten = 0;
323 if (buffer ==
nullptr || remainingBufferSize == 0)
330 unsigned int uint64_t_size =
sizeof(uint64_t);
333 std::vector<uint32_t> swTraceLabel;
334 bool result = arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(label, swTraceLabel);
341 unsigned int swTraceLabelSize =
armnn::numeric_cast<
unsigned int>(swTraceLabel.size()) * uint32_t_size;
344 unsigned int timelineLabelPacketDataLength = uint32_t_size +
349 if (timelineLabelPacketDataLength > remainingBufferSize)
355 unsigned int offset = 0;
363 offset += uint64_t_size;
364 for (uint32_t swTraceLabelWord : swTraceLabel)
371 numberOfBytesWritten = timelineLabelPacketDataLength;
377 unsigned char* buffer,
378 unsigned int remainingBufferSize,
379 unsigned int& numberOfBytesWritten)
382 numberOfBytesWritten = 0;
385 if (buffer ==
nullptr || remainingBufferSize == 0)
392 unsigned int uint64_t_size =
sizeof(uint64_t);
395 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size;
398 if (timelineEntityDataLength > remainingBufferSize)
404 unsigned int offset = 0;
414 numberOfBytesWritten = timelineEntityDataLength;
420 uint64_t relationshipGuid,
423 uint64_t attributeGuid,
424 unsigned char* buffer,
425 unsigned int remainingBufferSize,
426 unsigned int& numberOfBytesWritten)
429 numberOfBytesWritten = 0;
432 if (buffer ==
nullptr || remainingBufferSize == 0)
439 unsigned int uint64_t_size =
sizeof(uint64_t);
442 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 +
447 if (timelineRelationshipDataLength > remainingBufferSize)
453 unsigned int offset = 0;
455 uint32_t relationshipTypeUint = 0;
457 switch (relationshipType)
460 relationshipTypeUint = 0;
463 relationshipTypeUint = 1;
466 relationshipTypeUint = 2;
469 relationshipTypeUint = 3;
483 offset += uint64_t_size;
485 offset += uint64_t_size;
487 offset += uint64_t_size;
492 numberOfBytesWritten = timelineRelationshipDataLength;
498 unsigned int remainingBufferSize,
499 unsigned int& numberOfBytesWritten)
502 numberOfBytesWritten = 0;
505 if (buffer ==
nullptr || remainingBufferSize == 0)
511 unsigned int uint8_t_size =
sizeof(uint8_t);
513 unsigned int uint64_t_size =
sizeof(uint64_t);
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" }
537 std::vector<uint32_t> swTraceBuffer;
538 for (
const auto& directoryComponent : timelineDirectoryMessages)
546 catch (
const std::exception&)
550 swTraceBuffer.push_back(declId);
553 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceNameCharPolicy>(
554 directoryComponent[1], swTraceBuffer);
555 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
556 directoryComponent[2], swTraceBuffer);
557 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceTypeCharPolicy>(
558 directoryComponent[3], swTraceBuffer);
559 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
560 directoryComponent[4], swTraceBuffer);
567 unsigned int dataLength = 3 * uint8_t_size +
572 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size +
576 if (timelineDirectoryPacketSize > remainingBufferSize)
585 unsigned int offset = 0;
594 uint8_t streamVersion = 4;
597 switch (threadIdBytes)
606 offset += uint8_t_size;
608 offset += uint8_t_size;
610 offset += uint8_t_size;
613 uint32_t numberOfDeclarations =
armnn::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
616 for (uint32_t i : swTraceBuffer)
623 numberOfBytesWritten = timelineDirectoryPacketSize;
630 unsigned char* buffer,
631 unsigned int remainingBufferSize,
632 unsigned int& numberOfBytesWritten)
635 numberOfBytesWritten = 0;
638 if (buffer ==
nullptr || remainingBufferSize == 0)
645 unsigned int uint64_t_size =
sizeof(uint64_t);
651 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2);
654 if (dataSize > remainingBufferSize)
660 unsigned int offset = 0;
666 offset += uint64_t_size;
670 numberOfBytesWritten = dataSize;
677 uint64_t profilingGuid,
678 unsigned char* buffer,
679 unsigned int remainingBufferSize,
680 unsigned int& numberOfBytesWritten)
683 numberOfBytesWritten = 0;
685 if (buffer ==
nullptr || remainingBufferSize == 0)
692 unsigned int uint64_t_size =
sizeof(uint64_t);
698 unsigned int timelineEventDataLength = uint32_t_size +
704 if (timelineEventDataLength > remainingBufferSize)
710 unsigned int offset = 0;
716 offset += uint64_t_size;
720 offset += uint64_t_size;
722 numberOfBytesWritten = timelineEventDataLength;
743 std::cout << std::string(body.size(),
'-') <<
"\n";
758 std::cout << std::string(body.size(),
'-') <<
"\n";
789 std::cout << std::string(body.size(),
'-') <<
"\n";
795 std::unordered_map<
unsigned short, std::shared_ptr<Counter>> counterMap)
797 std::string categoryBody;
798 std::string categoryHeader;
801 categoryHeader.append(
" | ");
803 categoryHeader.append(
"\n");
806 categoryBody.append(
" | ");
809 std::cout <<
"\n" <<
"\n";
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;
823 counterHeader.append(
" | ");
825 counterHeader.append(
" | ");
827 counterHeader.append(
" | ");
829 counterHeader.append(
" | ");
831 counterHeader.append(
" | ");
833 counterHeader.append(
" | ");
835 counterHeader.append(
" | ");
837 counterHeader.append(
" | ");
839 counterHeader.append(
" | ");
841 counterHeader.append(
"\n");
843 std::cout <<
"\n" <<
"\n";
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()) {
859 std::string devicesHeader;
862 devicesHeader.append(
" | ");
864 devicesHeader.append(
" | ");
866 devicesHeader.append(
"\n");
868 std::cout <<
"\n" <<
"\n";
871 std::cout << std::string(devicesHeader.size(),
'=') <<
"\n";
872 std::cout << devicesHeader;
873 for (
auto& it: counterDirectory.
GetDevices()) {
877 std::string counterSetHeader;
880 counterSetHeader.append(
" | ");
882 counterSetHeader.append(
" | ");
884 counterSetHeader.append(
"\n");
886 std::cout <<
"\n" <<
"\n";
889 std::cout << std::string(counterSetHeader.size(),
'=') <<
"\n";
891 std::cout << counterSetHeader;
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);
954 return std::memcmp(left.data(), &right, left.size()) == 0;
void WriteBytes(unsigned char *buffer, unsigned int offset, const void *value, unsigned int valueSize)
uint8_t ReadUint8(const unsigned char *buffer, unsigned int offset)
void WriteUint32(unsigned char *buffer, unsigned int offset, uint32_t value)
void ReadBytes(const unsigned char *buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
uint32_t ReadUint32(const unsigned char *buffer, unsigned int offset)
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::string GetHardwareVersion()
bool operator==(const std::vector< uint8_t > &left, int right)
void WriteUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint16_t value)
void ReadBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)
uint64_t ReadUint64(const IPacketBufferPtr &packetBuffer, unsigned int offset)
std::pair< uint32_t, uint32_t > CreateTimelinePacketHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType, uint32_t streamId, uint32_t sequenceNumbered, uint32_t dataLength)
Creates a timeline packet header.
#define ARMNN_MINOR_VERSION
std::string GetProcessName()
TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, uint64_t attributeGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
Head retains(parents) Tail.
uint16_t ReadUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset)
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2021 ARM Limited and Contributors.
Head execution start depends on Tail execution completion.
void PrintDeviceDetails(const std::pair< const unsigned short, std::unique_ptr< Device >> &devicePair)
virtual const CounterSets & GetCounterSets() const =0
std::string GetSoftwareInfo()
arm::pipe::Packet ReceivePacket(const unsigned char *buffer, uint32_t length)
void WriteUint16(unsigned char *buffer, unsigned int offset, uint16_t value)
uint16_t ReadUint16(const unsigned char *buffer, unsigned int offset)
virtual const Categories & GetCategories() const =0
uint16_t GetNextUid(bool peekOnly)
void WriteUint8(unsigned char *buffer, unsigned int offset, uint8_t value)
virtual const Devices & GetDevices() const =0
TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, int threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
void WriteBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, const void *value, unsigned int valueSize)
void PrintCategoryDetails(const std::unique_ptr< Category > &category, std::unordered_map< unsigned short, std::shared_ptr< Counter >> counterMap)
uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetId)
ProfilingRelationshipType
#define ARMNN_ASSERT(COND)
constexpr unsigned int ThreadIdSize
TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid, uint64_t nameGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void WriteUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint8_t value)
uint64_t ReadUint64(const unsigned char *buffer, unsigned int offset)
void WriteUint64(const std::unique_ptr< IPacketBuffer > &packetBuffer, unsigned int offset, uint64_t value)
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
void PrintCounterSetDetails(const std::pair< const unsigned short, std::unique_ptr< CounterSet >> &counterSetPair)
std::string GetSoftwareVersion()
std::pair< uint32_t, uint32_t > CreateTimelineMessagePacketHeader(unsigned int dataLength)
Creates a packet header for the timeline messages:
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
void PrintCounterDetails(std::shared_ptr< Counter > &counter)
virtual const Counters & GetCounters() const =0
std::string CentreAlignFormatting(const std::string &stringToPass, const int spacingWidth)
void WriteUint64(unsigned char *buffer, unsigned int offset, uint64_t value)
TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void PrintCounterDirectory(ICounterDirectory &counterDirectory)
#define ARMNN_MAJOR_VERSION
std::unique_ptr< IPacketBuffer > IPacketBufferPtr