8 #include "common/include/ProfilingException.hpp" 29 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
38 if (uid == std::numeric_limits<uint16_t>::max())
40 throw RuntimeException(
"Generating the next UID for profiling would result in an overflow");
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");
59 static uint16_t uid = 1;
62 ThrowIfCantGenerateNextUid(uid);
79 ThrowIfCantGenerateNextUid(firstUid, cores);
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++;
95 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
101 return (( packetFamily & 0x0000003F ) << 26 )|
102 (( packetId & 0x000003FF ) << 16 );
105 void WriteUint64(
const std::unique_ptr<IPacketBuffer>& packetBuffer,
unsigned int offset, uint64_t value)
109 WriteUint64(packetBuffer->GetWritableData(), offset, value);
116 WriteUint32(packetBuffer->GetWritableData(), offset, value);
123 WriteUint16(packetBuffer->GetWritableData(), offset, value);
130 WriteUint8(packetBuffer->GetWritableData(), offset, value);
133 void WriteBytes(
unsigned char* buffer,
unsigned int offset,
const void* value,
unsigned int valueSize)
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)
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)
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)
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)
180 buffer[offset] =
static_cast<unsigned char>(value);
187 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
194 return ReadUint64(packetBuffer->GetReadableData(), offset);
201 return ReadUint32(packetBuffer->GetReadableData(), offset);
208 return ReadUint16(packetBuffer->GetReadableData(), offset);
215 return ReadUint8(packetBuffer->GetReadableData(), offset);
218 void ReadBytes(
const unsigned char* buffer,
unsigned int offset,
unsigned int valueSize, uint8_t 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)
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)
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)
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)
272 return buffer[offset];
277 return std::string(
"ArmNN");
282 return std::string();
293 std::ifstream comm(
"/proc/self/comm");
304 std::vector<uint32_t> swTraceString;
305 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
321 uint32_t readDeclId =
ReadUint32(packetBuffer, offset);
322 swTraceMessage.
m_Id = readDeclId;
329 uint32_t swTraceDeclNameLength =
ReadUint32(packetBuffer, offset);
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());
340 uint32_t swTraceUINameLength =
ReadUint32(packetBuffer, offset);
343 swTraceStringBuffer.resize(swTraceUINameLength - 1);
344 std::memcpy(swTraceStringBuffer.data(),
345 packetBuffer + offset, swTraceStringBuffer.size());
347 swTraceMessage.
m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
351 uint32_t swTraceArgTypesLength =
ReadUint32(packetBuffer, offset);
354 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
355 std::memcpy(swTraceStringBuffer.data(),
356 packetBuffer + offset, swTraceStringBuffer.size());
358 swTraceMessage.
m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
360 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
364 uint32_t swTraceArgNamesLength =
ReadUint32(packetBuffer, offset);
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,
','))
381 return swTraceMessage;
397 uint32_t packetClass,
400 uint32_t sequenceNumbered,
409 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
410 ((packetClass & 0x0000007F) << 19) |
411 ((packetType & 0x00000007) << 16) |
412 ((streamId & 0x00000007) << 0);
418 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
419 ((dataLength & 0x00FFFFFF) << 0);
421 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
447 const std::string& label,
448 unsigned char* buffer,
449 unsigned int remainingBufferSize,
450 unsigned int& numberOfBytesWritten)
453 numberOfBytesWritten = 0;
456 if (buffer ==
nullptr || remainingBufferSize == 0)
463 unsigned int uint64_t_size =
sizeof(uint64_t);
466 std::vector<uint32_t> swTraceLabel;
467 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
474 unsigned int swTraceLabelSize =
boost::numeric_cast<
unsigned int>(swTraceLabel.size()) * uint32_t_size;
477 unsigned int timelineLabelPacketDataLength = uint32_t_size +
482 if (timelineLabelPacketDataLength > remainingBufferSize)
488 unsigned int offset = 0;
496 offset += uint64_t_size;
497 for (uint32_t swTraceLabelWord : swTraceLabel)
504 numberOfBytesWritten = timelineLabelPacketDataLength;
510 unsigned char* buffer,
511 unsigned int remainingBufferSize,
512 unsigned int& numberOfBytesWritten)
515 numberOfBytesWritten = 0;
518 if (buffer ==
nullptr || remainingBufferSize == 0)
525 unsigned int uint64_t_size =
sizeof(uint64_t);
528 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size;
531 if (timelineEntityDataLength > remainingBufferSize)
537 unsigned int offset = 0;
547 numberOfBytesWritten = timelineEntityDataLength;
553 uint64_t relationshipGuid,
556 unsigned char* buffer,
557 unsigned int remainingBufferSize,
558 unsigned int& numberOfBytesWritten)
561 numberOfBytesWritten = 0;
564 if (buffer ==
nullptr || remainingBufferSize == 0)
571 unsigned int uint64_t_size =
sizeof(uint64_t);
574 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 +
578 if (timelineRelationshipDataLength > remainingBufferSize)
584 unsigned int offset = 0;
586 uint32_t relationshipTypeUint = 0;
588 switch (relationshipType)
591 relationshipTypeUint = 0;
594 relationshipTypeUint = 1;
597 relationshipTypeUint = 2;
600 relationshipTypeUint = 3;
614 offset += uint64_t_size;
616 offset += uint64_t_size;
620 numberOfBytesWritten = timelineRelationshipDataLength;
626 unsigned int remainingBufferSize,
627 unsigned int& numberOfBytesWritten)
630 numberOfBytesWritten = 0;
633 if (buffer ==
nullptr || remainingBufferSize == 0)
639 unsigned int uint8_t_size =
sizeof(uint8_t);
641 unsigned int uint64_t_size =
sizeof(uint64_t);
654 std::vector<std::vector<std::string>> timelineDirectoryMessages
656 {
"0",
"declareLabel",
"declare label",
"ps",
"guid,value" },
657 {
"1",
"declareEntity",
"declare entity",
"p",
"guid" },
658 {
"2",
"declareEventClass",
"declare event class",
"p",
"guid" },
659 {
"3",
"declareRelationship",
"declare relationship",
"Ippp",
660 "relationshipType,relationshipGuid,headGuid,tailGuid" },
661 {
"4",
"declareEvent",
"declare event",
"@tp",
"timestamp,threadId,eventGuid" }
665 std::vector<uint32_t> swTraceBuffer;
666 for (
const auto& directoryComponent : timelineDirectoryMessages)
674 catch (
const std::exception&)
678 swTraceBuffer.push_back(declId);
681 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer);
682 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer);
683 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer);
684 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer);
691 unsigned int dataLength = 3 * uint8_t_size +
696 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size +
700 if (timelineDirectoryPacketSize > remainingBufferSize)
709 unsigned int offset = 0;
718 uint8_t streamVersion = 4;
721 switch (threadIdBytes)
730 offset += uint8_t_size;
732 offset += uint8_t_size;
734 offset += uint8_t_size;
737 uint32_t numberOfDeclarations =
boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
740 for (uint32_t i : swTraceBuffer)
747 numberOfBytesWritten = timelineDirectoryPacketSize;
753 unsigned char* buffer,
754 unsigned int remainingBufferSize,
755 unsigned int& numberOfBytesWritten)
758 numberOfBytesWritten = 0;
761 if (buffer ==
nullptr || remainingBufferSize == 0)
768 unsigned int uint64_t_size =
sizeof(uint64_t);
774 unsigned int dataSize = uint32_t_size + uint64_t_size;
777 if (dataSize > remainingBufferSize)
783 unsigned int offset = 0;
791 numberOfBytesWritten = dataSize;
797 std::thread::id threadId,
798 uint64_t profilingGuid,
799 unsigned char* buffer,
800 unsigned int remainingBufferSize,
801 unsigned int& numberOfBytesWritten)
804 numberOfBytesWritten = 0;
806 if (buffer ==
nullptr || remainingBufferSize == 0)
813 unsigned int uint64_t_size =
sizeof(uint64_t);
819 unsigned int timelineEventDataLength = uint32_t_size +
825 if (timelineEventDataLength > remainingBufferSize)
831 unsigned int offset = 0;
837 offset += uint64_t_size;
841 offset += uint64_t_size;
843 numberOfBytesWritten = timelineEventDataLength;
850 std::stringstream outputStream, centrePadding;
851 int padding = spacingWidth -
static_cast<int>(stringToPass.size());
853 for (
int i = 0; i < padding / 2; ++i)
855 centrePadding <<
" ";
858 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
860 if (padding > 0 && padding %2 != 0)
865 return outputStream.str();
879 std::cout << std::string(body.size(),
'-') <<
"\n";
894 std::cout << std::string(body.size(),
'-') <<
"\n";
925 std::cout << std::string(body.size(),
'-') <<
"\n";
931 std::unordered_map<
unsigned short, std::shared_ptr<Counter>> counterMap)
933 std::string categoryBody;
934 std::string categoryHeader;
937 categoryHeader.append(
" | ");
939 categoryHeader.append(
"\n");
942 categoryBody.append(
" | ");
945 std::cout <<
"\n" <<
"\n";
948 std::cout << std::string(categoryHeader.size(),
'=') <<
"\n";
950 std::cout << categoryHeader;
952 std::cout << std::string(categoryBody.size(),
'-') <<
"\n";
954 std::cout << categoryBody;
956 std::string counterHeader;
959 counterHeader.append(
" | ");
961 counterHeader.append(
" | ");
963 counterHeader.append(
" | ");
965 counterHeader.append(
" | ");
967 counterHeader.append(
" | ");
969 counterHeader.append(
" | ");
971 counterHeader.append(
" | ");
973 counterHeader.append(
" | ");
975 counterHeader.append(
" | ");
977 counterHeader.append(
"\n");
979 std::cout <<
"\n" <<
"\n";
981 static_cast<int>(counterHeader.size()));
983 std::cout << std::string(counterHeader.size(),
'=') <<
"\n";
984 std::cout << counterHeader;
985 for (
auto& it: category->m_Counters) {
986 auto search = counterMap.find(it);
987 if(search != counterMap.end()) {
995 std::string devicesHeader;
998 devicesHeader.append(
" | ");
1000 devicesHeader.append(
" | ");
1002 devicesHeader.append(
"\n");
1004 std::cout <<
"\n" <<
"\n";
1007 std::cout << std::string(devicesHeader.size(),
'=') <<
"\n";
1008 std::cout << devicesHeader;
1009 for (
auto& it: counterDirectory.
GetDevices()) {
1013 std::string counterSetHeader;
1016 counterSetHeader.append(
" | ");
1018 counterSetHeader.append(
" | ");
1020 counterSetHeader.append(
"\n");
1022 std::cout <<
"\n" <<
"\n";
1025 std::cout << std::string(counterSetHeader.size(),
'=') <<
"\n";
1027 std::cout << counterSetHeader;
1042 #if USE_CLOCK_MONOTONIC_RAW 1043 using clock = MonotonicClockRaw;
1045 using clock = std::chrono::steady_clock;
1049 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
1051 return static_cast<uint64_t
>(timestamp.count());
1056 if (buffer ==
nullptr)
1058 throw armnnProfiling::ProfilingException(
"data buffer is nullptr");
1062 throw armnnProfiling::ProfilingException(
"length of data buffer is less than 8");
1065 uint32_t metadataIdentifier = 0;
1066 std::memcpy(&metadataIdentifier, buffer,
sizeof(metadataIdentifier));
1068 uint32_t dataLength = 0;
1069 std::memcpy(&dataLength, buffer + 4u,
sizeof(dataLength));
1071 std::unique_ptr<unsigned char[]> packetData;
1074 packetData = std::make_unique<unsigned char[]>(dataLength);
1075 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1078 return Packet(metadataIdentifier, dataLength, packetData);
1088 bool operator==(
const std::vector<uint8_t>& left, std::thread::id right)
1090 return std::memcmp(left.data(), &right, left.size()) == 0;
bool operator==(const std::vector< uint8_t > &left, std::thread::id right)
TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
std::string GetHardwareVersion()
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.
std::string GetProcessName()
Head retains(parents) Tail.
uint16_t ReadUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset)
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2020 ARM Limited.
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()
TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
virtual const Categories & GetCategories() const =0
uint16_t GetNextUid(bool peekOnly)
SwTraceMessage ReadSwTraceMessage(const unsigned char *packetBuffer, unsigned int &offset)
virtual const Devices & GetDevices() const =0
std::vector< std::string > m_ArgNames
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
std::vector< char > m_ArgTypes
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)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
constexpr unsigned int ThreadIdSize
void WriteUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint8_t value)
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()
Packet ReceivePacket(const unsigned char *buffer, uint32_t length)
TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
uint32_t CalculateSizeOfPaddedSwString(const std::string &str)
std::pair< uint32_t, uint32_t > CreateTimelineMessagePacketHeader(unsigned int dataLength)
Creates a packet header for the timeline messages:
#define ARMNN_MAJOR_VERSION
TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void PrintCounterDetails(std::shared_ptr< Counter > &counter)
virtual const Counters & GetCounters() const =0
std::string CentreAlignFormatting(const std::string &stringToPass, const int spacingWidth)
TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid, unsigned char *buffer, unsigned int remainingBufferSize, unsigned int &numberOfBytesWritten)
void PrintCounterDirectory(ICounterDirectory &counterDirectory)
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
#define ARMNN_MINOR_VERSION