blob: f4d4f8fa44f31522c5282f2e8b83b160ae6861b6 [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ProfilingUtils.hpp"
7
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01008#include <armnn/Version.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +01009#include <armnn/Conversion.hpp>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010010
Ferran Balaguer73882172019-09-02 16:39:42 +010011#include <boost/assert.hpp>
12
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010013#include <fstream>
Matteo Martincighab173e92019-09-05 12:02:04 +010014#include <limits>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010015
Ferran Balaguer73882172019-09-02 16:39:42 +010016namespace armnn
17{
18
19namespace profiling
20{
21
Matteo Martincigh6db5f202019-09-05 12:02:04 +010022namespace
Matteo Martincighab173e92019-09-05 12:02:04 +010023{
Matteo Martincighab173e92019-09-05 12:02:04 +010024
Matteo Martincigh6db5f202019-09-05 12:02:04 +010025void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
26{
Matteo Martincighab173e92019-09-05 12:02:04 +010027 // Check that it is possible to generate the next UID without causing an overflow
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028 switch (cores)
Matteo Martincighab173e92019-09-05 12:02:04 +010029 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010030 case 0:
31 case 1:
32 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
33 // running multiple parallel workloads and will not provide multiple streams of data for each event)
34 if (uid == std::numeric_limits<uint16_t>::max())
35 {
36 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
37 }
38 break;
39 default: // cores > 1
40 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
41 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
42 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
43 {
44 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
45 }
46 break;
Matteo Martincighab173e92019-09-05 12:02:04 +010047 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010048}
Matteo Martincighab173e92019-09-05 12:02:04 +010049
Matteo Martincigh6db5f202019-09-05 12:02:04 +010050} // Anonymous namespace
51
52uint16_t GetNextUid(bool peekOnly)
53{
54 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
55 static uint16_t uid = 1;
56
57 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
58 ThrowIfCantGenerateNextUid(uid);
59
60 if (peekOnly)
61 {
62 // Peek only
63 return uid;
64 }
65 else
66 {
67 // Get the next UID
68 return uid++;
69 }
70}
71
72std::vector<uint16_t> GetNextCounterUids(uint16_t cores)
73{
74 // The UID used for counters only. The first valid UID is 0
75 static uint16_t counterUid = 0;
76
77 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
78 ThrowIfCantGenerateNextUid(counterUid, cores);
79
80 // Get the next counter UIDs
81 size_t counterUidsSize = cores == 0 ? 1 : cores;
82 std::vector<uint16_t> counterUids(counterUidsSize, 0);
83 for (size_t i = 0; i < counterUidsSize; i++)
84 {
85 counterUids[i] = counterUid++;
86 }
87 return counterUids;
Matteo Martincighab173e92019-09-05 12:02:04 +010088}
89
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010090void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
91{
92 BOOST_ASSERT(packetBuffer);
93
94 WriteUint64(packetBuffer->GetWritableData(), offset, value);
95}
96
97void WriteUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint32_t value)
98{
99 BOOST_ASSERT(packetBuffer);
100
101 WriteUint32(packetBuffer->GetWritableData(), offset, value);
102}
103
104void WriteUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint16_t value)
105{
106 BOOST_ASSERT(packetBuffer);
107
108 WriteUint16(packetBuffer->GetWritableData(), offset, value);
109}
110
Francis Murtagh3a161982019-09-04 15:25:02 +0100111void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
112{
113 BOOST_ASSERT(buffer);
114
115 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
116 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
117 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
118 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
119 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
120 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
121 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
122 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
123}
124
Ferran Balaguer73882172019-09-02 16:39:42 +0100125void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
126{
127 BOOST_ASSERT(buffer);
128
Matteo Martincigh149528e2019-09-05 12:02:04 +0100129 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100130 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
131 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
132 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
133}
134
135void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
136{
Matteo Martincigh149528e2019-09-05 12:02:04 +0100137 BOOST_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100138
Matteo Martincigh149528e2019-09-05 12:02:04 +0100139 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100140 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
141}
142
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100143uint64_t ReadUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
144{
145 BOOST_ASSERT(packetBuffer);
146
147 return ReadUint64(packetBuffer->GetReadableData(), offset);
148}
149
150uint32_t ReadUint32(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
151{
152 BOOST_ASSERT(packetBuffer);
153
154 return ReadUint32(packetBuffer->GetReadableData(), offset);
155}
156
157uint16_t ReadUint16(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
158{
159 BOOST_ASSERT(packetBuffer);
160
161 return ReadUint16(packetBuffer->GetReadableData(), offset);
162}
163
164uint8_t ReadUint8(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset)
165{
166 BOOST_ASSERT(packetBuffer);
167
168 return ReadUint8(packetBuffer->GetReadableData(), offset);
169}
170
Francis Murtagh3a161982019-09-04 15:25:02 +0100171uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
172{
173 BOOST_ASSERT(buffer);
174
175 uint64_t value = 0;
Matteo Martincighab173e92019-09-05 12:02:04 +0100176 value = static_cast<uint64_t>(buffer[offset]);
Francis Murtagh3a161982019-09-04 15:25:02 +0100177 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
178 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
179 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
180 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
181 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
182 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
183 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
184
185 return value;
186}
187
Ferran Balaguer73882172019-09-02 16:39:42 +0100188uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
189{
190 BOOST_ASSERT(buffer);
191
192 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100193 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100194 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
195 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
196 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
197 return value;
198}
199
200uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
201{
202 BOOST_ASSERT(buffer);
203
204 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100205 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100206 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
207 return static_cast<uint16_t>(value);
208}
209
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100210uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
211{
212 BOOST_ASSERT(buffer);
213
214 return buffer[offset];
215}
216
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100217std::string GetSoftwareInfo()
218{
219 return std::string("ArmNN");
220}
221
222std::string GetHardwareVersion()
223{
224 return std::string();
225}
226
227std::string GetSoftwareVersion()
228{
229 std::string armnnVersion(ARMNN_VERSION);
230 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
231 return result;
232}
233
234std::string GetProcessName()
235{
236 std::ifstream comm("/proc/self/comm");
237 std::string name;
238 getline(comm, name);
239 return name;
240}
241
Jan Eilers92fa15b2019-10-15 15:23:25 +0100242/// Creates a timeline packet header
243///
244/// \params
245/// packetFamiliy Timeline Packet Family
246/// packetClass Timeline Packet Class
247/// packetType Timeline Packet Type
248/// streamId Stream identifier
249/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100250/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
Jan Eilers92fa15b2019-10-15 15:23:25 +0100251///
252/// \returns
253/// Pair of uint32_t containing word0 and word1 of the header
254std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
255 uint32_t packetClass,
256 uint32_t packetType,
257 uint32_t streamId,
258 uint32_t sequenceNumbered,
259 uint32_t dataLength)
260{
261 // Packet header word 0:
262 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
263 // 19:25 [7] packet_class: packet class
264 // 16:18 [3] packet_type: packet type
265 // 8:15 [8] reserved: all zeros
266 // 0:7 [8] stream_id: stream identifier
267 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
268 ((packetClass & 0x0000007F) << 19) |
269 ((packetType & 0x00000007) << 16) |
270 ((streamId & 0x00000007) << 0);
271
272 // Packet header word 1:
273 // 25:31 [7] reserved: all zeros
274 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
275 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
276 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
277 ((dataLength & 0x00FFFFFF) << 0);
278
279 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
280}
281
282/// Creates a packet header for the timeline messages:
283/// * declareLabel
284/// * declareEntity
285/// * declareEventClass
286/// * declareRelationship
287/// * declareEvent
288///
289/// \param
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100290/// dataLength The length of the message body in bytes
Jan Eilers92fa15b2019-10-15 15:23:25 +0100291///
292/// \returns
293/// Pair of uint32_t containing word0 and word1 of the header
294std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
295{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100296 return CreateTimelinePacketHeader(1, // Packet family
297 0, // Packet class
298 1, // Packet type
299 0, // Stream id
300 0, // Sequence number
301 dataLength); // Data length
Jan Eilers92fa15b2019-10-15 15:23:25 +0100302}
303
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100304TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
305 const std::string& label,
306 unsigned char* buffer,
307 unsigned int bufferSize,
308 unsigned int& numberOfBytesWritten)
309{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100310 // Initialize the output value
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100311 numberOfBytesWritten = 0;
312
313 // Check that the given buffer is valid
314 if (buffer == nullptr || bufferSize == 0)
315 {
316 return TimelinePacketStatus::BufferExhaustion;
317 }
318
319 // Utils
320 unsigned int uint32_t_size = sizeof(uint32_t);
321 unsigned int uint64_t_size = sizeof(uint64_t);
322
323 // Convert the label into a SWTrace string
324 std::vector<uint32_t> swTraceLabel;
325 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
326 if (!result)
327 {
328 return TimelinePacketStatus::Error;
329 }
330
331 // Calculate the size of the SWTrace string label (in bytes)
332 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
333
334 // Calculate the length of the data (in bytes)
Jan Eilersb884ea42019-10-16 09:54:15 +0100335 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
336 uint64_t_size + // Profiling GUID
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100337 swTraceLabelSize; // Label
338
339 // Calculate the timeline binary packet size (in bytes)
340 unsigned int timelineLabelPacketSize = 2 * uint32_t_size + // Header (2 words)
Jan Eilersb884ea42019-10-16 09:54:15 +0100341 timelineLabelPacketDataLength; // decl_Id + Profiling GUID + label
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100342
343 // Check whether the timeline binary packet fits in the given buffer
344 if (timelineLabelPacketSize > bufferSize)
345 {
346 return TimelinePacketStatus::BufferExhaustion;
347 }
348
Jan Eilersb884ea42019-10-16 09:54:15 +0100349 // Create packet header
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100350 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineLabelPacketDataLength);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100351
352 // Initialize the offset for writing in the buffer
353 unsigned int offset = 0;
354
355 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100356 WriteUint32(buffer, offset, packetHeader.first);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100357 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100358 WriteUint32(buffer, offset, packetHeader.second);
359 offset += uint32_t_size;
360
361 // Write decl_Id to the buffer
362 WriteUint32(buffer, offset, 0u);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100363 offset += uint32_t_size;
364
365 // Write the timeline binary packet payload to the buffer
366 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
367 offset += uint64_t_size;
368 for (uint32_t swTraceLabelWord : swTraceLabel)
369 {
370 WriteUint32(buffer, offset, swTraceLabelWord); // Label
371 offset += uint32_t_size;
372 }
373
374 // Update the number of bytes written
375 numberOfBytesWritten = timelineLabelPacketSize;
376
377 return TimelinePacketStatus::Ok;
378}
379
David Monahanf21f6062019-10-07 15:11:15 +0100380TimelinePacketStatus WriteTimelineEntityBinaryPacket(uint64_t profilingGuid,
381 unsigned char* buffer,
382 unsigned int bufferSize,
383 unsigned int& numberOfBytesWritten)
384{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100385 // Initialize the output value
David Monahanf21f6062019-10-07 15:11:15 +0100386 numberOfBytesWritten = 0;
387
388 // Check that the given buffer is valid
389 if (buffer == nullptr || bufferSize == 0)
390 {
391 return TimelinePacketStatus::BufferExhaustion;
392 }
393
394 // Utils
395 unsigned int uint32_t_size = sizeof(uint32_t);
396 unsigned int uint64_t_size = sizeof(uint64_t);
397
398 // Calculate the length of the data (in bytes)
399 unsigned int timelineEntityPacketDataLength = uint64_t_size; // Profiling GUID
400
401
402 // Calculate the timeline binary packet size (in bytes)
403 unsigned int timelineEntityPacketSize = 2 * uint32_t_size + // Header (2 words)
Jan Eilersb884ea42019-10-16 09:54:15 +0100404 uint32_t_size + // decl_Id
405 timelineEntityPacketDataLength; // Profiling GUID
David Monahanf21f6062019-10-07 15:11:15 +0100406
407 // Check whether the timeline binary packet fits in the given buffer
408 if (timelineEntityPacketSize > bufferSize)
409 {
410 return TimelinePacketStatus::BufferExhaustion;
411 }
412
Jan Eilersb884ea42019-10-16 09:54:15 +0100413 // Create packet header
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100414 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEntityPacketDataLength);
David Monahanf21f6062019-10-07 15:11:15 +0100415
416 // Initialize the offset for writing in the buffer
417 unsigned int offset = 0;
418
419 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100420 WriteUint32(buffer, offset, packetHeader.first);
David Monahanf21f6062019-10-07 15:11:15 +0100421 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100422 WriteUint32(buffer, offset, packetHeader.second);
423 offset += uint32_t_size;
424
425 // Write the decl_Id to the buffer
426 WriteUint32(buffer, offset, 1u);
David Monahanf21f6062019-10-07 15:11:15 +0100427 offset += uint32_t_size;
428
429 // Write the timeline binary packet payload to the buffer
430 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
431
432 // Update the number of bytes written
433 numberOfBytesWritten = timelineEntityPacketSize;
434
435 return TimelinePacketStatus::Ok;
436}
437
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100438TimelinePacketStatus WriteTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
439 uint64_t relationshipGuid,
440 uint64_t headGuid,
441 uint64_t tailGuid,
442 unsigned char* buffer,
443 unsigned int bufferSize,
444 unsigned int& numberOfBytesWritten)
445{
446 // Initialize the output value
447 numberOfBytesWritten = 0;
448
449 // Check that the given buffer is valid
450 if (buffer == nullptr || bufferSize == 0)
451 {
452 return TimelinePacketStatus::BufferExhaustion;
453 }
454
455 // Utils
456 unsigned int uint32_t_size = sizeof(uint32_t);
457 unsigned int uint64_t_size = sizeof(uint64_t);
458
459 // Calculate the length of the data (in bytes)
460 unsigned int timelineRelationshipPacketDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
461 uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID
462
463 // Calculate the timeline binary packet size (in bytes)
464 unsigned int timelineRelationshipPacketSize = 2 * uint32_t_size + // Header (2 words)
465 timelineRelationshipPacketDataLength;
466
467 // Check whether the timeline binary packet fits in the given buffer
468 if (timelineRelationshipPacketSize > bufferSize)
469 {
470 return TimelinePacketStatus::BufferExhaustion;
471 }
472
473 // Create packet header
474 uint32_t dataLength = boost::numeric_cast<uint32_t>(timelineRelationshipPacketDataLength);
475 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(dataLength);
476
477 // Initialize the offset for writing in the buffer
478 unsigned int offset = 0;
479
480 // Write the timeline binary packet header to the buffer
481 WriteUint32(buffer, offset, packetHeader.first);
482 offset += uint32_t_size;
483 WriteUint32(buffer, offset, packetHeader.second);
484 offset += uint32_t_size;
485
486 uint32_t relationshipTypeUint = 0;
487
488 switch (relationshipType)
489 {
490 case ProfilingRelationshipType::RetentionLink:
491 relationshipTypeUint = 0;
492 break;
493 case ProfilingRelationshipType::ExecutionLink:
494 relationshipTypeUint = 1;
495 break;
496 case ProfilingRelationshipType::DataLink:
497 relationshipTypeUint = 2;
498 break;
499 case ProfilingRelationshipType::LabelLink:
500 relationshipTypeUint = 3;
501 break;
502 default:
503 throw InvalidArgumentException("Unknown relationship type given.");
504 }
505
506 // Write the timeline binary packet payload to the buffer
507 // decl_id of the timeline message
508 uint32_t declId = 3;
509 WriteUint32(buffer, offset, declId); // decl_id
510 offset += uint32_t_size;
511 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
512 offset += uint32_t_size;
513 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
514 offset += uint64_t_size;
515 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
516 offset += uint64_t_size;
517 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
518
519 // Update the number of bytes written
520 numberOfBytesWritten = timelineRelationshipPacketSize;
521
522 return TimelinePacketStatus::Ok;
523}
524
Sadik Armagan784db772019-10-08 15:05:38 +0100525TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
526 unsigned int bufferSize,
527 unsigned int& numberOfBytesWritten)
528{
529 // Initialize the output value
530 numberOfBytesWritten = 0;
531
532 // Check that the given buffer is valid
533 if (buffer == nullptr || bufferSize == 0)
534 {
535 return TimelinePacketStatus::BufferExhaustion;
536 }
537
538 // Utils
539 unsigned int uint32_t_size = sizeof(uint32_t);
540
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100541 // The payload/data of the packet consists of swtrace event definitions encoded according
Sadik Armagan784db772019-10-08 15:05:38 +0100542 // to the swtrace directory specification. The messages being the five defined below:
543 // | decl_id | decl_name | ui_name | arg_types | arg_names |
544 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
545 // | 0 | declareLabel | declare label | ps | guid,value |
546 // | 1 | declareEntity | declare entity | p | guid |
547 // | 2 | declareEventClass | declare event class | p | guid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100548 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
549 // | | | | | headGuid,tailGuid |
Sadik Armagan784db772019-10-08 15:05:38 +0100550 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
551
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100552 std::vector<std::vector<std::string>> timelineDirectoryMessages
553 {
554 {"declareLabel", "declare label", "ps", "guid,value"},
555 {"declareEntity", "declare entity", "p", "guid"},
556 {"declareEventClass", "declare event class", "p", "guid"},
557 {"declareRelationship", "declare relationship", "Ippp", "relationshipType,relationshipGuid,headGuid,tailGuid"},
558 {"declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid"}
559 };
Sadik Armagan784db772019-10-08 15:05:38 +0100560
561 unsigned int messagesDataLength = 0u;
562 std::vector<std::vector<std::vector<uint32_t>>> swTraceTimelineDirectoryMessages;
563
564 for (const auto& timelineDirectoryMessage : timelineDirectoryMessages)
565 {
566 messagesDataLength += uint32_t_size; // decl_id
567
568 std::vector<std::vector<uint32_t>> swTraceStringsVector;
569 for (const auto& label : timelineDirectoryMessage)
570 {
571 std::vector<uint32_t> swTraceString;
572 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceString);
573 if (!result)
574 {
575 return TimelinePacketStatus::Error;
576 }
577
578 messagesDataLength += boost::numeric_cast<unsigned int>(swTraceString.size()) * uint32_t_size;
579 swTraceStringsVector.push_back(swTraceString);
580 }
581 swTraceTimelineDirectoryMessages.push_back(swTraceStringsVector);
582 }
583
584 // Calculate the timeline directory binary packet size (in bytes)
585 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
586 messagesDataLength; // 5 messages length
587
588 // Check whether the timeline directory binary packet fits in the given buffer
589 if (timelineDirectoryPacketSize > bufferSize)
590 {
591 return TimelinePacketStatus::BufferExhaustion;
592 }
593
Jan Eilersb884ea42019-10-16 09:54:15 +0100594 // Create packet header
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100595 uint32_t dataLength = boost::numeric_cast<uint32_t>(messagesDataLength);
596 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, dataLength);
Sadik Armagan784db772019-10-08 15:05:38 +0100597
598 // Initialize the offset for writing in the buffer
599 unsigned int offset = 0;
600
601 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100602 WriteUint32(buffer, offset, packetHeader.first);
Sadik Armagan784db772019-10-08 15:05:38 +0100603 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100604 WriteUint32(buffer, offset, packetHeader.second);
Sadik Armagan784db772019-10-08 15:05:38 +0100605 offset += uint32_t_size;
606
607 for (unsigned int i = 0u; i < swTraceTimelineDirectoryMessages.size(); ++i)
608 {
609 // Write the timeline binary packet payload to the buffer
610 WriteUint32(buffer, offset, i); // decl_id
611 offset += uint32_t_size;
612
613 for (std::vector<uint32_t> swTraceString : swTraceTimelineDirectoryMessages[i])
614 {
615 for (uint32_t swTraceDeclStringWord : swTraceString)
616 {
617 WriteUint32(buffer, offset, swTraceDeclStringWord);
618 offset += uint32_t_size;
619 }
620 }
621 }
622
623 // Update the number of bytes written
624 numberOfBytesWritten = timelineDirectoryPacketSize;
625
626 return TimelinePacketStatus::Ok;
627}
628
Jan Eilers92fa15b2019-10-15 15:23:25 +0100629TimelinePacketStatus WriteTimelineEventClassBinaryPacket(uint64_t profilingGuid,
630 unsigned char* buffer,
631 unsigned int bufferSize,
632 unsigned int& numberOfBytesWritten)
633{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100634 // Initialize the output value
Jan Eilers92fa15b2019-10-15 15:23:25 +0100635 numberOfBytesWritten = 0;
636
637 // Check that the given buffer is valid
638 if (buffer == nullptr || bufferSize == 0)
639 {
640 return TimelinePacketStatus::BufferExhaustion;
641 }
642
643 // Utils
644 unsigned int uint32_t_size = sizeof(uint32_t);
645 unsigned int uint64_t_size = sizeof(uint64_t);
646
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100647 // decl_id of the timeline message
648 uint32_t declId = 2;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100649
650 // Calculate the length of the data (in bytes)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100651 unsigned int packetBodySize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100652
653 // Calculate the timeline binary packet size (in bytes)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100654 unsigned int packetSize = 2 * uint32_t_size + // Header (2 words)
655 packetBodySize; // Body
Jan Eilers92fa15b2019-10-15 15:23:25 +0100656
657 // Check whether the timeline binary packet fits in the given buffer
658 if (packetSize > bufferSize)
659 {
660 return TimelinePacketStatus::BufferExhaustion;
661 }
662
663 // Create packet header
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100664 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(packetBodySize);
Jan Eilers92fa15b2019-10-15 15:23:25 +0100665
666 // Initialize the offset for writing in the buffer
667 unsigned int offset = 0;
668
669 // Write the timeline binary packet header to the buffer
670 WriteUint32(buffer, offset, packetHeader.first);
671 offset += uint32_t_size;
672 WriteUint32(buffer, offset, packetHeader.second);
673 offset += uint32_t_size;
674
675 // Write the timeline binary packet payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100676 WriteUint32(buffer, offset, declId); // decl_id
Jan Eilers92fa15b2019-10-15 15:23:25 +0100677 offset += uint32_t_size;
678 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
679
680 // Update the number of bytes written
681 numberOfBytesWritten = packetSize;
682
683 return TimelinePacketStatus::Ok;
684}
685
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100686TimelinePacketStatus WriteTimelineEventBinaryPacket(uint64_t timestamp,
687 uint32_t threadId,
688 uint64_t profilingGuid,
689 unsigned char* buffer,
690 unsigned int bufferSize,
691 unsigned int& numberOfBytesWritten)
692{
693 // Initialize the output value
694 numberOfBytesWritten = 0;
695
696 // Check that the given buffer is valid
697 if (buffer == nullptr || bufferSize == 0)
698 {
699 return TimelinePacketStatus::BufferExhaustion;
700 }
701
702 // Utils
703 unsigned int uint32_t_size = sizeof(uint32_t);
704 unsigned int uint64_t_size = sizeof(uint64_t);
705
706 // decl_id of the timeline message
707 uint32_t declId = 4;
708
709 // Calculate the length of the data (in bytes)
710 unsigned int timelineEventPacketDataLength = uint32_t_size + // decl_id
711 uint64_t_size + // Timestamp
712 uint32_t_size + // Thread id
713 uint64_t_size; // Profiling GUID
714
715 // Calculate the timeline binary packet size (in bytes)
716 unsigned int timelineEventPacketSize = 2 * uint32_t_size + // Header (2 words)
717 timelineEventPacketDataLength; // Timestamp + thread id + profiling GUID
718
719 // Check whether the timeline binary packet fits in the given buffer
720 if (timelineEventPacketSize > bufferSize)
721 {
722 return TimelinePacketStatus::BufferExhaustion;
723 }
724
725 // Create packet header
726 std::pair<uint32_t, uint32_t> packetHeader = CreateTimelineMessagePacketHeader(timelineEventPacketDataLength);
727
728 // Initialize the offset for writing in the buffer
729 unsigned int offset = 0;
730
731 // Write the timeline binary packet header to the buffer
732 WriteUint32(buffer, offset, packetHeader.first);
733 offset += uint32_t_size;
734 WriteUint32(buffer, offset, packetHeader.second);
735 offset += uint32_t_size;
736
737 // Write the timeline binary packet payload to the buffer
738 WriteUint32(buffer, offset, declId); // decl_id
739 offset += uint32_t_size;
740 WriteUint64(buffer, offset, timestamp); // Timestamp
741 offset += uint64_t_size;
742 WriteUint32(buffer, offset, threadId); // Thread id
743 offset += uint32_t_size;
744 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
745 offset += uint64_t_size;
746
747 // Update the number of bytes written
748 numberOfBytesWritten = timelineEventPacketSize;
749
750 return TimelinePacketStatus::Ok;
751}
752
Ferran Balaguer73882172019-09-02 16:39:42 +0100753} // namespace profiling
754
Matteo Martincigh149528e2019-09-05 12:02:04 +0100755} // namespace armnn