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