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>
25 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
27 // Check that it is possible to generate the next UID without causing an overflow
32 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
33 // running multiple parallel workloads and will not provide multiple streams of data for each event)
34 if (uid == std::numeric_limits<uint16_t>::max())
36 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
40 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
41 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
42 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
44 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
50 } // Anonymous namespace
52 uint16_t GetNextUid(bool peekOnly)
54 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
55 static uint16_t uid = 1;
57 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
58 ThrowIfCantGenerateNextUid(uid);
72 std::vector<uint16_t> GetNextCounterUids(uint16_t cores)
74 // The UID used for counters only. The first valid UID is 0
75 static uint16_t counterUid = 0;
77 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
78 ThrowIfCantGenerateNextUid(counterUid, cores);
80 // Get the next counter UIDs
81 size_t counterUidsSize = cores == 0 ? 1 : cores;
82 std::vector<uint16_t> counterUids(counterUidsSize, 0);
83 for (size_t i = 0; i < counterUidsSize; i++)
85 counterUids[i] = counterUid++;
90 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
92 BOOST_ASSERT(packetBuffer);
94 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
97 void WriteUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint64_t value)
99 BOOST_ASSERT(packetBuffer);
101 WriteUint64(packetBuffer->GetWritableData(), offset, value);
104 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
106 BOOST_ASSERT(packetBuffer);
108 WriteUint32(packetBuffer->GetWritableData(), offset, value);
111 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
113 BOOST_ASSERT(packetBuffer);
115 WriteUint16(packetBuffer->GetWritableData(), offset, value);
118 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
120 BOOST_ASSERT(buffer);
123 for (unsigned int i = 0; i < valueSize; i++, offset++)
125 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
129 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
131 BOOST_ASSERT(buffer);
133 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
134 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
135 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
136 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
137 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
138 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
139 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
140 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
143 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
145 BOOST_ASSERT(buffer);
147 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
148 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
149 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
150 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
153 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
155 BOOST_ASSERT(buffer);
157 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
158 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
161 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
163 BOOST_ASSERT(packetBuffer);
165 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
168 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
170 BOOST_ASSERT(packetBuffer);
172 return ReadUint64(packetBuffer->GetReadableData(), offset);
175 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
177 BOOST_ASSERT(packetBuffer);
179 return ReadUint32(packetBuffer->GetReadableData(), offset);
182 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
184 BOOST_ASSERT(packetBuffer);
186 return ReadUint16(packetBuffer->GetReadableData(), offset);
189 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
191 BOOST_ASSERT(packetBuffer);
193 return ReadUint8(packetBuffer->GetReadableData(), offset);
196 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
198 BOOST_ASSERT(buffer);
199 BOOST_ASSERT(outValue);
201 for (unsigned int i = 0; i < valueSize; i++, offset++)
203 outValue[i] = static_cast<uint8_t>(buffer[offset]);
207 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
209 BOOST_ASSERT(buffer);
212 value = static_cast<uint64_t>(buffer[offset]);
213 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
214 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
215 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
216 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
217 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
218 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
219 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
224 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
226 BOOST_ASSERT(buffer);
229 value = static_cast<uint32_t>(buffer[offset]);
230 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
231 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
232 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
236 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
238 BOOST_ASSERT(buffer);
241 value = static_cast<uint32_t>(buffer[offset]);
242 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
243 return static_cast<uint16_t>(value);
246 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
248 BOOST_ASSERT(buffer);
250 return buffer[offset];
253 std::string GetSoftwareInfo()
255 return std::string("ArmNN");
258 std::string GetHardwareVersion()
260 return std::string();
263 std::string GetSoftwareVersion()
265 std::string armnnVersion(ARMNN_VERSION);
266 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
270 std::string GetProcessName()
272 std::ifstream comm("/proc/self/comm");
278 /// Creates a timeline packet header
281 /// packetFamiliy Timeline Packet Family
282 /// packetClass Timeline Packet Class
283 /// packetType Timeline Packet Type
284 /// streamId Stream identifier
285 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
286 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
289 /// Pair of uint32_t containing word0 and word1 of the header
290 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
291 uint32_t packetClass,
294 uint32_t sequenceNumbered,
297 // Packet header word 0:
298 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
299 // 19:25 [7] packet_class: packet class
300 // 16:18 [3] packet_type: packet type
301 // 8:15 [8] reserved: all zeros
302 // 0:7 [8] stream_id: stream identifier
303 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
304 ((packetClass & 0x0000007F) << 19) |
305 ((packetType & 0x00000007) << 16) |
306 ((streamId & 0x00000007) << 0);
308 // Packet header word 1:
309 // 25:31 [7] reserved: all zeros
310 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
311 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
312 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
313 ((dataLength & 0x00FFFFFF) << 0);
315 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
318 // Calculate the actual length an SwString will be including the terminating null character
319 // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
320 // the size to allow the offset to be correctly updated when decoding a binary packet.
321 uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
323 std::vector<uint32_t> swTraceString;
324 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
325 unsigned int uint32_t_size = sizeof(uint32_t);
326 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
330 // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
331 SwTraceMessage ReadSwTraceMessage(const IPacketBufferPtr& packetBuffer, unsigned int& offset)
333 BOOST_ASSERT(packetBuffer);
335 unsigned int uint32_t_size = sizeof(uint32_t);
337 SwTraceMessage swTraceMessage;
340 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
341 swTraceMessage.id = readDeclId;
343 // SWTrace "namestring" format
344 // length of the string (first 4 bytes) + string + null terminator
346 // Check the decl_name
347 offset += uint32_t_size;
348 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
350 offset += uint32_t_size;
351 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
352 std::memcpy(swTraceStringBuffer.data(),
353 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
355 swTraceMessage.name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
358 offset += CalculateSizeOfPaddedSwString(swTraceMessage.name);
359 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
361 offset += uint32_t_size;
362 swTraceStringBuffer.resize(swTraceUINameLength - 1);
363 std::memcpy(swTraceStringBuffer.data(),
364 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
366 swTraceMessage.uiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
369 offset += CalculateSizeOfPaddedSwString(swTraceMessage.uiName);
370 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
372 offset += uint32_t_size;
373 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
374 std::memcpy(swTraceStringBuffer.data(),
375 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
377 swTraceMessage.argTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
379 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
382 offset += CalculateSizeOfPaddedSwString(swTraceString);
383 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
385 offset += uint32_t_size;
386 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
387 std::memcpy(swTraceStringBuffer.data(),
388 packetBuffer->GetReadableData() + offset, swTraceStringBuffer.size());
390 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
391 std::stringstream stringStream(swTraceString);
393 while (std::getline(stringStream, argName, ','))
395 swTraceMessage.argNames.push_back(argName);
398 offset += CalculateSizeOfPaddedSwString(swTraceString);
400 return swTraceMessage;
403 /// Creates a packet header for the timeline messages:
406 /// * declareEventClass
407 /// * declareRelationship
411 /// dataLength The length of the message body in bytes
414 /// Pair of uint32_t containing word0 and word1 of the header
415 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
417 return CreateTimelinePacketHeader(1, // Packet family
421 0, // Sequence number
422 dataLength); // Data length
425 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
426 const std::string& label,
427 unsigned char* buffer,
428 unsigned int bufferSize,
429 unsigned int& numberOfBytesWritten)
431 // Initialize the output value
432 numberOfBytesWritten = 0;
434 // Check that the given buffer is valid
435 if (buffer == nullptr || bufferSize == 0)
437 return TimelinePacketStatus::BufferExhaustion;
441 unsigned int uint32_t_size = sizeof(uint32_t);
442 unsigned int uint64_t_size = sizeof(uint64_t);
444 // Convert the label into a SWTrace string
445 std::vector<uint32_t> swTraceLabel;
446 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
449 return TimelinePacketStatus::Error;
452 // Calculate the size of the SWTrace string label (in bytes)
453 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
455 // Calculate the length of the data (in bytes)
456 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
457 uint64_t_size + // Profiling GUID
458 swTraceLabelSize; // Label
460 // Calculate the timeline binary packet size (in bytes)
461 unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words)
462 timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label
464 // Check whether the timeline binary packet fits in the given buffer
465 if (timelineLabelPacketSize > bufferSize)
467 return TimelinePacketStatus::BufferExhaustion;
470 // Create packet header
471 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineLabelPacketDataLength);
473 // Initialize the offset for writing in the buffer
474 unsigned int offset = 0;
476 // Write the timeline binary packet header to the buffer
477 WriteUint32(buffer, offset, packetHeader.first);
478 offset += uint32_t_size;
479 WriteUint32(buffer, offset, packetHeader.second);
480 offset += uint32_t_size;
482 // Write decl_Id to the buffer
483 WriteUint32(buffer, offset, 0u);
484 offset += uint32_t_size;
486 // Write the timeline binary packet payload to the buffer
487 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
488 offset += uint64_t_size;
489 for (uint32_t swTraceLabelWord : swTraceLabel)
491 WriteUint32(buffer, offset, swTraceLabelWord); // Label
492 offset += uint32_t_size;
495 // Update the number of bytes written
496 numberOfBytesWritten = timelineLabelPacketSize;
498 return TimelinePacketStatus::Ok;
501 TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
502 unsigned char* buffer,
503 unsigned int bufferSize,
504 unsigned int& numberOfBytesWritten)
506 // Initialize the output value
507 numberOfBytesWritten = 0;
509 // Check that the given buffer is valid
510 if (buffer == nullptr || bufferSize == 0)
512 return TimelinePacketStatus::BufferExhaustion;
516 unsigned int uint32_t_size = sizeof(uint32_t);
517 unsigned int uint64_t_size = sizeof(uint64_t);
519 // Calculate the length of the data (in bytes)
520 unsigned int timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID
523 // Calculate the timeline binary packet size (in bytes)
524 unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words)
525 uint32_t_size + // decl_Id
526 timelineEntityPacketDataLength; // Profiling GUID
528 // Check whether the timeline binary packet fits in the given buffer
529 if (timelineEntityPacketSize > bufferSize)
531 return TimelinePacketStatus::BufferExhaustion;
534 // Create packet header
535 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEntityPacketDataLength);
537 // Initialize the offset for writing in the buffer
538 unsigned int offset = 0;
540 // Write the timeline binary packet header to the buffer
541 WriteUint32(buffer, offset, packetHeader.first);
542 offset += uint32_t_size;
543 WriteUint32(buffer, offset, packetHeader.second);
544 offset += uint32_t_size;
546 // Write the decl_Id to the buffer
547 WriteUint32(buffer, offset, 1u);
548 offset += uint32_t_size;
550 // Write the timeline binary packet payload to the buffer
551 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
553 // Update the number of bytes written
554 numberOfBytesWritten = timelineEntityPacketSize;
556 return TimelinePacketStatus::Ok;
559 TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
560 uint64_t relationshipGuid,
563 unsigned char* buffer,
564 unsigned int bufferSize,
565 unsigned int& numberOfBytesWritten)
567 // Initialize the output value
568 numberOfBytesWritten = 0;
570 // Check that the given buffer is valid
571 if (buffer == nullptr || bufferSize == 0)
573 return TimelinePacketStatus::BufferExhaustion;
577 unsigned int uint32_t_size = sizeof(uint32_t);
578 unsigned int uint64_t_size = sizeof(uint64_t);
580 // Calculate the length of the data (in bytes)
581 unsigned int timelineRelationshipPacketDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
582 uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID
584 // Calculate the timeline binary packet size (in bytes)
585 unsigned int timelineRelationshipPacketSize = 2 * uint32_t_size + // Header (2 words)
586 timelineRelationshipPacketDataLength;
588 // Check whether the timeline binary packet fits in the given buffer
589 if (timelineRelationshipPacketSize > bufferSize)
591 return TimelinePacketStatus::BufferExhaustion;
594 // Create packet header
595 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineRelationshipPacketDataLength);
596 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
598 // Initialize the offset for writing in the buffer
599 unsigned int offset = 0;
601 // Write the timeline binary packet header to the buffer
602 WriteUint32(buffer, offset, packetHeader.first);
603 offset += uint32_t_size;
604 WriteUint32(buffer, offset, packetHeader.second);
605 offset += uint32_t_size;
607 uint32_t relationshipTypeUint = 0;
609 switch (relationshipType)
611 case ProfilingRelationshipType::RetentionLink:
612 relationshipTypeUint = 0;
614 case ProfilingRelationshipType::ExecutionLink:
615 relationshipTypeUint = 1;
617 case ProfilingRelationshipType::DataLink:
618 relationshipTypeUint = 2;
620 case ProfilingRelationshipType::LabelLink:
621 relationshipTypeUint = 3;
624 throw InvalidArgumentException("Unknown relationship type given.");
627 // Write the timeline binary packet payload to the buffer
628 // decl_id of the timeline message
630 WriteUint32(buffer, offset, declId); // decl_id
631 offset += uint32_t_size;
632 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
633 offset += uint32_t_size;
634 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
635 offset += uint64_t_size;
636 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
637 offset += uint64_t_size;
638 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
640 // Update the number of bytes written
641 numberOfBytesWritten = timelineRelationshipPacketSize;
643 return TimelinePacketStatus::Ok;
646 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
647 unsigned int bufferSize,
648 unsigned int& numberOfBytesWritten)
650 // Initialize the output value
651 numberOfBytesWritten = 0;
653 // Check that the given buffer is valid
654 if (buffer == nullptr || bufferSize == 0)
656 return TimelinePacketStatus::BufferExhaustion;
660 unsigned int uint32_t_size = sizeof(uint32_t);
662 // The payload/data of the packet consists of swtrace event definitions encoded according
663 // to the swtrace directory specification. The messages being the five defined below:
664 // | decl_id | decl_name | ui_name | arg_types | arg_names |
665 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
666 // | 0 | declareLabel | declare label | ps | guid,value |
667 // | 1 | declareEntity | declare entity | p | guid |
668 // | 2 | declareEventClass | declare event class | p | guid |
669 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
670 // | | | | | headGuid,tailGuid |
671 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
673 std::vector<std::vector<std::string>> timelineDirectoryMessages
675 {"declareLabel", "declare label", "ps", "guid,value"},
676 {"declareEntity", "declare entity", "p", "guid"},
677 {"declareEventClass", "declare event class", "p", "guid"},
678 {"declareRelationship", "declare relationship", "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
679 {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"}
682 unsigned int messagesDataLength = 0u;
683 std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
685 for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
687 messagesDataLength += uint32_t_size; // decl_id
689 std::vector<std::vector<uint32_t>> swTraceStringsVector;
690 for (const auto& label : timelineDirectoryMessage)
692 std::vector<uint32_t> swTraceString;
693 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
696 return TimelinePacketStatus::Error;
699 messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
700 swTraceStringsVector.push_back(swTraceString);
702 swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
705 // Calculate the timeline directory binary packet size (in bytes)
706 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
707 messagesDataLength; // 5 messages length
709 // Check whether the timeline directory binary packet fits in the given buffer
710 if (timelineDirectoryPacketSize > bufferSize)
712 return TimelinePacketStatus::BufferExhaustion;
715 // Create packet header
716 uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
717 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, dataLength);
719 // Initialize the offset for writing in the buffer
720 unsigned int offset = 0;
722 // Write the timeline binary packet header to the buffer
723 WriteUint32(buffer, offset, packetHeader.first);
724 offset += uint32_t_size;
725 WriteUint32(buffer, offset, packetHeader.second);
726 offset += uint32_t_size;
728 for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
730 // Write the timeline binary packet payload to the buffer
731 WriteUint32(buffer, offset, i); // decl_id
732 offset += uint32_t_size;
734 for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
736 for (uint32_t swTraceDeclStringWord : swTraceString)
738 WriteUint32(buffer, offset, swTraceDeclStringWord);
739 offset += uint32_t_size;
744 // Update the number of bytes written
745 numberOfBytesWritten = timelineDirectoryPacketSize;
747 return TimelinePacketStatus::Ok;
750 TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
751 unsigned char* buffer,
752 unsigned int bufferSize,
753 unsigned int& numberOfBytesWritten)
755 // Initialize the output value
756 numberOfBytesWritten = 0;
758 // Check that the given buffer is valid
759 if (buffer == nullptr || bufferSize == 0)
761 return TimelinePacketStatus::BufferExhaustion;
765 unsigned int uint32_t_size = sizeof(uint32_t);
766 unsigned int uint64_t_size = sizeof(uint64_t);
768 // decl_id of the timeline message
771 // Calculate the length of the data (in bytes)
772 unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
774 // Calculate the timeline binary packet size (in bytes)
775 unsigned int packetSize = 2 * uint32_t_size + // Header (2 words)
776 packetBodySize; // Body
778 // Check whether the timeline binary packet fits in the given buffer
779 if (packetSize > bufferSize)
781 return TimelinePacketStatus::BufferExhaustion;
784 // Create packet header
785 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(packetBodySize);
787 // Initialize the offset for writing in the buffer
788 unsigned int offset = 0;
790 // Write the timeline binary packet header to the buffer
791 WriteUint32(buffer, offset, packetHeader.first);
792 offset += uint32_t_size;
793 WriteUint32(buffer, offset, packetHeader.second);
794 offset += uint32_t_size;
796 // Write the timeline binary packet payload to the buffer
797 WriteUint32(buffer, offset, declId); // decl_id
798 offset += uint32_t_size;
799 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
801 // Update the number of bytes written
802 numberOfBytesWritten = packetSize;
804 return TimelinePacketStatus::Ok;
807 TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
808 std::thread::id threadId,
809 uint64_t profilingGuid,
810 unsigned char* buffer,
811 unsigned int bufferSize,
812 unsigned int& numberOfBytesWritten)
814 // Initialize the output value
815 numberOfBytesWritten = 0;
817 // Check that the given buffer is valid
818 if (buffer == nullptr || bufferSize == 0)
820 return TimelinePacketStatus::BufferExhaustion;
824 unsigned int uint32_t_size = sizeof(uint32_t);
825 unsigned int uint64_t_size = sizeof(uint64_t);
826 unsigned int threadId_size = sizeof(std::thread::id);
828 // decl_id of the timeline message
831 // Calculate the length of the data (in bytes)
832 unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id
833 uint64_t_size + // Timestamp
834 threadId_size + // Thread id
835 uint64_t_size; // Profiling GUID
837 // Calculate the timeline binary packet size (in bytes)
838 unsigned int timelineEventPacketSize = 2 * uint32_t_size + // Header (2 words)
839 timelineEventPacketDataLength; // Timestamp + thread id + profiling GUID
841 // Check whether the timeline binary packet fits in the given buffer
842 if (timelineEventPacketSize > bufferSize)
844 return TimelinePacketStatus::BufferExhaustion;
847 // Create packet header
848 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEventPacketDataLength);
850 // Initialize the offset for writing in the buffer
851 unsigned int offset = 0;
853 // Write the timeline binary packet header to the buffer
854 WriteUint32(buffer, offset, packetHeader.first);
855 offset += uint32_t_size;
856 WriteUint32(buffer, offset, packetHeader.second);
857 offset += uint32_t_size;
859 // Write the timeline binary packet payload to the buffer
860 WriteUint32(buffer, offset, declId); // decl_id
861 offset += uint32_t_size;
862 WriteUint64(buffer, offset, timestamp); // Timestamp
863 offset += uint64_t_size;
864 WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id
865 offset += threadId_size;
866 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
867 offset += uint64_t_size;
869 // Update the number of bytes written
870 numberOfBytesWritten = timelineEventPacketSize;
872 return TimelinePacketStatus::Ok;
875 } // namespace profiling
882 bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
884 return std::memcmp(left.data(), &right, left.size()) == 0;