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 WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
92 BOOST_ASSERT(packetBuffer);
94 WriteUint64(packetBuffer->GetWritableData(), offset, value);
97 void WriteUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint32_t value)
99 BOOST_ASSERT(packetBuffer);
101 WriteUint32(packetBuffer->GetWritableData(), offset, value);
104 void WriteUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint16_t value)
106 BOOST_ASSERT(packetBuffer);
108 WriteUint16(packetBuffer->GetWritableData(), offset, value);
111 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
113 BOOST_ASSERT(buffer);
115 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
116 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
117 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
118 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
119 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
120 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
121 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
122 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
125 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
127 BOOST_ASSERT(buffer);
129 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
130 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
131 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
132 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
135 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
137 BOOST_ASSERT(buffer);
139 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
140 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
143 uint64_t ReadUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
145 BOOST_ASSERT(packetBuffer);
147 return ReadUint64(packetBuffer->GetReadableData(), offset);
150 uint32_t ReadUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
152 BOOST_ASSERT(packetBuffer);
154 return ReadUint32(packetBuffer->GetReadableData(), offset);
157 uint16_t ReadUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
159 BOOST_ASSERT(packetBuffer);
161 return ReadUint16(packetBuffer->GetReadableData(), offset);
164 uint8_t ReadUint8(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
166 BOOST_ASSERT(packetBuffer);
168 return ReadUint8(packetBuffer->GetReadableData(), offset);
171 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
173 BOOST_ASSERT(buffer);
176 value = static_cast<uint64_t>(buffer[offset]);
177 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
178 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
179 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
180 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
181 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
182 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
183 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
188 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
190 BOOST_ASSERT(buffer);
193 value = static_cast<uint32_t>(buffer[offset]);
194 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
195 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
196 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
200 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
202 BOOST_ASSERT(buffer);
205 value = static_cast<uint32_t>(buffer[offset]);
206 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
207 return static_cast<uint16_t>(value);
210 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
212 BOOST_ASSERT(buffer);
214 return buffer[offset];
217 std::string GetSoftwareInfo()
219 return std::string("ArmNN");
222 std::string GetHardwareVersion()
224 return std::string();
227 std::string GetSoftwareVersion()
229 std::string armnnVersion(ARMNN_VERSION);
230 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
234 std::string GetProcessName()
236 std::ifstream comm("/proc/self/comm");
242 /// Creates a timeline packet header
245 /// packetFamiliy Timeline Packet Family
246 /// packetClass Timeline Packet Class
247 /// packetType Timeline Packet Type
248 /// streamId Stream identifier
249 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
250 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted.
253 /// Pair of uint32_t containing word0 and word1 of the header
254 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
255 uint32_t packetClass,
258 uint32_t sequenceNumbered,
261 // Packet header word 0:
262 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
263 // 19:25 [7] packet_class: packet class
264 // 16:18 [3] packet_type: packet type
265 // 8:15 [8] reserved: all zeros
266 // 0:7 [8] stream_id: stream identifier
267 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
268 ((packetClass & 0x0000007F) << 19) |
269 ((packetType & 0x00000007) << 16) |
270 ((streamId & 0x00000007) << 0);
272 // Packet header word 1:
273 // 25:31 [7] reserved: all zeros
274 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
275 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
276 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
277 ((dataLength & 0x00FFFFFF) << 0);
279 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
282 /// Creates a packet header for the timeline messages:
285 /// * declareEventClass
286 /// * declareRelationship
290 /// Data lenght of the message body in byte
293 /// Pair of uint32_t containing word0 and word1 of the header
294 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
296 return CreateTimelinePacketHeader(1,0,1,0,0,dataLength);
299 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
300 const std::string& label,
301 unsigned char* buffer,
302 unsigned int bufferSize,
303 unsigned int& numberOfBytesWritten)
305 // Initialize the ouput value
306 numberOfBytesWritten = 0;
308 // Check that the given buffer is valid
309 if (buffer == nullptr || bufferSize == 0)
311 return TimelinePacketStatus::BufferExhaustion;
315 unsigned int uint32_t_size = sizeof(uint32_t);
316 unsigned int uint64_t_size = sizeof(uint64_t);
318 // Convert the label into a SWTrace string
319 std::vector<uint32_t> swTraceLabel;
320 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
323 return TimelinePacketStatus::Error;
326 // Calculate the size of the SWTrace string label (in bytes)
327 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
329 // Calculate the length of the data (in bytes)
330 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
331 uint64_t_size + // Profiling GUID
332 swTraceLabelSize; // Label
334 // Calculate the timeline binary packet size (in bytes)
335 unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words)
336 timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label
338 // Check whether the timeline binary packet fits in the given buffer
339 if (timelineLabelPacketSize > bufferSize)
341 return TimelinePacketStatus::BufferExhaustion;
344 // Create packet header
345 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineLabelPacketDataLength); // decl_id + GUID + label
346 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
348 // Initialize the offset for writing in the buffer
349 unsigned int offset = 0;
351 // Write the timeline binary packet header to the buffer
352 WriteUint32(buffer, offset, packetHeader.first);
353 offset += uint32_t_size;
354 WriteUint32(buffer, offset, packetHeader.second);
355 offset += uint32_t_size;
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 = timelineLabelPacketSize;
373 return TimelinePacketStatus::Ok;
376 TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
377 unsigned char* buffer,
378 unsigned int bufferSize,
379 unsigned int& numberOfBytesWritten)
381 // Initialize the ouput value
382 numberOfBytesWritten = 0;
384 // Check that the given buffer is valid
385 if (buffer == nullptr || bufferSize == 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 timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID
398 // Calculate the timeline binary packet size (in bytes)
399 unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words)
400 uint32_t_size + // decl_Id
401 timelineEntityPacketDataLength; // Profiling GUID
403 // Check whether the timeline binary packet fits in the given buffer
404 if (timelineEntityPacketSize > bufferSize)
406 return TimelinePacketStatus::BufferExhaustion;
409 // Create packet header
410 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineEntityPacketDataLength);
411 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
413 // Initialize the offset for writing in the buffer
414 unsigned int offset = 0;
416 // Write the timeline binary packet header to the buffer
417 WriteUint32(buffer, offset, packetHeader.first);
418 offset += uint32_t_size;
419 WriteUint32(buffer, offset, packetHeader.second);
420 offset += uint32_t_size;
422 // Write the decl_Id to the buffer
423 WriteUint32(buffer, offset, 1u);
424 offset += uint32_t_size;
426 // Write the timeline binary packet payload to the buffer
427 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
429 // Update the number of bytes written
430 numberOfBytesWritten = timelineEntityPacketSize;
432 return TimelinePacketStatus::Ok;
435 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
436 unsigned int bufferSize,
437 unsigned int& numberOfBytesWritten)
439 // Initialize the output value
440 numberOfBytesWritten = 0;
442 // Check that the given buffer is valid
443 if (buffer == nullptr || bufferSize == 0)
445 return TimelinePacketStatus::BufferExhaustion;
449 unsigned int uint32_t_size = sizeof(uint32_t);
451 // the payload/data of the packet consists of swtrace event definitions encoded according
452 // to the swtrace directory specification. The messages being the five defined below:
453 // | decl_id | decl_name | ui_name | arg_types | arg_names |
454 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
455 // | 0 | declareLabel | declare label | ps | guid,value |
456 // | 1 | declareEntity | declare entity | p | guid |
457 // | 2 | declareEventClass | declare event class | p | guid |
458 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid,
459 // headGuid,tailGuid |
460 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
462 std::vector<std::vector<std::string>> timelineDirectoryMessages =
463 { {"declareLabel", "declare label", "ps", "guid,value"},
464 {"declareEntity", "declare entity", "p", "guid"},
465 {"declareEventClass", "declare event class", "p", "guid"},
466 {"declareRelationship", "declare relationship",
467 "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
468 {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"} };
470 unsigned int messagesDataLength = 0u;
471 std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
473 for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
475 messagesDataLength += uint32_t_size; // decl_id
477 std::vector<std::vector<uint32_t>> swTraceStringsVector;
478 for (const auto& label : timelineDirectoryMessage)
480 std::vector<uint32_t> swTraceString;
481 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
484 return TimelinePacketStatus::Error;
487 messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
488 swTraceStringsVector.push_back(swTraceString);
490 swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
493 // Calculate the timeline directory binary packet size (in bytes)
494 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
495 messagesDataLength; // 5 messages length
497 // Check whether the timeline directory binary packet fits in the given buffer
498 if (timelineDirectoryPacketSize > bufferSize)
500 return TimelinePacketStatus::BufferExhaustion;
503 // Create packet header
504 uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
505 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1,0,0,0,0,dataLength);
507 // Initialize the offset for writing in the buffer
508 unsigned int offset = 0;
510 // Write the timeline binary packet header to the buffer
511 WriteUint32(buffer, offset, packetHeader.first);
512 offset += uint32_t_size;
513 WriteUint32(buffer, offset, packetHeader.second);
514 offset += uint32_t_size;
516 for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
518 // Write the timeline binary packet payload to the buffer
519 WriteUint32(buffer, offset, i); // decl_id
520 offset += uint32_t_size;
522 for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
524 for (uint32_t swTraceDeclStringWord : swTraceString)
526 WriteUint32(buffer, offset, swTraceDeclStringWord);
527 offset += uint32_t_size;
532 // Update the number of bytes written
533 numberOfBytesWritten = timelineDirectoryPacketSize;
535 return TimelinePacketStatus::Ok;
538 TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
539 unsigned char* buffer,
540 unsigned int bufferSize,
541 unsigned int& numberOfBytesWritten)
543 // Initialize the ouput value
544 numberOfBytesWritten = 0;
546 // Check that the given buffer is valid
547 if (buffer == nullptr || bufferSize == 0)
549 return TimelinePacketStatus::BufferExhaustion;
553 unsigned int uint32_t_size = sizeof(uint32_t);
554 unsigned int uint64_t_size = sizeof(uint64_t);
556 // dec_id of the timeline message
559 // Calculate the length of the data (in bytes)
560 unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
562 // Calculate the timeline binary packet size (in bytes)
563 unsigned int packetSize = 2 * uint32_t_size + // Header (2 words)
564 packetBodySize; // Body
566 // Check whether the timeline binary packet fits in the given buffer
567 if (packetSize > bufferSize)
569 return TimelinePacketStatus::BufferExhaustion;
572 // Create packet header
573 uint32_t dataLength = boost::numeric_cast<uint32_t>(packetBodySize);
574 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
576 // Initialize the offset for writing in the buffer
577 unsigned int offset = 0;
579 // Write the timeline binary packet header to the buffer
580 WriteUint32(buffer, offset, packetHeader.first);
581 offset += uint32_t_size;
582 WriteUint32(buffer, offset, packetHeader.second);
583 offset += uint32_t_size;
585 // Write the timeline binary packet payload to the buffer
586 WriteUint32(buffer, offset, decId); // dec_id
587 offset += uint32_t_size;
588 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
590 // Update the number of bytes written
591 numberOfBytesWritten = packetSize;
593 return TimelinePacketStatus::Ok;
596 } // namespace profiling