IVGCVSW-3951 Create the timeline decoder
[platform/upstream/armnn.git] / src / profiling / ProfilingUtils.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ProfilingUtils.hpp"
7
8 #include <armnn/Version.hpp>
9 #include <armnn/Conversion.hpp>
10
11 #include <WallClockTimer.hpp>
12
13 #include <boost/assert.hpp>
14
15 #include <fstream>
16 #include <iostream>
17 #include <limits>
18
19 namespace armnn
20 {
21
22 namespace profiling
23 {
24
25 namespace
26 {
27
28 void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
29 {
30     // Check that it is possible to generate the next UID without causing an overflow
31     switch (cores)
32     {
33     case 0:
34     case 1:
35         // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
36         // running multiple parallel workloads and will not provide multiple streams of data for each event)
37         if (uid == std::numeric_limits<uint16_t>::max())
38         {
39             throw RuntimeException("Generating the next UID for profiling would result in an overflow");
40         }
41         break;
42     default: // cores > 1
43         // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
44         // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
45         if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
46         {
47             throw RuntimeException("Generating the next UID for profiling would result in an overflow");
48         }
49         break;
50     }
51 }
52
53 } // Anonymous namespace
54
55 uint16_t GetNextUid(bool peekOnly)
56 {
57     // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
58     static uint16_t uid = 1;
59
60     // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
61     ThrowIfCantGenerateNextUid(uid);
62
63     if (peekOnly)
64     {
65         // Peek only
66         return uid;
67     }
68     else
69     {
70         // Get the next UID
71         return uid++;
72     }
73 }
74
75 std::vector<uint16_t> GetNextCounterUids(uint16_t cores)
76 {
77     // The UID used for counters only. The first valid UID is 0
78     static uint16_t counterUid = 0;
79
80     // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
81     ThrowIfCantGenerateNextUid(counterUid, cores);
82
83     // Get the next counter UIDs
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++)
87     {
88         counterUids[i] = counterUid++;
89     }
90     return counterUids;
91 }
92
93 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset,  const void* value, unsigned int valueSize)
94 {
95     BOOST_ASSERT(packetBuffer);
96
97     WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
98 }
99
100 uint32_t ConstructHeader(uint32_t packetFamily,
101                          uint32_t packetId)
102 {
103     return ((packetFamily & 0x3F) << 26)|
104            ((packetId & 0x3FF) << 16);
105 }
106
107 uint32_t ConstructHeader(uint32_t packetFamily,
108                          uint32_t packetClass,
109                          uint32_t packetType)
110 {
111     return ((packetFamily & 0x3F) << 26)|
112            ((packetClass & 0x3FF) << 19)|
113            ((packetType & 0x3FFF) << 16);
114 }
115
116 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
117 {
118     BOOST_ASSERT(packetBuffer);
119
120     WriteUint64(packetBuffer->GetWritableData(), offset, value);
121 }
122
123 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
124 {
125     BOOST_ASSERT(packetBuffer);
126
127     WriteUint32(packetBuffer->GetWritableData(), offset, value);
128 }
129
130 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
131 {
132     BOOST_ASSERT(packetBuffer);
133
134     WriteUint16(packetBuffer->GetWritableData(), offset, value);
135 }
136
137 void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
138 {
139     BOOST_ASSERT(buffer);
140     BOOST_ASSERT(value);
141
142     for (unsigned int i = 0; i < valueSize; i++, offset++)
143     {
144         buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
145     }
146 }
147
148 void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
149 {
150     BOOST_ASSERT(buffer);
151
152     buffer[offset]     = static_cast<unsigned char>(value & 0xFF);
153     buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
154     buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
155     buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
156     buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
157     buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
158     buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
159     buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
160 }
161
162 void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
163 {
164     BOOST_ASSERT(buffer);
165
166     buffer[offset]     = static_cast<unsigned char>(value & 0xFF);
167     buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
168     buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
169     buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
170 }
171
172 void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
173 {
174     BOOST_ASSERT(buffer);
175
176     buffer[offset]     = static_cast<unsigned char>(value & 0xFF);
177     buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
178 }
179
180 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
181 {
182     BOOST_ASSERT(packetBuffer);
183
184     ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
185 }
186
187 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
188 {
189     BOOST_ASSERT(packetBuffer);
190
191     return ReadUint64(packetBuffer->GetReadableData(), offset);
192 }
193
194 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
195 {
196     BOOST_ASSERT(packetBuffer);
197
198     return ReadUint32(packetBuffer->GetReadableData(), offset);
199 }
200
201 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
202 {
203     BOOST_ASSERT(packetBuffer);
204
205     return ReadUint16(packetBuffer->GetReadableData(), offset);
206 }
207
208 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
209 {
210     BOOST_ASSERT(packetBuffer);
211
212     return ReadUint8(packetBuffer->GetReadableData(), offset);
213 }
214
215 void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
216 {
217     BOOST_ASSERT(buffer);
218     BOOST_ASSERT(outValue);
219
220     for (unsigned int i = 0; i < valueSize; i++, offset++)
221     {
222         outValue[i] = static_cast<uint8_t>(buffer[offset]);
223     }
224 }
225
226 uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
227 {
228     BOOST_ASSERT(buffer);
229
230     uint64_t value = 0;
231     value  = static_cast<uint64_t>(buffer[offset]);
232     value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
233     value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
234     value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
235     value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
236     value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
237     value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
238     value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
239
240     return value;
241 }
242
243 uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
244 {
245     BOOST_ASSERT(buffer);
246
247     uint32_t value = 0;
248     value  = static_cast<uint32_t>(buffer[offset]);
249     value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
250     value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
251     value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
252     return value;
253 }
254
255 uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
256 {
257     BOOST_ASSERT(buffer);
258
259     uint32_t value = 0;
260     value  = static_cast<uint32_t>(buffer[offset]);
261     value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
262     return static_cast<uint16_t>(value);
263 }
264
265 uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
266 {
267     BOOST_ASSERT(buffer);
268
269     return buffer[offset];
270 }
271
272 std::string GetSoftwareInfo()
273 {
274     return std::string("ArmNN");
275 }
276
277 std::string GetHardwareVersion()
278 {
279     return std::string();
280 }
281
282 std::string GetSoftwareVersion()
283 {
284     std::string armnnVersion(ARMNN_VERSION);
285     std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
286     return result;
287 }
288
289 std::string GetProcessName()
290 {
291     std::ifstream comm("/proc/self/comm");
292     std::string name;
293     getline(comm, name);
294     return name;
295 }
296
297 /// Creates a timeline packet header
298 ///
299 /// \params
300 ///   packetFamiliy     Timeline Packet Family
301 ///   packetClass       Timeline Packet Class
302 ///   packetType        Timeline Packet Type
303 ///   streamId          Stream identifier
304 ///   seqeunceNumbered  When non-zero the 4 bytes following the header is a u32 sequence number
305 ///   dataLength        Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
306 ///
307 /// \returns
308 ///   Pair of uint32_t containing word0 and word1 of the header
309 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
310                                                          uint32_t packetClass,
311                                                          uint32_t packetType,
312                                                          uint32_t streamId,
313                                                          uint32_t sequenceNumbered,
314                                                          uint32_t dataLength)
315 {
316     // Packet header word 0:
317     // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
318     // 19:25 [7] packet_class: packet class
319     // 16:18 [3] packet_type: packet type
320     // 8:15  [8] reserved: all zeros
321     // 0:7   [8] stream_id: stream identifier
322     uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
323                                  ((packetClass  & 0x0000007F) << 19) |
324                                  ((packetType   & 0x00000007) << 16) |
325                                  ((streamId     & 0x00000007) <<  0);
326
327     // Packet header word 1:
328     // 25:31 [7]  reserved: all zeros
329     // 24    [1]  sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
330     // 0:23  [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
331     uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
332                                  ((dataLength       & 0x00FFFFFF) <<  0);
333
334     return std::make_pair(packetHeaderWord0, packetHeaderWord1);
335 }
336
337 // Calculate the actual length an SwString will be including the terminating null character
338 // padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
339 // the size to allow the offset to be correctly updated when decoding a binary packet.
340 uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
341 {
342     std::vector<uint32_t> swTraceString;
343     StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
344     unsigned int uint32_t_size = sizeof(uint32_t);
345     uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
346     return size;
347 }
348
349 // Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
350 SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
351 {
352     BOOST_ASSERT(packetBuffer);
353
354     unsigned int uint32_t_size = sizeof(uint32_t);
355
356     SwTraceMessage swTraceMessage;
357
358     // Read the decl_id
359     uint32_t readDeclId = ReadUint32(packetBuffer, offset);
360     swTraceMessage.id = readDeclId;
361
362     // SWTrace "namestring" format
363     // length of the string (first 4 bytes) + string + null terminator
364
365     // Check the decl_name
366     offset += uint32_t_size;
367     uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
368
369     offset += uint32_t_size;
370     std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
371     std::memcpy(swTraceStringBuffer.data(),
372                 packetBuffer + offset, swTraceStringBuffer.size());
373
374     swTraceMessage.name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
375
376     // Check the ui_name
377     offset += CalculateSizeOfPaddedSwString(swTraceMessage.name);
378     uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
379
380     offset += uint32_t_size;
381     swTraceStringBuffer.resize(swTraceUINameLength - 1);
382     std::memcpy(swTraceStringBuffer.data(),
383                 packetBuffer  + offset, swTraceStringBuffer.size());
384
385     swTraceMessage.uiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
386
387     // Check arg_types
388     offset += CalculateSizeOfPaddedSwString(swTraceMessage.uiName);
389     uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
390
391     offset += uint32_t_size;
392     swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
393     std::memcpy(swTraceStringBuffer.data(),
394                 packetBuffer  + offset, swTraceStringBuffer.size());
395
396     swTraceMessage.argTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
397
398     std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
399
400     // Check arg_names
401     offset += CalculateSizeOfPaddedSwString(swTraceString);
402     uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
403
404     offset += uint32_t_size;
405     swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
406     std::memcpy(swTraceStringBuffer.data(),
407                 packetBuffer  + offset, swTraceStringBuffer.size());
408
409     swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
410     std::stringstream stringStream(swTraceString);
411     std::string argName;
412     while (std::getline(stringStream, argName, ','))
413     {
414         swTraceMessage.argNames.push_back(argName);
415     }
416
417     offset += CalculateSizeOfPaddedSwString(swTraceString);
418
419     return swTraceMessage;
420 }
421
422 /// Creates a packet header for the timeline messages:
423 /// * declareLabel
424 /// * declareEntity
425 /// * declareEventClass
426 /// * declareRelationship
427 /// * declareEvent
428 ///
429 /// \param
430 ///   dataLength The length of the message body in bytes
431 ///
432 /// \returns
433 ///   Pair of uint32_t containing word0 and word1 of the header
434 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
435 {
436     return CreateTimelinePacketHeader(1,           // Packet family
437                                       0,           // Packet class
438                                       1,           // Packet type
439                                       0,           // Stream id
440                                       0,           // Sequence number
441                                       dataLength); // Data length
442 }
443
444 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
445                                                     const std::string& label,
446                                                     unsigned char* buffer,
447                                                     unsigned int bufferSize,
448                                                     unsigned int& numberOfBytesWritten)
449 {
450     // Initialize the output value
451     numberOfBytesWritten = 0;
452
453     // Check that the given buffer is valid
454     if (buffer == nullptr || bufferSize == 0)
455     {
456         return TimelinePacketStatus::BufferExhaustion;
457     }
458
459     // Utils
460     unsigned int uint32_t_size = sizeof(uint32_t);
461     unsigned int uint64_t_size = sizeof(uint64_t);
462
463     // Convert the label into a SWTrace string
464     std::vector<uint32_t> swTraceLabel;
465     bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
466     if (!result)
467     {
468         return TimelinePacketStatus::Error;
469     }
470
471     // Calculate the size of the SWTrace string label (in bytes)
472     unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
473
474     // Calculate the length of the data (in bytes)
475     unsigned int timelineLabelPacketDataLength = uint32_t_size +   // decl_Id
476                                                  uint64_t_size +   // Profiling GUID
477                                                  swTraceLabelSize; // Label
478
479     // Calculate the timeline binary packet size (in bytes)
480     unsigned int timelineLabelPacketSize = 2 * uint32_t_size +            // Header (2 words)
481                                            timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label
482
483     // Check whether the timeline binary packet fits in the given buffer
484     if (timelineLabelPacketSize > bufferSize)
485     {
486         return TimelinePacketStatus::BufferExhaustion;
487     }
488
489     // Create packet header
490     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineLabelPacketDataLength);
491
492     // Initialize the offset for writing in the buffer
493     unsigned int offset = 0;
494
495     // Write the timeline binary packet header to the buffer
496     WriteUint32(buffer, offset, packetHeader.first);
497     offset += uint32_t_size;
498     WriteUint32(buffer, offset, packetHeader.second);
499     offset += uint32_t_size;
500
501     // Write decl_Id to the buffer
502     WriteUint32(buffer, offset, 0u);
503     offset += uint32_t_size;
504
505     // Write the timeline binary packet payload to the buffer
506     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
507     offset += uint64_t_size;
508     for (uint32_t swTraceLabelWord : swTraceLabel)
509     {
510         WriteUint32(buffer, offset, swTraceLabelWord); // Label
511         offset += uint32_t_size;
512     }
513
514     // Update the number of bytes written
515     numberOfBytesWritten = timelineLabelPacketSize;
516
517     return TimelinePacketStatus::Ok;
518 }
519
520 TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
521                                                      unsigned char* buffer,
522                                                      unsigned int bufferSize,
523                                                      unsigned int& numberOfBytesWritten)
524 {
525     // Initialize the output value
526     numberOfBytesWritten = 0;
527
528     // Check that the given buffer is valid
529     if (buffer == nullptr || bufferSize == 0)
530     {
531         return TimelinePacketStatus::BufferExhaustion;
532     }
533
534     // Utils
535     unsigned int uint32_t_size = sizeof(uint32_t);
536     unsigned int uint64_t_size = sizeof(uint64_t);
537
538     // Calculate the length of the data (in bytes)
539     unsigned int timelineEntityPacketDataLength = uint32_t_size + uint64_t_size;  // decl_id + Profiling GUID
540
541
542     // Calculate the timeline binary packet size (in bytes)
543     unsigned int timelineEntityPacketSize = 2 * uint32_t_size +             // Header (2 words)
544                                             timelineEntityPacketDataLength; // Profiling GUID
545
546     // Check whether the timeline binary packet fits in the given buffer
547     if (timelineEntityPacketSize > bufferSize)
548     {
549         return TimelinePacketStatus::BufferExhaustion;
550     }
551
552     // Create packet header
553     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEntityPacketDataLength);
554
555     // Initialize the offset for writing in the buffer
556     unsigned int offset = 0;
557
558     // Write the timeline binary packet header to the buffer
559     WriteUint32(buffer, offset, packetHeader.first);
560     offset += uint32_t_size;
561     WriteUint32(buffer, offset, packetHeader.second);
562     offset += uint32_t_size;
563
564     // Write the decl_Id to the buffer
565     WriteUint32(buffer, offset, 1u);
566     offset += uint32_t_size;
567
568     // Write the timeline binary packet payload to the buffer
569     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
570
571     // Update the number of bytes written
572     numberOfBytesWritten = timelineEntityPacketSize;
573
574     return TimelinePacketStatus::Ok;
575 }
576
577 TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
578                                                            uint64_t relationshipGuid,
579                                                            uint64_t headGuid,
580                                                            uint64_t tailGuid,
581                                                            unsigned char* buffer,
582                                                            unsigned int bufferSize,
583                                                            unsigned int& numberOfBytesWritten)
584 {
585     // Initialize the output value
586     numberOfBytesWritten = 0;
587
588     // Check that the given buffer is valid
589     if (buffer == nullptr || bufferSize == 0)
590     {
591         return TimelinePacketStatus::BufferExhaustion;
592     }
593
594     // Utils
595     unsigned int uint32_t_size = sizeof(uint32_t);
596     unsigned int uint64_t_size = sizeof(uint64_t);
597
598     // Calculate the length of the data (in bytes)
599     unsigned int timelineRelationshipPacketDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
600                                                         uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID
601
602     // Calculate the timeline binary packet size (in bytes)
603     unsigned int timelineRelationshipPacketSize = 2 * uint32_t_size + // Header (2 words)
604                                                   timelineRelationshipPacketDataLength;
605
606     // Check whether the timeline binary packet fits in the given buffer
607     if (timelineRelationshipPacketSize > bufferSize)
608     {
609         return TimelinePacketStatus::BufferExhaustion;
610     }
611
612     // Create packet header
613     uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineRelationshipPacketDataLength);
614     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
615
616     // Initialize the offset for writing in the buffer
617     unsigned int offset = 0;
618
619     // Write the timeline binary packet header to the buffer
620     WriteUint32(buffer, offset, packetHeader.first);
621     offset += uint32_t_size;
622     WriteUint32(buffer, offset, packetHeader.second);
623     offset += uint32_t_size;
624
625     uint32_t relationshipTypeUint = 0;
626
627     switch (relationshipType)
628     {
629         case ProfilingRelationshipType::RetentionLink:
630             relationshipTypeUint = 0;
631             break;
632         case ProfilingRelationshipType::ExecutionLink:
633             relationshipTypeUint = 1;
634             break;
635         case ProfilingRelationshipType::DataLink:
636             relationshipTypeUint = 2;
637             break;
638         case ProfilingRelationshipType::LabelLink:
639             relationshipTypeUint = 3;
640             break;
641         default:
642             throw InvalidArgumentException("Unknown relationship type given.");
643     }
644
645     // Write the timeline binary packet payload to the buffer
646     // decl_id of the timeline message
647     uint32_t declId = 3;
648     WriteUint32(buffer, offset, declId); // decl_id
649     offset += uint32_t_size;
650     WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
651     offset += uint32_t_size;
652     WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
653     offset += uint64_t_size;
654     WriteUint64(buffer, offset, headGuid); // head of relationship GUID
655     offset += uint64_t_size;
656     WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
657
658     // Update the number of bytes written
659     numberOfBytesWritten = timelineRelationshipPacketSize;
660
661     return TimelinePacketStatus::Ok;
662 }
663
664 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
665                                                           unsigned int bufferSize,
666                                                           unsigned int& numberOfBytesWritten)
667 {
668     // Initialize the output value
669     numberOfBytesWritten = 0;
670
671     // Check that the given buffer is valid
672     if (buffer == nullptr || bufferSize == 0)
673     {
674         return TimelinePacketStatus::BufferExhaustion;
675     }
676
677     // Utils
678     unsigned int uint32_t_size = sizeof(uint32_t);
679
680     // The payload/data of the packet consists of swtrace event definitions encoded according
681     // to the swtrace directory specification. The messages being the five defined below:
682     // |  decl_id  |  decl_name          |    ui_name            |  arg_types  |  arg_names                          |
683     // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
684     // |    0      |   declareLabel      |   declare label       |    ps       |  guid,value                         |
685     // |    1      |   declareEntity     |   declare entity      |    p        |  guid                               |
686     // |    2      | declareEventClass   |  declare event class  |    p        |  guid                               |
687     // |    3      | declareRelationship | declare relationship  |    Ippp     |  relationshipType,relationshipGuid, |
688     // |           |                     |                       |             |  headGuid,tailGuid                  |
689     // |    4      |   declareEvent      |   declare event       |    @tp      |  timestamp,threadId,eventGuid       |
690
691     std::vector<std::vector<std::string>> timelineDirectoryMessages
692     {
693         {"declareLabel", "declare label", "ps", "guid,value"},
694         {"declareEntity", "declare entity", "p", "guid"},
695         {"declareEventClass", "declare event class", "p", "guid"},
696         {"declareRelationship", "declare relationship", "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
697         {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"}
698     };
699
700     unsigned int messagesDataLength = 0u;
701     std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
702
703     for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
704     {
705         messagesDataLength += uint32_t_size; // decl_id
706
707         std::vector<std::vector<uint32_t>> swTraceStringsVector;
708         for (const auto& label : timelineDirectoryMessage)
709         {
710             std::vector<uint32_t> swTraceString;
711             bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
712             if (!result)
713             {
714                 return TimelinePacketStatus::Error;
715             }
716
717             messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
718             swTraceStringsVector.push_back(swTraceString);
719         }
720         swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
721     }
722
723     // Calculate the timeline directory binary packet size (in bytes)
724     unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
725                                                messagesDataLength; // 5 messages length
726
727     // Check whether the timeline directory binary packet fits in the given buffer
728     if (timelineDirectoryPacketSize > bufferSize)
729     {
730         return TimelinePacketStatus::BufferExhaustion;
731     }
732
733     // Create packet header
734     uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
735     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, dataLength);
736
737     // Initialize the offset for writing in the buffer
738     unsigned int offset = 0;
739
740     // Write the timeline binary packet header to the buffer
741     WriteUint32(buffer, offset, packetHeader.first);
742     offset += uint32_t_size;
743     WriteUint32(buffer, offset, packetHeader.second);
744     offset += uint32_t_size;
745
746     WriteUint32(buffer, offset, static_cast<uint32_t >(swTraceTimelineDirectoryMessages.size()));
747     offset += uint32_t_size;
748
749     for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
750     {
751         // Write the timeline binary packet payload to the buffer
752         WriteUint32(buffer, offset, i); // decl_id
753         offset += uint32_t_size;
754
755         for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
756         {
757             for (uint32_t swTraceDeclStringWord : swTraceString)
758             {
759                 WriteUint32(buffer, offset, swTraceDeclStringWord);
760                 offset += uint32_t_size;
761             }
762         }
763     }
764
765     // Update the number of bytes written
766     numberOfBytesWritten = timelineDirectoryPacketSize;
767
768     return TimelinePacketStatus::Ok;
769 }
770
771 TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
772                                                          unsigned char* buffer,
773                                                          unsigned int bufferSize,
774                                                          unsigned int& numberOfBytesWritten)
775 {
776     // Initialize the output value
777     numberOfBytesWritten = 0;
778
779     // Check that the given buffer is valid
780     if (buffer == nullptr || bufferSize == 0)
781     {
782         return TimelinePacketStatus::BufferExhaustion;
783     }
784
785     // Utils
786     unsigned int uint32_t_size = sizeof(uint32_t);
787     unsigned int uint64_t_size = sizeof(uint64_t);
788
789     // decl_id of the timeline message
790     uint32_t declId = 2;
791
792     // Calculate the length of the data (in bytes)
793     unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
794
795     // Calculate the timeline binary packet size (in bytes)
796     unsigned int packetSize = 2 * uint32_t_size + // Header (2 words)
797                               packetBodySize;     // Body
798
799     // Check whether the timeline binary packet fits in the given buffer
800     if (packetSize > bufferSize)
801     {
802         return TimelinePacketStatus::BufferExhaustion;
803     }
804
805     // Create packet header
806     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(packetBodySize);
807
808     // Initialize the offset for writing in the buffer
809     unsigned int offset = 0;
810
811     // Write the timeline binary packet header to the buffer
812     WriteUint32(buffer, offset, packetHeader.first);
813     offset += uint32_t_size;
814     WriteUint32(buffer, offset, packetHeader.second);
815     offset += uint32_t_size;
816
817     // Write the timeline binary packet payload to the buffer
818     WriteUint32(buffer, offset, declId);        // decl_id
819     offset += uint32_t_size;
820     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
821
822     // Update the number of bytes written
823     numberOfBytesWritten = packetSize;
824
825     return TimelinePacketStatus::Ok;
826 }
827
828 TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
829                                                     std::thread::id threadId,
830                                                     uint64_t profilingGuid,
831                                                     unsigned char* buffer,
832                                                     unsigned int bufferSize,
833                                                     unsigned int& numberOfBytesWritten)
834 {
835     // Initialize the output value
836     numberOfBytesWritten = 0;
837
838     // Check that the given buffer is valid
839     if (buffer == nullptr || bufferSize == 0)
840     {
841         return TimelinePacketStatus::BufferExhaustion;
842     }
843
844     // Utils
845     unsigned int uint32_t_size = sizeof(uint32_t);
846     unsigned int uint64_t_size = sizeof(uint64_t);
847     unsigned int threadId_size = sizeof(std::thread::id);
848
849     // decl_id of the timeline message
850     uint32_t declId = 4;
851
852     // Calculate the length of the data (in bytes)
853     unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id
854                                                  uint64_t_size + // Timestamp
855                                                  threadId_size + // Thread id
856                                                  uint64_t_size;  // Profiling GUID
857
858     // Calculate the timeline binary packet size (in bytes)
859     unsigned int timelineEventPacketSize = 2 * uint32_t_size +            // Header (2 words)
860                                            timelineEventPacketDataLength; // Timestamp + thread id + profiling GUID
861
862     // Check whether the timeline binary packet fits in the given buffer
863     if (timelineEventPacketSize > bufferSize)
864     {
865         return TimelinePacketStatus::BufferExhaustion;
866     }
867
868     // Create packet header
869     std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEventPacketDataLength);
870
871     // Initialize the offset for writing in the buffer
872     unsigned int offset = 0;
873
874     // Write the timeline binary packet header to the buffer
875     WriteUint32(buffer, offset, packetHeader.first);
876     offset += uint32_t_size;
877     WriteUint32(buffer, offset, packetHeader.second);
878     offset += uint32_t_size;
879
880     // Write the timeline binary packet payload to the buffer
881     WriteUint32(buffer, offset, declId); // decl_id
882     offset += uint32_t_size;
883     WriteUint64(buffer, offset, timestamp); // Timestamp
884     offset += uint64_t_size;
885     WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id
886     offset += threadId_size;
887     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
888     offset += uint64_t_size;
889
890     // Update the number of bytes written
891     numberOfBytesWritten = timelineEventPacketSize;
892
893     return TimelinePacketStatus::Ok;
894 }
895
896 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
897 {
898     std::stringstream outputStream, centrePadding;
899     int padding = spacingWidth - static_cast<int>(stringToPass.size());
900
901     for (int i = 0; i < padding / 2; ++i)
902     {
903         centrePadding << " ";
904     }
905
906     outputStream << centrePadding.str() << stringToPass << centrePadding.str();
907
908     if (padding > 0 && padding %2 != 0)
909     {
910         outputStream << " ";
911     }
912
913     return outputStream.str();
914 }
915
916 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
917 {
918     std::string body;
919
920     body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
921     body.append(" | ");
922     body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
923     body.append(" | ");
924     body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
925     body.append("\n");
926
927     std::cout << std::string(body.size(), '-') << "\n";
928     std::cout<< body;
929 }
930
931 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
932 {
933     std::string body;
934
935     body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
936     body.append(" | ");
937     body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
938     body.append(" | ");
939     body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
940     body.append("\n");
941
942     std::cout << std::string(body.size(), '-') << "\n";
943
944     std::cout<< body;
945 }
946
947 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
948 {
949     std::string body;
950
951     body.append(CentreAlignFormatting(counter->m_Name, 20));
952     body.append(" | ");
953     body.append(CentreAlignFormatting(counter->m_Description, 50));
954     body.append(" | ");
955     body.append(CentreAlignFormatting(counter->m_Units, 14));
956     body.append(" | ");
957     body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
958     body.append(" | ");
959     body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
960     body.append(" | ");
961     body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
962     body.append(" | ");
963     body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
964     body.append(" | ");
965     body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
966     body.append(" | ");
967     body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
968     body.append(" | ");
969     body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
970
971     body.append("\n");
972
973     std::cout << std::string(body.size(), '-') << "\n";
974
975     std::cout << body;
976 }
977
978 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
979                           std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
980 {
981     std::string categoryBody;
982     std::string categoryHeader;
983
984     categoryHeader.append(CentreAlignFormatting("Name", 20));
985     categoryHeader.append(" | ");
986     categoryHeader.append(CentreAlignFormatting("Device", 12));
987     categoryHeader.append(" | ");
988     categoryHeader.append(CentreAlignFormatting("Counter set UID:", 16));
989     categoryHeader.append(" | ");
990     categoryHeader.append(CentreAlignFormatting("Event Count", 14));
991     categoryHeader.append("\n");
992
993     categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
994     categoryBody.append(" | ");
995     categoryBody.append(CentreAlignFormatting(std::to_string(category->m_DeviceUid), 12));
996     categoryBody.append(" | ");
997     categoryBody.append(CentreAlignFormatting(std::to_string(category->m_CounterSetUid), 16));
998     categoryBody.append(" | ");
999     categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
1000
1001     std::cout << "\n" << "\n";
1002     std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
1003     std::cout << "\n";
1004     std::cout << std::string(categoryHeader.size(), '=') << "\n";
1005
1006     std::cout << categoryHeader;
1007
1008     std::cout << std::string(categoryBody.size(), '-') << "\n";
1009
1010     std::cout << categoryBody;
1011
1012     std::string counterHeader;
1013
1014     counterHeader.append(CentreAlignFormatting("Counter Name", 20));
1015     counterHeader.append(" | ");
1016     counterHeader.append(CentreAlignFormatting("Description", 50));
1017     counterHeader.append(" | ");
1018     counterHeader.append(CentreAlignFormatting("Units", 14));
1019     counterHeader.append(" | ");
1020     counterHeader.append(CentreAlignFormatting("UID", 6));
1021     counterHeader.append(" | ");
1022     counterHeader.append(CentreAlignFormatting("Max UID", 10));
1023     counterHeader.append(" | ");
1024     counterHeader.append(CentreAlignFormatting("Class", 8));
1025     counterHeader.append(" | ");
1026     counterHeader.append(CentreAlignFormatting("Interpolation", 14));
1027     counterHeader.append(" | ");
1028     counterHeader.append(CentreAlignFormatting("Multiplier", 20));
1029     counterHeader.append(" | ");
1030     counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
1031     counterHeader.append(" | ");
1032     counterHeader.append(CentreAlignFormatting("Device UID", 14));
1033     counterHeader.append("\n");
1034
1035     std::cout << "\n" << "\n";
1036     std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
1037                                        static_cast<int>(counterHeader.size()));
1038     std::cout << "\n";
1039     std::cout << std::string(counterHeader.size(), '=') << "\n";
1040     std::cout << counterHeader;
1041     for (auto& it: category->m_Counters) {
1042         auto search = counterMap.find(it);
1043         if(search != counterMap.end()) {
1044             PrintCounterDetails(search->second);
1045         }
1046     }
1047 }
1048
1049 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1050 {
1051     std::string devicesHeader;
1052
1053     devicesHeader.append(CentreAlignFormatting("Device name", 20));
1054     devicesHeader.append(" | ");
1055     devicesHeader.append(CentreAlignFormatting("UID", 13));
1056     devicesHeader.append(" | ");
1057     devicesHeader.append(CentreAlignFormatting("Cores", 10));
1058     devicesHeader.append("\n");
1059
1060     std::cout << "\n" << "\n";
1061     std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1062     std::cout << "\n";
1063     std::cout << std::string(devicesHeader.size(), '=') << "\n";
1064     std::cout << devicesHeader;
1065     for (auto& it: counterDirectory.GetDevices()) {
1066         PrintDeviceDetails(it);
1067     }
1068
1069     std::string counterSetHeader;
1070
1071     counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1072     counterSetHeader.append(" | ");
1073     counterSetHeader.append(CentreAlignFormatting("UID", 13));
1074     counterSetHeader.append(" | ");
1075     counterSetHeader.append(CentreAlignFormatting("Count", 10));
1076     counterSetHeader.append("\n");
1077
1078     std::cout << "\n" << "\n";
1079     std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1080     std::cout << "\n";
1081     std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1082
1083     std::cout << counterSetHeader;
1084
1085     for (auto& it: counterDirectory.GetCounterSets()) {
1086         PrintCounterSetDetails(it);
1087     }
1088
1089     auto counters = counterDirectory.GetCounters();
1090     for (auto& it: counterDirectory.GetCategories()) {
1091         PrintCategoryDetails(it, counters);
1092     }
1093     std::cout << "\n";
1094 }
1095
1096 uint64_t GetTimestamp()
1097 {
1098 #if USE_CLOCK_MONOTONIC_RAW
1099     using clock = MonotonicClockRaw;
1100 #else
1101     using clock = std::chrono::steady_clock;
1102 #endif
1103
1104     // Take a timestamp
1105     auto timestamp = clock::now();
1106
1107     return static_cast<uint64_t>(timestamp.time_since_epoch().count());
1108 }
1109
1110 } // namespace profiling
1111
1112 } // namespace armnn
1113
1114 namespace std
1115 {
1116
1117 bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
1118 {
1119     return std::memcmp(left.data(), &right, left.size()) == 0;
1120 }
1121
1122 } // namespace std