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/Assert.hpp>
9 #include <common/include/CommonProfilingUtils.hpp>
10 #include <common/include/NumericCast.hpp>
11 #include <common/include/ProfilingException.hpp>
12 #include <common/include/SwTrace.hpp>
13
14 #include <armnn/Version.hpp>
15
16 #include <chrono>
17 #include <fstream>
18 #include <iostream>
19 #include <limits>
20
21 namespace arm
22 {
23
24 namespace pipe
25 {
26
WriteBytes(const IPacketBufferPtr & packetBuffer,unsigned int offset,const void * value,unsigned int valueSize)27 void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
28 {
29 ARM_PIPE_ASSERT(packetBuffer);
30
31 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
32 }
33
ConstructHeader(uint32_t packetFamily,uint32_t packetId)34 uint32_t ConstructHeader(uint32_t packetFamily,
35 uint32_t packetId)
36 {
37 return (( packetFamily & 0x0000003F ) << 26 )|
38 (( packetId & 0x000003FF ) << 16 );
39 }
40
ConstructHeader(uint32_t packetFamily,uint32_t packetClass,uint32_t packetType)41 uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType)
42 {
43 return ((packetFamily & 0x0000003F) << 26) |
44 ((packetClass & 0x0000007F) << 19) |
45 ((packetType & 0x00000007) << 16);
46 }
47
WriteUint64(const std::unique_ptr<IPacketBuffer> & packetBuffer,unsigned int offset,uint64_t value)48 void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
49 {
50 ARM_PIPE_ASSERT(packetBuffer);
51
52 WriteUint64(packetBuffer->GetWritableData(), offset, value);
53 }
54
WriteUint32(const IPacketBufferPtr & packetBuffer,unsigned int offset,uint32_t value)55 void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
56 {
57 ARM_PIPE_ASSERT(packetBuffer);
58
59 WriteUint32(packetBuffer->GetWritableData(), offset, value);
60 }
61
WriteUint16(const IPacketBufferPtr & packetBuffer,unsigned int offset,uint16_t value)62 void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
63 {
64 ARM_PIPE_ASSERT(packetBuffer);
65
66 WriteUint16(packetBuffer->GetWritableData(), offset, value);
67 }
68
WriteUint8(const IPacketBufferPtr & packetBuffer,unsigned int offset,uint8_t value)69 void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
70 {
71 ARM_PIPE_ASSERT(packetBuffer);
72
73 WriteUint8(packetBuffer->GetWritableData(), offset, value);
74 }
75
ReadBytes(const IPacketBufferPtr & packetBuffer,unsigned int offset,unsigned int valueSize,uint8_t outValue[])76 void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
77 {
78 ARM_PIPE_ASSERT(packetBuffer);
79
80 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
81 }
82
ReadUint64(const IPacketBufferPtr & packetBuffer,unsigned int offset)83 uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
84 {
85 ARM_PIPE_ASSERT(packetBuffer);
86
87 return ReadUint64(packetBuffer->GetReadableData(), offset);
88 }
89
ReadUint32(const IPacketBufferPtr & packetBuffer,unsigned int offset)90 uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
91 {
92 ARM_PIPE_ASSERT(packetBuffer);
93
94 return ReadUint32(packetBuffer->GetReadableData(), offset);
95 }
96
ReadUint16(const IPacketBufferPtr & packetBuffer,unsigned int offset)97 uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
98 {
99 ARM_PIPE_ASSERT(packetBuffer);
100
101 return ReadUint16(packetBuffer->GetReadableData(), offset);
102 }
103
ReadUint8(const IPacketBufferPtr & packetBuffer,unsigned int offset)104 uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
105 {
106 ARM_PIPE_ASSERT(packetBuffer);
107
108 return ReadUint8(packetBuffer->GetReadableData(), offset);
109 }
110
GetProcessName()111 std::string GetProcessName()
112 {
113 std::ifstream comm("/proc/self/comm");
114 std::string name;
115 getline(comm, name);
116 return name;
117 }
118
119 /// Creates a timeline packet header
120 ///
121 /// \params
122 /// packetFamiliy Timeline Packet Family
123 /// packetClass Timeline Packet Class
124 /// packetType Timeline Packet Type
125 /// streamId Stream identifier
126 /// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
127 /// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
128 ///
129 /// \returns
130 /// Pair of uint32_t containing word0 and word1 of the header
CreateTimelinePacketHeader(uint32_t packetFamily,uint32_t packetClass,uint32_t packetType,uint32_t streamId,uint32_t sequenceNumbered,uint32_t dataLength)131 std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
132 uint32_t packetClass,
133 uint32_t packetType,
134 uint32_t streamId,
135 uint32_t sequenceNumbered,
136 uint32_t dataLength)
137 {
138 // Packet header word 0:
139 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
140 // 19:25 [7] packet_class: packet class
141 // 16:18 [3] packet_type: packet type
142 // 8:15 [8] reserved: all zeros
143 // 0:7 [8] stream_id: stream identifier
144 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
145 ((packetClass & 0x0000007F) << 19) |
146 ((packetType & 0x00000007) << 16) |
147 ((streamId & 0x00000007) << 0);
148
149 // Packet header word 1:
150 // 25:31 [7] reserved: all zeros
151 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
152 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
153 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
154 ((dataLength & 0x00FFFFFF) << 0);
155
156 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
157 }
158
159 /// Creates a packet header for the timeline messages:
160 /// * declareLabel
161 /// * declareEntity
162 /// * declareEventClass
163 /// * declareRelationship
164 /// * declareEvent
165 ///
166 /// \param
167 /// dataLength The length of the message body in bytes
168 ///
169 /// \returns
170 /// Pair of uint32_t containing word0 and word1 of the header
CreateTimelineMessagePacketHeader(unsigned int dataLength)171 std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
172 {
173 return CreateTimelinePacketHeader(1, // Packet family
174 0, // Packet class
175 1, // Packet type
176 0, // Stream id
177 0, // Sequence number
178 dataLength); // Data length
179 }
180
WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,const std::string & label,unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)181 TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
182 const std::string& label,
183 unsigned char* buffer,
184 unsigned int remainingBufferSize,
185 unsigned int& numberOfBytesWritten)
186 {
187 // Initialize the output value
188 numberOfBytesWritten = 0;
189
190 // Check that the given buffer is valid
191 if (buffer == nullptr || remainingBufferSize == 0)
192 {
193 return TimelinePacketStatus::BufferExhaustion;
194 }
195
196 // Utils
197 unsigned int uint32_t_size = sizeof(uint32_t);
198 unsigned int uint64_t_size = sizeof(uint64_t);
199
200 // Convert the label into a SWTrace string
201 std::vector<uint32_t> swTraceLabel;
202 bool result = arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(label, swTraceLabel);
203 if (!result)
204 {
205 return TimelinePacketStatus::Error;
206 }
207
208 // Calculate the size of the SWTrace string label (in bytes)
209 unsigned int swTraceLabelSize = arm::pipe::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
210
211 // Calculate the length of the data (in bytes)
212 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
213 uint64_t_size + // Profiling GUID
214 swTraceLabelSize; // Label
215
216 // Check whether the timeline binary packet fits in the given buffer
217 if (timelineLabelPacketDataLength > remainingBufferSize)
218 {
219 return TimelinePacketStatus::BufferExhaustion;
220 }
221
222 // Initialize the offset for writing in the buffer
223 unsigned int offset = 0;
224
225 // Write decl_Id to the buffer
226 WriteUint32(buffer, offset, 0u);
227 offset += uint32_t_size;
228
229 // Write the timeline binary packet payload to the buffer
230 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
231 offset += uint64_t_size;
232 for (uint32_t swTraceLabelWord : swTraceLabel)
233 {
234 WriteUint32(buffer, offset, swTraceLabelWord); // Label
235 offset += uint32_t_size;
236 }
237
238 // Update the number of bytes written
239 numberOfBytesWritten = timelineLabelPacketDataLength;
240
241 return TimelinePacketStatus::Ok;
242 }
243
WriteTimelineEntityBinary(uint64_t profilingGuid,unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)244 TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
245 unsigned char* buffer,
246 unsigned int remainingBufferSize,
247 unsigned int& numberOfBytesWritten)
248 {
249 // Initialize the output value
250 numberOfBytesWritten = 0;
251
252 // Check that the given buffer is valid
253 if (buffer == nullptr || remainingBufferSize == 0)
254 {
255 return TimelinePacketStatus::BufferExhaustion;
256 }
257
258 // Utils
259 unsigned int uint32_t_size = sizeof(uint32_t);
260 unsigned int uint64_t_size = sizeof(uint64_t);
261
262 // Calculate the length of the data (in bytes)
263 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
264
265 // Check whether the timeline binary packet fits in the given buffer
266 if (timelineEntityDataLength > remainingBufferSize)
267 {
268 return TimelinePacketStatus::BufferExhaustion;
269 }
270
271 // Initialize the offset for writing in the buffer
272 unsigned int offset = 0;
273
274 // Write the decl_Id to the buffer
275 WriteUint32(buffer, offset, 1u);
276 offset += uint32_t_size;
277
278 // Write the timeline binary packet payload to the buffer
279 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
280
281 // Update the number of bytes written
282 numberOfBytesWritten = timelineEntityDataLength;
283
284 return TimelinePacketStatus::Ok;
285 }
286
WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,uint64_t relationshipGuid,uint64_t headGuid,uint64_t tailGuid,uint64_t attributeGuid,unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)287 TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
288 uint64_t relationshipGuid,
289 uint64_t headGuid,
290 uint64_t tailGuid,
291 uint64_t attributeGuid,
292 unsigned char* buffer,
293 unsigned int remainingBufferSize,
294 unsigned int& numberOfBytesWritten)
295 {
296 // Initialize the output value
297 numberOfBytesWritten = 0;
298
299 // Check that the given buffer is valid
300 if (buffer == nullptr || remainingBufferSize == 0)
301 {
302 return TimelinePacketStatus::BufferExhaustion;
303 }
304
305 // Utils
306 unsigned int uint32_t_size = sizeof(uint32_t);
307 unsigned int uint64_t_size = sizeof(uint64_t);
308
309 // Calculate the length of the data (in bytes)
310 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
311 uint64_t_size * 4; // Relationship GUID + Head GUID +
312 // tail GUID + attributeGuid
313
314 // Check whether the timeline binary fits in the given buffer
315 if (timelineRelationshipDataLength > remainingBufferSize)
316 {
317 return TimelinePacketStatus::BufferExhaustion;
318 }
319
320 // Initialize the offset for writing in the buffer
321 unsigned int offset = 0;
322
323 uint32_t relationshipTypeUint = 0;
324
325 switch (relationshipType)
326 {
327 case ProfilingRelationshipType::RetentionLink:
328 relationshipTypeUint = 0;
329 break;
330 case ProfilingRelationshipType::ExecutionLink:
331 relationshipTypeUint = 1;
332 break;
333 case ProfilingRelationshipType::DataLink:
334 relationshipTypeUint = 2;
335 break;
336 case ProfilingRelationshipType::LabelLink:
337 relationshipTypeUint = 3;
338 break;
339 default:
340 throw arm::pipe::InvalidArgumentException("Unknown relationship type given.");
341 }
342
343 // Write the timeline binary payload to the buffer
344 // decl_id of the timeline message
345 uint32_t declId = 3;
346 WriteUint32(buffer, offset, declId); // decl_id
347 offset += uint32_t_size;
348 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
349 offset += uint32_t_size;
350 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
351 offset += uint64_t_size;
352 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
353 offset += uint64_t_size;
354 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
355 offset += uint64_t_size;
356 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
357
358
359 // Update the number of bytes written
360 numberOfBytesWritten = timelineRelationshipDataLength;
361
362 return TimelinePacketStatus::Ok;
363 }
364
WriteTimelineMessageDirectoryPackage(unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)365 TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
366 unsigned int remainingBufferSize,
367 unsigned int& numberOfBytesWritten)
368 {
369 // Initialize the output value
370 numberOfBytesWritten = 0;
371
372 // Check that the given buffer is valid
373 if (buffer == nullptr || remainingBufferSize == 0)
374 {
375 return TimelinePacketStatus::BufferExhaustion;
376 }
377
378 // Utils
379 unsigned int uint8_t_size = sizeof(uint8_t);
380 unsigned int uint32_t_size = sizeof(uint32_t);
381 unsigned int uint64_t_size = sizeof(uint64_t);
382
383 // The payload/data of the packet consists of swtrace event definitions encoded according
384 // to the swtrace directory specification. The messages being the five defined below:
385 //
386 // | decl_id | decl_name | ui_name | arg_types | arg_names |
387 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
388 // | 0 | declareLabel | declare label | ps | guid,value |
389 // | 1 | declareEntity | declare entity | p | guid |
390 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
391 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
392 // | | | | | headGuid,tailGuid,attributeGuid |
393 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
394 std::vector<std::vector<std::string>> timelineDirectoryMessages
395 {
396 { "0", "declareLabel", "declare label", "ps", "guid,value" },
397 { "1", "declareEntity", "declare entity", "p", "guid" },
398 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
399 { "3", "declareRelationship", "declare relationship", "Ipppp",
400 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
401 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
402 };
403
404 // Build the message declarations
405 std::vector<uint32_t> swTraceBuffer;
406 for (const auto& directoryComponent : timelineDirectoryMessages)
407 {
408 // decl_id
409 uint32_t declId = 0;
410 try
411 {
412 declId = arm::pipe::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
413 }
414 catch (const std::exception&)
415 {
416 return TimelinePacketStatus::Error;
417 }
418 swTraceBuffer.push_back(declId);
419
420 bool result = true;
421 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceNameCharPolicy>(
422 directoryComponent[1], swTraceBuffer); // decl_name
423 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
424 directoryComponent[2], swTraceBuffer); // ui_name
425 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceTypeCharPolicy>(
426 directoryComponent[3], swTraceBuffer); // arg_types
427 result &= arm::pipe::ConvertDirectoryComponent<arm::pipe::SwTraceCharPolicy> (
428 directoryComponent[4], swTraceBuffer); // arg_names
429 if (!result)
430 {
431 return TimelinePacketStatus::Error;
432 }
433 }
434
435 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
436 arm::pipe::numeric_cast<unsigned int>(swTraceBuffer.size()) *
437 uint32_t_size; // Trace directory (5 messages)
438
439 // Calculate the timeline directory binary packet size (in bytes)
440 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
441 dataLength; // Payload
442
443 // Check whether the timeline directory binary packet fits in the given buffer
444 if (timelineDirectoryPacketSize > remainingBufferSize)
445 {
446 return TimelinePacketStatus::BufferExhaustion;
447 }
448
449 // Create packet header
450 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, arm::pipe::numeric_cast<uint32_t>(dataLength));
451
452 // Initialize the offset for writing in the buffer
453 unsigned int offset = 0;
454
455 // Write the timeline binary packet header to the buffer
456 WriteUint32(buffer, offset, packetHeader.first);
457 offset += uint32_t_size;
458 WriteUint32(buffer, offset, packetHeader.second);
459 offset += uint32_t_size;
460
461 // Write the stream header
462 uint8_t streamVersion = 4;
463 uint8_t pointerBytes = arm::pipe::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
464 uint8_t threadIdBytes = arm::pipe::numeric_cast<uint8_t>(ThreadIdSize);
465 switch (threadIdBytes)
466 {
467 case 4: // Typically Windows and Android
468 case 8: // Typically Linux
469 break; // Valid values
470 default:
471 return TimelinePacketStatus::Error; // Invalid value
472 }
473 WriteUint8(buffer, offset, streamVersion);
474 offset += uint8_t_size;
475 WriteUint8(buffer, offset, pointerBytes);
476 offset += uint8_t_size;
477 WriteUint8(buffer, offset, threadIdBytes);
478 offset += uint8_t_size;
479
480 // Write the SWTrace directory
481 uint32_t numberOfDeclarations = arm::pipe::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
482 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
483 offset += uint32_t_size;
484 for (uint32_t i : swTraceBuffer)
485 {
486 WriteUint32(buffer, offset, i); // Message declarations
487 offset += uint32_t_size;
488 }
489
490 // Update the number of bytes written
491 numberOfBytesWritten = timelineDirectoryPacketSize;
492
493 return TimelinePacketStatus::Ok;
494 }
495
WriteTimelineEventClassBinary(uint64_t profilingGuid,uint64_t nameGuid,unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)496 TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
497 uint64_t nameGuid,
498 unsigned char* buffer,
499 unsigned int remainingBufferSize,
500 unsigned int& numberOfBytesWritten)
501 {
502 // Initialize the output value
503 numberOfBytesWritten = 0;
504
505 // Check that the given buffer is valid
506 if (buffer == nullptr || remainingBufferSize == 0)
507 {
508 return TimelinePacketStatus::BufferExhaustion;
509 }
510
511 // Utils
512 unsigned int uint32_t_size = sizeof(uint32_t);
513 unsigned int uint64_t_size = sizeof(uint64_t);
514
515 // decl_id of the timeline message
516 uint32_t declId = 2;
517
518 // Calculate the length of the data (in bytes)
519 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
520
521 // Check whether the timeline binary fits in the given buffer
522 if (dataSize > remainingBufferSize)
523 {
524 return TimelinePacketStatus::BufferExhaustion;
525 }
526
527 // Initialize the offset for writing in the buffer
528 unsigned int offset = 0;
529
530 // Write the timeline binary payload to the buffer
531 WriteUint32(buffer, offset, declId); // decl_id
532 offset += uint32_t_size;
533 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
534 offset += uint64_t_size;
535 WriteUint64(buffer, offset, nameGuid); // Name GUID
536
537 // Update the number of bytes written
538 numberOfBytesWritten = dataSize;
539
540 return TimelinePacketStatus::Ok;
541 }
542
WriteTimelineEventBinary(uint64_t timestamp,int threadId,uint64_t profilingGuid,unsigned char * buffer,unsigned int remainingBufferSize,unsigned int & numberOfBytesWritten)543 TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
544 int threadId,
545 uint64_t profilingGuid,
546 unsigned char* buffer,
547 unsigned int remainingBufferSize,
548 unsigned int& numberOfBytesWritten)
549 {
550 // Initialize the output value
551 numberOfBytesWritten = 0;
552 // Check that the given buffer is valid
553 if (buffer == nullptr || remainingBufferSize == 0)
554 {
555 return TimelinePacketStatus::BufferExhaustion;
556 }
557
558 // Utils
559 unsigned int uint32_t_size = sizeof(uint32_t);
560 unsigned int uint64_t_size = sizeof(uint64_t);
561
562 // decl_id of the timeline message
563 uint32_t declId = 4;
564
565 // Calculate the length of the data (in bytes)
566 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
567 uint64_t_size + // Timestamp
568 ThreadIdSize + // Thread id
569 uint64_t_size; // Profiling GUID
570
571 // Check whether the timeline binary packet fits in the given buffer
572 if (timelineEventDataLength > remainingBufferSize)
573 {
574 return TimelinePacketStatus::BufferExhaustion;
575 }
576
577 // Initialize the offset for writing in the buffer
578 unsigned int offset = 0;
579
580 // Write the timeline binary payload to the buffer
581 WriteUint32(buffer, offset, declId); // decl_id
582 offset += uint32_t_size;
583 WriteUint64(buffer, offset, timestamp); // Timestamp
584 offset += uint64_t_size;
585 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
586 offset += ThreadIdSize;
587 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
588 offset += uint64_t_size;
589 // Update the number of bytes written
590 numberOfBytesWritten = timelineEventDataLength;
591
592 return TimelinePacketStatus::Ok;
593 }
594
GetTimestamp()595 uint64_t GetTimestamp()
596 {
597 using clock = std::chrono::steady_clock;
598
599 // Take a timestamp
600 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
601
602 return static_cast<uint64_t>(timestamp.count());
603 }
604
ReceivePacket(const unsigned char * buffer,uint32_t length)605 arm::pipe::Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
606 {
607 if (buffer == nullptr)
608 {
609 throw arm::pipe::ProfilingException("data buffer is nullptr");
610 }
611 if (length < 8)
612 {
613 throw arm::pipe::ProfilingException("length of data buffer is less than 8");
614 }
615
616 uint32_t metadataIdentifier = 0;
617 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
618
619 uint32_t dataLength = 0;
620 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
621
622 std::unique_ptr<unsigned char[]> packetData;
623 if (dataLength > 0)
624 {
625 packetData = std::make_unique<unsigned char[]>(dataLength);
626 std::memcpy(packetData.get(), buffer + 8u, dataLength);
627 }
628
629 return arm::pipe::Packet(metadataIdentifier, dataLength, packetData);
630 }
631
632 } // namespace pipe
633
634 } // namespace arm
635
636 namespace std
637 {
638
operator ==(const std::vector<uint8_t> & left,int right)639 bool operator==(const std::vector<uint8_t>& left, int right)
640 {
641 return std::memcmp(left.data(), &right, left.size()) == 0;
642 }
643
644 } // namespace std
645