IVGCVSW-5076 Correct Profiling Stream Metadata packet revision table
[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, unsigned int& offset)
320 {
321     ARMNN_ASSERT(packetBuffer);
322
323     unsigned int uint32_t_size = sizeof(uint32_t);
324
325     SwTraceMessage swTraceMessage;
326
327     // Read the decl_id
328     uint32_t readDeclId = ReadUint32(packetBuffer, offset);
329     swTraceMessage.m_Id = readDeclId;
330
331     // SWTrace "namestring" format
332     // length of the string (first 4 bytes) + string + null terminator
333
334     // Check the decl_name
335     offset += uint32_t_size;
336     uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
337
338     offset += uint32_t_size;
339     std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
340     std::memcpy(swTraceStringBuffer.data(),
341                 packetBuffer + offset, swTraceStringBuffer.size());
342
343     swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
344
345     // Check the ui_name
346     offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
347     uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
348
349     offset += uint32_t_size;
350     swTraceStringBuffer.resize(swTraceUINameLength - 1);
351     std::memcpy(swTraceStringBuffer.data(),
352                 packetBuffer  + offset, swTraceStringBuffer.size());
353
354     swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
355
356     // Check arg_types
357     offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
358     uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
359
360     offset += uint32_t_size;
361     swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
362     std::memcpy(swTraceStringBuffer.data(),
363                 packetBuffer  + offset, swTraceStringBuffer.size());
364
365     swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
366
367     std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
368
369     // Check arg_names
370     offset += CalculateSizeOfPaddedSwString(swTraceString);
371     uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
372
373     offset += uint32_t_size;
374     swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
375     std::memcpy(swTraceStringBuffer.data(),
376                 packetBuffer  + offset, swTraceStringBuffer.size());
377
378     swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
379     std::stringstream stringStream(swTraceString);
380     std::string argName;
381     while (std::getline(stringStream, argName, ','))
382     {
383         swTraceMessage.m_ArgNames.push_back(argName);
384     }
385
386     offset += CalculateSizeOfPaddedSwString(swTraceString);
387
388     return swTraceMessage;
389 }
390
391 /// Creates a timeline packet header
392 ///
393 /// \params
394 ///   packetFamiliy     Timeline Packet Family
395 ///   packetClass       Timeline Packet Class
396 ///   packetType        Timeline Packet Type
397 ///   streamId          Stream identifier
398 ///   seqeunceNumbered  When non-zero the 4 bytes following the header is a u32 sequence number
399 ///   dataLength        Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
400 ///
401 /// \returns
402 ///   Pair of uint32_t containing word0 and word1 of the header
403 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
404                                                          uint32_t packetClass,
405                                                          uint32_t packetType,
406                                                          uint32_t streamId,
407                                                          uint32_t sequenceNumbered,
408                                                          uint32_t dataLength)
409 {
410     // Packet header word 0:
411     // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
412     // 19:25 [7] packet_class: packet class
413     // 16:18 [3] packet_type: packet type
414     // 8:15  [8] reserved: all zeros
415     // 0:7   [8] stream_id: stream identifier
416     uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
417                                  ((packetClass  & 0x0000007F) << 19) |
418                                  ((packetType   & 0x00000007) << 16) |
419                                  ((streamId     & 0x00000007) <<  0);
420
421     // Packet header word 1:
422     // 25:31 [7]  reserved: all zeros
423     // 24    [1]  sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
424     // 0:23  [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
425     uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
426                                  ((dataLength       & 0x00FFFFFF) <<  0);
427
428     return std::make_pair(packetHeaderWord0, packetHeaderWord1);
429 }
430
431 /// Creates a packet header for the timeline messages:
432 /// * declareLabel
433 /// * declareEntity
434 /// * declareEventClass
435 /// * declareRelationship
436 /// * declareEvent
437 ///
438 /// \param
439 ///   dataLength The length of the message body in bytes
440 ///
441 /// \returns
442 ///   Pair of uint32_t containing word0 and word1 of the header
443 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
444 {
445     return CreateTimelinePacketHeader(1,           // Packet family
446                                       0,           // Packet class
447                                       1,           // Packet type
448                                       0,           // Stream id
449                                       0,           // Sequence number
450                                       dataLength); // Data length
451 }
452
453 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
454                                                     const std::string& label,
455                                                     unsigned char* buffer,
456                                                     unsigned int remainingBufferSize,
457                                                     unsigned int& numberOfBytesWritten)
458 {
459     // Initialize the output value
460     numberOfBytesWritten = 0;
461
462     // Check that the given buffer is valid
463     if (buffer == nullptr || remainingBufferSize == 0)
464     {
465         return TimelinePacketStatus::BufferExhaustion;
466     }
467
468     // Utils
469     unsigned int uint32_t_size = sizeof(uint32_t);
470     unsigned int uint64_t_size = sizeof(uint64_t);
471
472     // Convert the label into a SWTrace string
473     std::vector<uint32_t> swTraceLabel;
474     bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
475     if (!result)
476     {
477         return TimelinePacketStatus::Error;
478     }
479
480     // Calculate the size of the SWTrace string label (in bytes)
481     unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
482
483     // Calculate the length of the data (in bytes)
484     unsigned int timelineLabelPacketDataLength = uint32_t_size +   // decl_Id
485                                                  uint64_t_size +   // Profiling GUID
486                                                  swTraceLabelSize; // Label
487
488     // Check whether the timeline binary packet fits in the given buffer
489     if (timelineLabelPacketDataLength > remainingBufferSize)
490     {
491         return TimelinePacketStatus::BufferExhaustion;
492     }
493
494     // Initialize the offset for writing in the buffer
495     unsigned int offset = 0;
496
497     // Write decl_Id to the buffer
498     WriteUint32(buffer, offset, 0u);
499     offset += uint32_t_size;
500
501     // Write the timeline binary packet payload to the buffer
502     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
503     offset += uint64_t_size;
504     for (uint32_t swTraceLabelWord : swTraceLabel)
505     {
506         WriteUint32(buffer, offset, swTraceLabelWord); // Label
507         offset += uint32_t_size;
508     }
509
510     // Update the number of bytes written
511     numberOfBytesWritten = timelineLabelPacketDataLength;
512
513     return TimelinePacketStatus::Ok;
514 }
515
516 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
517                                                unsigned char* buffer,
518                                                unsigned int remainingBufferSize,
519                                                unsigned int& numberOfBytesWritten)
520 {
521     // Initialize the output value
522     numberOfBytesWritten = 0;
523
524     // Check that the given buffer is valid
525     if (buffer == nullptr || remainingBufferSize == 0)
526     {
527         return TimelinePacketStatus::BufferExhaustion;
528     }
529
530     // Utils
531     unsigned int uint32_t_size = sizeof(uint32_t);
532     unsigned int uint64_t_size = sizeof(uint64_t);
533
534     // Calculate the length of the data (in bytes)
535     unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size;  // decl_id + Profiling GUID
536
537     // Check whether the timeline binary packet fits in the given buffer
538     if (timelineEntityDataLength > remainingBufferSize)
539     {
540         return TimelinePacketStatus::BufferExhaustion;
541     }
542
543     // Initialize the offset for writing in the buffer
544     unsigned int offset = 0;
545
546     // Write the decl_Id to the buffer
547     WriteUint32(buffer, offset, 1u);
548     offset += uint32_t_size;
549
550     // Write the timeline binary packet payload to the buffer
551     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
552
553     // Update the number of bytes written
554     numberOfBytesWritten = timelineEntityDataLength;
555
556     return TimelinePacketStatus::Ok;
557 }
558
559 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
560                                                      uint64_t relationshipGuid,
561                                                      uint64_t headGuid,
562                                                      uint64_t tailGuid,
563                                                      uint64_t attributeGuid,
564                                                      unsigned char* buffer,
565                                                      unsigned int remainingBufferSize,
566                                                      unsigned int& numberOfBytesWritten)
567 {
568     // Initialize the output value
569     numberOfBytesWritten = 0;
570
571     // Check that the given buffer is valid
572     if (buffer == nullptr || remainingBufferSize == 0)
573     {
574         return TimelinePacketStatus::BufferExhaustion;
575     }
576
577     // Utils
578     unsigned int uint32_t_size = sizeof(uint32_t);
579     unsigned int uint64_t_size = sizeof(uint64_t);
580
581     // Calculate the length of the data (in bytes)
582     unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
583                                                   uint64_t_size * 4;  // Relationship GUID + Head GUID +
584                                                                       // tail GUID + attributeGuid
585
586     // Check whether the timeline binary fits in the given buffer
587     if (timelineRelationshipDataLength > remainingBufferSize)
588     {
589         return TimelinePacketStatus::BufferExhaustion;
590     }
591
592     // Initialize the offset for writing in the buffer
593     unsigned int offset = 0;
594
595     uint32_t relationshipTypeUint = 0;
596
597     switch (relationshipType)
598     {
599         case ProfilingRelationshipType::RetentionLink:
600             relationshipTypeUint = 0;
601             break;
602         case ProfilingRelationshipType::ExecutionLink:
603             relationshipTypeUint = 1;
604             break;
605         case ProfilingRelationshipType::DataLink:
606             relationshipTypeUint = 2;
607             break;
608         case ProfilingRelationshipType::LabelLink:
609             relationshipTypeUint = 3;
610             break;
611         default:
612             throw InvalidArgumentException("Unknown relationship type given.");
613     }
614
615     // Write the timeline binary payload to the buffer
616     // decl_id of the timeline message
617     uint32_t declId = 3;
618     WriteUint32(buffer, offset, declId); // decl_id
619     offset += uint32_t_size;
620     WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
621     offset += uint32_t_size;
622     WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
623     offset += uint64_t_size;
624     WriteUint64(buffer, offset, headGuid); // head of relationship GUID
625     offset += uint64_t_size;
626     WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
627     offset += uint64_t_size;
628     WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
629
630
631     // Update the number of bytes written
632     numberOfBytesWritten = timelineRelationshipDataLength;
633
634     return TimelinePacketStatus::Ok;
635 }
636
637 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
638                                                           unsigned int remainingBufferSize,
639                                                           unsigned int& numberOfBytesWritten)
640 {
641     // Initialize the output value
642     numberOfBytesWritten = 0;
643
644     // Check that the given buffer is valid
645     if (buffer == nullptr || remainingBufferSize == 0)
646     {
647         return TimelinePacketStatus::BufferExhaustion;
648     }
649
650     // Utils
651     unsigned int uint8_t_size  = sizeof(uint8_t);
652     unsigned int uint32_t_size = sizeof(uint32_t);
653     unsigned int uint64_t_size = sizeof(uint64_t);
654
655     // The payload/data of the packet consists of swtrace event definitions encoded according
656     // to the swtrace directory specification. The messages being the five defined below:
657     //
658     // |  decl_id  |     decl_name       |      ui_name          |  arg_types  |            arg_names                |
659     // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
660     // |    0      |   declareLabel      |   declare label       |    ps       |  guid,value                         |
661     // |    1      |   declareEntity     |   declare entity      |    p        |  guid                               |
662     // |    2      | declareEventClass   |  declare event class  |    pp       |  guid,nameGuid                      |
663     // |    3      | declareRelationship | declare relationship  |    Ipppp    |  relationshipType,relationshipGuid, |
664     // |           |                     |                       |             |  headGuid,tailGuid,attributeGuid    |
665     // |    4      |   declareEvent      |   declare event       |    @tp      |  timestamp,threadId,eventGuid       |
666     std::vector<std::vector<std::string>> timelineDirectoryMessages
667     {
668         { "0", "declareLabel", "declare label", "ps", "guid,value" },
669         { "1", "declareEntity", "declare entity", "p", "guid" },
670         { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
671         { "3", "declareRelationship", "declare relationship", "Ipppp",
672           "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
673         { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
674     };
675
676     // Build the message declarations
677     std::vector<uint32_t> swTraceBuffer;
678     for (const auto& directoryComponent : timelineDirectoryMessages)
679     {
680         // decl_id
681         uint32_t declId = 0;
682         try
683         {
684             declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
685         }
686         catch (const std::exception&)
687         {
688             return TimelinePacketStatus::Error;
689         }
690         swTraceBuffer.push_back(declId);
691
692         bool result = true;
693         result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
694         result &= ConvertDirectoryComponent<SwTraceCharPolicy>    (directoryComponent[2], swTraceBuffer); // ui_name
695         result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
696         result &= ConvertDirectoryComponent<SwTraceCharPolicy>    (directoryComponent[4], swTraceBuffer); // arg_names
697         if (!result)
698         {
699             return TimelinePacketStatus::Error;
700         }
701     }
702
703     unsigned int dataLength = 3 * uint8_t_size +  // Stream header (3 bytes)
704                               boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
705                                   uint32_t_size; // Trace directory (5 messages)
706
707     // Calculate the timeline directory binary packet size (in bytes)
708     unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
709                                                dataLength;         // Payload
710
711     // Check whether the timeline directory binary packet fits in the given buffer
712     if (timelineDirectoryPacketSize > remainingBufferSize)
713     {
714         return TimelinePacketStatus::BufferExhaustion;
715     }
716
717     // Create packet header
718     auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
719
720     // Initialize the offset for writing in the buffer
721     unsigned int offset = 0;
722
723     // Write the timeline binary packet header to the buffer
724     WriteUint32(buffer, offset, packetHeader.first);
725     offset += uint32_t_size;
726     WriteUint32(buffer, offset, packetHeader.second);
727     offset += uint32_t_size;
728
729     // Write the stream header
730     uint8_t streamVersion = 4;
731     uint8_t pointerBytes  = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
732     uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
733     switch (threadIdBytes)
734     {
735     case 4: // Typically Windows and Android
736     case 8: // Typically Linux
737         break; // Valid values
738     default:
739         return TimelinePacketStatus::Error; // Invalid value
740     }
741     WriteUint8(buffer, offset, streamVersion);
742     offset += uint8_t_size;
743     WriteUint8(buffer, offset, pointerBytes);
744     offset += uint8_t_size;
745     WriteUint8(buffer, offset, threadIdBytes);
746     offset += uint8_t_size;
747
748     // Write the SWTrace directory
749     uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
750     WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
751     offset += uint32_t_size;
752     for (uint32_t i : swTraceBuffer)
753     {
754         WriteUint32(buffer, offset, i); // Message declarations
755         offset += uint32_t_size;
756     }
757
758     // Update the number of bytes written
759     numberOfBytesWritten = timelineDirectoryPacketSize;
760
761     return TimelinePacketStatus::Ok;
762 }
763
764 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
765                                                    uint64_t nameGuid,
766                                                    unsigned char* buffer,
767                                                    unsigned int remainingBufferSize,
768                                                    unsigned int& numberOfBytesWritten)
769 {
770     // Initialize the output value
771     numberOfBytesWritten = 0;
772
773     // Check that the given buffer is valid
774     if (buffer == nullptr || remainingBufferSize == 0)
775     {
776         return TimelinePacketStatus::BufferExhaustion;
777     }
778
779     // Utils
780     unsigned int uint32_t_size = sizeof(uint32_t);
781     unsigned int uint64_t_size = sizeof(uint64_t);
782
783     // decl_id of the timeline message
784     uint32_t declId = 2;
785
786     // Calculate the length of the data (in bytes)
787     unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
788
789     // Check whether the timeline binary fits in the given buffer
790     if (dataSize > remainingBufferSize)
791     {
792         return TimelinePacketStatus::BufferExhaustion;
793     }
794
795     // Initialize the offset for writing in the buffer
796     unsigned int offset = 0;
797
798     // Write the timeline binary payload to the buffer
799     WriteUint32(buffer, offset, declId);        // decl_id
800     offset += uint32_t_size;
801     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
802     offset += uint64_t_size;
803     WriteUint64(buffer, offset, nameGuid); // Name GUID
804
805     // Update the number of bytes written
806     numberOfBytesWritten = dataSize;
807
808     return TimelinePacketStatus::Ok;
809 }
810
811 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
812                                               int threadId,
813                                               uint64_t profilingGuid,
814                                               unsigned char* buffer,
815                                               unsigned int remainingBufferSize,
816                                               unsigned int& numberOfBytesWritten)
817 {
818     // Initialize the output value
819     numberOfBytesWritten = 0;
820     // Check that the given buffer is valid
821     if (buffer == nullptr || remainingBufferSize == 0)
822     {
823         return TimelinePacketStatus::BufferExhaustion;
824     }
825
826     // Utils
827     unsigned int uint32_t_size = sizeof(uint32_t);
828     unsigned int uint64_t_size = sizeof(uint64_t);
829
830     // decl_id of the timeline message
831     uint32_t declId = 4;
832
833     // Calculate the length of the data (in bytes)
834     unsigned int timelineEventDataLength = uint32_t_size + // decl_id
835                                            uint64_t_size + // Timestamp
836                                            ThreadIdSize +  // Thread id
837                                            uint64_t_size;  // Profiling GUID
838
839     // Check whether the timeline binary packet fits in the given buffer
840     if (timelineEventDataLength > remainingBufferSize)
841     {
842         return TimelinePacketStatus::BufferExhaustion;
843     }
844
845     // Initialize the offset for writing in the buffer
846     unsigned int offset = 0;
847
848     // Write the timeline binary payload to the buffer
849     WriteUint32(buffer, offset, declId); // decl_id
850     offset += uint32_t_size;
851     WriteUint64(buffer, offset, timestamp); // Timestamp
852     offset += uint64_t_size;
853     WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
854     offset += ThreadIdSize;
855     WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
856     offset += uint64_t_size;
857     // Update the number of bytes written
858     numberOfBytesWritten = timelineEventDataLength;
859
860     return TimelinePacketStatus::Ok;
861 }
862
863 std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
864 {
865     std::stringstream outputStream, centrePadding;
866     int padding = spacingWidth - static_cast<int>(stringToPass.size());
867
868     for (int i = 0; i < padding / 2; ++i)
869     {
870         centrePadding << " ";
871     }
872
873     outputStream << centrePadding.str() << stringToPass << centrePadding.str();
874
875     if (padding > 0 && padding %2 != 0)
876     {
877         outputStream << " ";
878     }
879
880     return outputStream.str();
881 }
882
883 void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
884 {
885     std::string body;
886
887     body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
888     body.append(" | ");
889     body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
890     body.append(" | ");
891     body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
892     body.append("\n");
893
894     std::cout << std::string(body.size(), '-') << "\n";
895     std::cout<< body;
896 }
897
898 void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
899 {
900     std::string body;
901
902     body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
903     body.append(" | ");
904     body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
905     body.append(" | ");
906     body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
907     body.append("\n");
908
909     std::cout << std::string(body.size(), '-') << "\n";
910
911     std::cout<< body;
912 }
913
914 void PrintCounterDetails(std::shared_ptr<Counter>& counter)
915 {
916     std::string body;
917
918     body.append(CentreAlignFormatting(counter->m_Name, 20));
919     body.append(" | ");
920     body.append(CentreAlignFormatting(counter->m_Description, 50));
921     body.append(" | ");
922     body.append(CentreAlignFormatting(counter->m_Units, 14));
923     body.append(" | ");
924     body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
925     body.append(" | ");
926     body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
927     body.append(" | ");
928     body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
929     body.append(" | ");
930     body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
931     body.append(" | ");
932     body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
933     body.append(" | ");
934     body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
935     body.append(" | ");
936     body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
937
938     body.append("\n");
939
940     std::cout << std::string(body.size(), '-') << "\n";
941
942     std::cout << body;
943 }
944
945 void PrintCategoryDetails(const std::unique_ptr<Category>& category,
946                           std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
947 {
948     std::string categoryBody;
949     std::string categoryHeader;
950
951     categoryHeader.append(CentreAlignFormatting("Name", 20));
952     categoryHeader.append(" | ");
953     categoryHeader.append(CentreAlignFormatting("Event Count", 14));
954     categoryHeader.append("\n");
955
956     categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
957     categoryBody.append(" | ");
958     categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
959
960     std::cout << "\n" << "\n";
961     std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
962     std::cout << "\n";
963     std::cout << std::string(categoryHeader.size(), '=') << "\n";
964
965     std::cout << categoryHeader;
966
967     std::cout << std::string(categoryBody.size(), '-') << "\n";
968
969     std::cout << categoryBody;
970
971     std::string counterHeader;
972
973     counterHeader.append(CentreAlignFormatting("Counter Name", 20));
974     counterHeader.append(" | ");
975     counterHeader.append(CentreAlignFormatting("Description", 50));
976     counterHeader.append(" | ");
977     counterHeader.append(CentreAlignFormatting("Units", 14));
978     counterHeader.append(" | ");
979     counterHeader.append(CentreAlignFormatting("UID", 6));
980     counterHeader.append(" | ");
981     counterHeader.append(CentreAlignFormatting("Max UID", 10));
982     counterHeader.append(" | ");
983     counterHeader.append(CentreAlignFormatting("Class", 8));
984     counterHeader.append(" | ");
985     counterHeader.append(CentreAlignFormatting("Interpolation", 14));
986     counterHeader.append(" | ");
987     counterHeader.append(CentreAlignFormatting("Multiplier", 20));
988     counterHeader.append(" | ");
989     counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
990     counterHeader.append(" | ");
991     counterHeader.append(CentreAlignFormatting("Device UID", 14));
992     counterHeader.append("\n");
993
994     std::cout << "\n" << "\n";
995     std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
996                                        static_cast<int>(counterHeader.size()));
997     std::cout << "\n";
998     std::cout << std::string(counterHeader.size(), '=') << "\n";
999     std::cout << counterHeader;
1000     for (auto& it: category->m_Counters) {
1001         auto search = counterMap.find(it);
1002         if(search != counterMap.end()) {
1003             PrintCounterDetails(search->second);
1004         }
1005     }
1006 }
1007
1008 void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1009 {
1010     std::string devicesHeader;
1011
1012     devicesHeader.append(CentreAlignFormatting("Device name", 20));
1013     devicesHeader.append(" | ");
1014     devicesHeader.append(CentreAlignFormatting("UID", 13));
1015     devicesHeader.append(" | ");
1016     devicesHeader.append(CentreAlignFormatting("Cores", 10));
1017     devicesHeader.append("\n");
1018
1019     std::cout << "\n" << "\n";
1020     std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1021     std::cout << "\n";
1022     std::cout << std::string(devicesHeader.size(), '=') << "\n";
1023     std::cout << devicesHeader;
1024     for (auto& it: counterDirectory.GetDevices()) {
1025         PrintDeviceDetails(it);
1026     }
1027
1028     std::string counterSetHeader;
1029
1030     counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1031     counterSetHeader.append(" | ");
1032     counterSetHeader.append(CentreAlignFormatting("UID", 13));
1033     counterSetHeader.append(" | ");
1034     counterSetHeader.append(CentreAlignFormatting("Count", 10));
1035     counterSetHeader.append("\n");
1036
1037     std::cout << "\n" << "\n";
1038     std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1039     std::cout << "\n";
1040     std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1041
1042     std::cout << counterSetHeader;
1043
1044     for (auto& it: counterDirectory.GetCounterSets()) {
1045         PrintCounterSetDetails(it);
1046     }
1047
1048     auto counters = counterDirectory.GetCounters();
1049     for (auto& it: counterDirectory.GetCategories()) {
1050         PrintCategoryDetails(it, counters);
1051     }
1052     std::cout << "\n";
1053 }
1054
1055 uint64_t GetTimestamp()
1056 {
1057 #if USE_CLOCK_MONOTONIC_RAW
1058     using clock = MonotonicClockRaw;
1059 #else
1060     using clock = std::chrono::steady_clock;
1061 #endif
1062
1063     // Take a timestamp
1064     auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
1065
1066     return static_cast<uint64_t>(timestamp.count());
1067 }
1068
1069 Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1070 {
1071     if (buffer == nullptr)
1072     {
1073         throw armnnProfiling::ProfilingException("data buffer is nullptr");
1074     }
1075     if (length < 8)
1076     {
1077         throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1078     }
1079
1080     uint32_t metadataIdentifier = 0;
1081     std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1082
1083     uint32_t dataLength = 0;
1084     std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1085
1086     std::unique_ptr<unsigned char[]> packetData;
1087     if (dataLength > 0)
1088     {
1089         packetData = std::make_unique<unsigned char[]>(dataLength);
1090         std::memcpy(packetData.get(), buffer + 8u, dataLength);
1091     }
1092
1093     return Packet(metadataIdentifier, dataLength, packetData);
1094 }
1095
1096 } // namespace profiling
1097
1098 } // namespace armnn
1099
1100 namespace std
1101 {
1102
1103 bool operator==(const std::vector<uint8_t>& left, int right)
1104 {
1105     return std::memcmp(left.data(), &right, left.size()) == 0;
1106 }
1107
1108 } // namespace std