12 #include <boost/assert.hpp> 27 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
36 if (uid == std::numeric_limits<uint16_t>::max())
38 throw RuntimeException(
"Generating the next UID for profiling would result in an overflow");
44 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
46 throw RuntimeException(
"Generating the next UID for profiling would result in an overflow");
57 static uint16_t uid = 1;
60 ThrowIfCantGenerateNextUid(uid);
77 ThrowIfCantGenerateNextUid(firstUid, cores);
80 size_t counterUidsSize = cores == 0 ? 1 : cores;
81 std::vector<uint16_t> counterUids(counterUidsSize, 0);
82 for (
size_t i = 0; i < counterUidsSize; i++)
84 counterUids[i] = firstUid++;
91 BOOST_ASSERT(packetBuffer);
93 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
99 return ((packetFamily & 0x3F) << 26)|
100 ((packetId & 0x3FF) << 16);
104 uint32_t packetClass,
107 return ((packetFamily & 0x3F) << 26)|
108 ((packetClass & 0x3FF) << 19)|
109 ((packetType & 0x3FFF) << 16);
112 void WriteUint64(
const std::unique_ptr<IPacketBuffer>& packetBuffer,
unsigned int offset, uint64_t value)
114 BOOST_ASSERT(packetBuffer);
116 WriteUint64(packetBuffer->GetWritableData(), offset, value);
121 BOOST_ASSERT(packetBuffer);
123 WriteUint32(packetBuffer->GetWritableData(), offset, value);
128 BOOST_ASSERT(packetBuffer);
130 WriteUint16(packetBuffer->GetWritableData(), offset, value);
135 BOOST_ASSERT(packetBuffer);
137 WriteUint8(packetBuffer->GetWritableData(), offset, value);
140 void WriteBytes(
unsigned char* buffer,
unsigned int offset,
const void* value,
unsigned int valueSize)
142 BOOST_ASSERT(buffer);
145 for (
unsigned int i = 0; i < valueSize; i++, offset++)
147 buffer[offset] = *(
reinterpret_cast<const unsigned char*
>(value) + i);
151 void WriteUint64(
unsigned char* buffer,
unsigned int offset, uint64_t value)
153 BOOST_ASSERT(buffer);
155 buffer[offset] =
static_cast<unsigned char>(value & 0xFF);
156 buffer[offset + 1] =
static_cast<unsigned char>((value >> 8) & 0xFF);
157 buffer[offset + 2] =
static_cast<unsigned char>((value >> 16) & 0xFF);
158 buffer[offset + 3] =
static_cast<unsigned char>((value >> 24) & 0xFF);
159 buffer[offset + 4] =
static_cast<unsigned char>((value >> 32) & 0xFF);
160 buffer[offset + 5] =
static_cast<unsigned char>((value >> 40) & 0xFF);
161 buffer[offset + 6] =
static_cast<unsigned char>((value >> 48) & 0xFF);
162 buffer[offset + 7] =
static_cast<unsigned char>((value >> 56) & 0xFF);
165 void WriteUint32(
unsigned char* buffer,
unsigned int offset, uint32_t value)
167 BOOST_ASSERT(buffer);
169 buffer[offset] =
static_cast<unsigned char>(value & 0xFF);
170 buffer[offset + 1] =
static_cast<unsigned char>((value >> 8) & 0xFF);
171 buffer[offset + 2] =
static_cast<unsigned char>((value >> 16) & 0xFF);
172 buffer[offset + 3] =
static_cast<unsigned char>((value >> 24) & 0xFF);
175 void WriteUint16(
unsigned char* buffer,
unsigned int offset, uint16_t value)
177 BOOST_ASSERT(buffer);
179 buffer[offset] =
static_cast<unsigned char>(value & 0xFF);
180 buffer[offset + 1] =
static_cast<unsigned char>((value >> 8) & 0xFF);
183 void WriteUint8(
unsigned char* buffer,
unsigned int offset, uint8_t value)
185 BOOST_ASSERT(buffer);
187 buffer[offset] =
static_cast<unsigned char>(value);
192 BOOST_ASSERT(packetBuffer);
194 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
199 BOOST_ASSERT(packetBuffer);
201 return ReadUint64(packetBuffer->GetReadableData(), offset);
206 BOOST_ASSERT(packetBuffer);
208 return ReadUint32(packetBuffer->GetReadableData(), offset);
213 BOOST_ASSERT(packetBuffer);
215 return ReadUint16(packetBuffer->GetReadableData(), offset);
220 BOOST_ASSERT(packetBuffer);
222 return ReadUint8(packetBuffer->GetReadableData(), offset);
225 void ReadBytes(
const unsigned char* buffer,
unsigned int offset,
unsigned int valueSize, uint8_t outValue[])
227 BOOST_ASSERT(buffer);
228 BOOST_ASSERT(outValue);
230 for (
unsigned int i = 0; i < valueSize; i++, offset++)
232 outValue[i] =
static_cast<uint8_t
>(buffer[offset]);
236 uint64_t
ReadUint64(
const unsigned char* buffer,
unsigned int offset)
238 BOOST_ASSERT(buffer);
241 value =
static_cast<uint64_t
>(buffer[offset]);
242 value |=
static_cast<uint64_t
>(buffer[offset + 1]) << 8;
243 value |=
static_cast<uint64_t
>(buffer[offset + 2]) << 16;
244 value |=
static_cast<uint64_t
>(buffer[offset + 3]) << 24;
245 value |=
static_cast<uint64_t
>(buffer[offset + 4]) << 32;
246 value |=
static_cast<uint64_t
>(buffer[offset + 5]) << 40;
247 value |=
static_cast<uint64_t
>(buffer[offset + 6]) << 48;
248 value |=
static_cast<uint64_t
>(buffer[offset + 7]) << 56;
253 uint32_t
ReadUint32(
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 value |=
static_cast<uint32_t
>(buffer[offset + 2]) << 16;
261 value |=
static_cast<uint32_t
>(buffer[offset + 3]) << 24;
265 uint16_t
ReadUint16(
const unsigned char* buffer,
unsigned int offset)
267 BOOST_ASSERT(buffer);
270 value =
static_cast<uint32_t
>(buffer[offset]);
271 value |=
static_cast<uint32_t
>(buffer[offset + 1]) << 8;
272 return static_cast<uint16_t
>(value);
275 uint8_t
ReadUint8(
const unsigned char* buffer,
unsigned int offset)
277 BOOST_ASSERT(buffer);
279 return buffer[offset];
284 return std::string(
"ArmNN");
289 return std::string();
295 std::string result =
"Armnn " + armnnVersion.substr(2,2) +
"." + armnnVersion.substr(4,2);
301 std::ifstream comm(
"/proc/self/comm");
312 std::vector<uint32_t> swTraceString;
313 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
322 BOOST_ASSERT(packetBuffer);
329 uint32_t readDeclId =
ReadUint32(packetBuffer, offset);
330 swTraceMessage.
m_Id = readDeclId;
337 uint32_t swTraceDeclNameLength =
ReadUint32(packetBuffer, offset);
340 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
341 std::memcpy(swTraceStringBuffer.data(),
342 packetBuffer + offset, swTraceStringBuffer.size());
344 swTraceMessage.
m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
348 uint32_t swTraceUINameLength =
ReadUint32(packetBuffer, offset);
351 swTraceStringBuffer.resize(swTraceUINameLength - 1);
352 std::memcpy(swTraceStringBuffer.data(),
353 packetBuffer + offset, swTraceStringBuffer.size());
355 swTraceMessage.
m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
359 uint32_t swTraceArgTypesLength =
ReadUint32(packetBuffer, offset);
362 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
363 std::memcpy(swTraceStringBuffer.data(),
364 packetBuffer + offset, swTraceStringBuffer.size());
366 swTraceMessage.
m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
368 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
372 uint32_t swTraceArgNamesLength =
ReadUint32(packetBuffer, offset);
375 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
376 std::memcpy(swTraceStringBuffer.data(),
377 packetBuffer + offset, swTraceStringBuffer.size());
379 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
380 std::stringstream stringStream(swTraceString);
382 while (std::getline(stringStream, argName,
','))
389 return swTraceMessage;
405 uint32_t packetClass,
408 uint32_t sequenceNumbered,
417 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
418 ((packetClass & 0x0000007F) << 19) |
419 ((packetType & 0x00000007) << 16) |
420 ((streamId & 0x00000007) << 0);
426 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
427 ((dataLength & 0x00FFFFFF) << 0);
429 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
455 const std::string& label,
456 unsigned char* buffer,
457 unsigned int remainingBufferSize,
458 unsigned int& numberOfBytesWritten)
461 numberOfBytesWritten = 0;
464 if (buffer ==
nullptr || remainingBufferSize == 0)
471 unsigned int uint64_t_size =
sizeof(uint64_t);
474 std::vector<uint32_t> swTraceLabel;
475 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
482 unsigned int swTraceLabelSize =
boost::numeric_cast<
unsigned int>(swTraceLabel.size()) * uint32_t_size;
485 unsigned int timelineLabelPacketDataLength = uint32_t_size +
490 if (timelineLabelPacketDataLength > remainingBufferSize)
496 unsigned int offset = 0;
504 offset += uint64_t_size;
505 for (uint32_t swTraceLabelWord : swTraceLabel)
512 numberOfBytesWritten = timelineLabelPacketDataLength;
518 unsigned char* buffer,
519 unsigned int remainingBufferSize,
520 unsigned int& numberOfBytesWritten)
523 numberOfBytesWritten = 0;
526 if (buffer ==
nullptr || remainingBufferSize == 0)
533 unsigned int uint64_t_size =
sizeof(uint64_t);
536 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size;
539 if (timelineEntityDataLength > remainingBufferSize)
545 unsigned int offset = 0;
555 numberOfBytesWritten = timelineEntityDataLength;
561 uint64_t relationshipGuid,
564 unsigned char* buffer,
565 unsigned int remainingBufferSize,
566 unsigned int& numberOfBytesWritten)
569 numberOfBytesWritten = 0;
572 if (buffer ==
nullptr || remainingBufferSize == 0)
579 unsigned int uint64_t_size =
sizeof(uint64_t);
582 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 +
586 if (timelineRelationshipDataLength > remainingBufferSize)
592 unsigned int offset = 0;
594 uint32_t relationshipTypeUint = 0;
596 switch (relationshipType)
599 relationshipTypeUint = 0;
602 relationshipTypeUint = 1;
605 relationshipTypeUint = 2;
608 relationshipTypeUint = 3;
622 offset += uint64_t_size;
624 offset += uint64_t_size;
628 numberOfBytesWritten = timelineRelationshipDataLength;
634 unsigned int remainingBufferSize,
635 unsigned int& numberOfBytesWritten)
638 numberOfBytesWritten = 0;
641 if (buffer ==
nullptr || remainingBufferSize == 0)
647 unsigned int uint8_t_size =
sizeof(uint8_t);
649 unsigned int uint64_t_size =
sizeof(uint64_t);
650 unsigned int threadId_size =
sizeof(std::thread::id);
663 std::vector<std::vector<std::string>> timelineDirectoryMessages
665 {
"0",
"declareLabel",
"declare label",
"ps",
"guid,value" },
666 {
"1",
"declareEntity",
"declare entity",
"p",
"guid" },
667 {
"2",
"declareEventClass",
"declare event class",
"p",
"guid" },
668 {
"3",
"declareRelationship",
"declare relationship",
"Ippp",
669 "relationshipType,relationshipGuid,headGuid,tailGuid" },
670 {
"4",
"declareEvent",
"declare event",
"@tp",
"timestamp,threadId,eventGuid" }
674 std::vector<uint32_t> swTraceBuffer;
675 for (
const auto& directoryComponent : timelineDirectoryMessages)
683 catch (
const std::exception&)
687 swTraceBuffer.push_back(declId);
690 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer);
691 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer);
692 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer);
693 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer);
700 unsigned int dataLength = 3 * uint8_t_size +
705 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size +
709 if (timelineDirectoryPacketSize > remainingBufferSize)
718 unsigned int offset = 0;
727 uint8_t streamVersion = 4;
730 switch (threadIdBytes)
739 offset += uint8_t_size;
741 offset += uint8_t_size;
743 offset += uint8_t_size;
746 uint32_t numberOfDeclarations =
boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
749 for (uint32_t i : swTraceBuffer)
756 numberOfBytesWritten = timelineDirectoryPacketSize;
762 unsigned char* buffer,
763 unsigned int remainingBufferSize,
764 unsigned int& numberOfBytesWritten)
767 numberOfBytesWritten = 0;
770 if (buffer ==
nullptr || remainingBufferSize == 0)
777 unsigned int uint64_t_size =
sizeof(uint64_t);
783 unsigned int dataSize = uint32_t_size + uint64_t_size;
786 if (dataSize > remainingBufferSize)
792 unsigned int offset = 0;
800 numberOfBytesWritten = dataSize;
806 std::thread::id threadId,
807 uint64_t profilingGuid,
808 unsigned char* buffer,
809 unsigned int remainingBufferSize,
810 unsigned int& numberOfBytesWritten)
813 numberOfBytesWritten = 0;
815 if (buffer ==
nullptr || remainingBufferSize == 0)
822 unsigned int uint64_t_size =
sizeof(uint64_t);
823 unsigned int threadId_size =
sizeof(std::thread::id);
829 unsigned int timelineEventDataLength = uint32_t_size +
835 if (timelineEventDataLength > remainingBufferSize)
841 unsigned int offset = 0;
847 offset += uint64_t_size;
848 WriteBytes(buffer, offset, &threadId, threadId_size);
849 offset += threadId_size;
851 offset += uint64_t_size;
853 numberOfBytesWritten = timelineEventDataLength;
860 std::stringstream outputStream, centrePadding;
861 int padding = spacingWidth -
static_cast<int>(stringToPass.size());
863 for (
int i = 0; i < padding / 2; ++i)
865 centrePadding <<
" ";
868 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
870 if (padding > 0 && padding %2 != 0)
875 return outputStream.str();
889 std::cout << std::string(body.size(),
'-') <<
"\n";
904 std::cout << std::string(body.size(),
'-') <<
"\n";
935 std::cout << std::string(body.size(),
'-') <<
"\n";
941 std::unordered_map<
unsigned short, std::shared_ptr<Counter>> counterMap)
943 std::string categoryBody;
944 std::string categoryHeader;
947 categoryHeader.append(
" | ");
949 categoryHeader.append(
"\n");
952 categoryBody.append(
" | ");
955 std::cout <<
"\n" <<
"\n";
958 std::cout << std::string(categoryHeader.size(),
'=') <<
"\n";
960 std::cout << categoryHeader;
962 std::cout << std::string(categoryBody.size(),
'-') <<
"\n";
964 std::cout << categoryBody;
966 std::string counterHeader;
969 counterHeader.append(
" | ");
971 counterHeader.append(
" | ");
973 counterHeader.append(
" | ");
975 counterHeader.append(
" | ");
977 counterHeader.append(
" | ");
979 counterHeader.append(
" | ");
981 counterHeader.append(
" | ");
983 counterHeader.append(
" | ");
985 counterHeader.append(
" | ");
987 counterHeader.append(
"\n");
989 std::cout <<
"\n" <<
"\n";
991 static_cast<int>(counterHeader.size()));
993 std::cout << std::string(counterHeader.size(),
'=') <<
"\n";
994 std::cout << counterHeader;
995 for (
auto& it: category->m_Counters) {
996 auto search = counterMap.find(it);
997 if(search != counterMap.end()) {
1005 std::string devicesHeader;
1008 devicesHeader.append(
" | ");
1010 devicesHeader.append(
" | ");
1012 devicesHeader.append(
"\n");
1014 std::cout <<
"\n" <<
"\n";
1017 std::cout << std::string(devicesHeader.size(),
'=') <<
"\n";
1018 std::cout << devicesHeader;
1019 for (
auto& it: counterDirectory.
GetDevices()) {
1023 std::string counterSetHeader;
1026 counterSetHeader.append(
" | ");
1028 counterSetHeader.append(
" | ");
1030 counterSetHeader.append(
"\n");
1032 std::cout <<
"\n" <<
"\n";
1035 std::cout << std::string(counterSetHeader.size(),
'=') <<
"\n";
1037 std::cout << counterSetHeader;
1052 #if USE_CLOCK_MONOTONIC_RAW 1053 using clock = MonotonicClockRaw;
1055 using clock = std::chrono::steady_clock;
1059 auto timestamp = clock::now();
1061 return static_cast<uint64_t
>(timestamp.time_since_epoch().count());
1071 bool operator==(
const std::vector<uint8_t>& left, std::thread::id right)
1073 return std::memcmp(left.data(), &right, left.size()) == 0;
#define ARMNN_VERSION
ARMNN_VERSION: "YYYYMMPP" where: YYYY = 4-digit year number MM = 2-digit month number PP = 2-digit pa...
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
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
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()
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:
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