blob: 2482e2c2050772a678c853a3d2003d952324d0f9 [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
Jim Flynn4e755a52020-03-29 17:48:26 +01008#include "common/include/ProfilingException.hpp"
9
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010010#include <armnn/Version.hpp>
11
Matteo Martincigh5dc816e2019-11-04 14:05:28 +000012#include <WallClockTimer.hpp>
13
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010014#include <armnn/utility/Assert.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010015
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010016#include <fstream>
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <iostream>
Matteo Martincighab173e92019-09-05 12:02:04 +010018#include <limits>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010019
Ferran Balaguer73882172019-09-02 16:39:42 +010020namespace armnn
21{
22
23namespace profiling
24{
25
Matteo Martincigh6db5f202019-09-05 12:02:04 +010026namespace
Matteo Martincighab173e92019-09-05 12:02:04 +010027{
Matteo Martincighab173e92019-09-05 12:02:04 +010028
Matteo Martincigh6db5f202019-09-05 12:02:04 +010029void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
30{
Matteo Martincighab173e92019-09-05 12:02:04 +010031 // Check that it is possible to generate the next UID without causing an overflow
Matteo Martincigh6db5f202019-09-05 12:02:04 +010032 switch (cores)
Matteo Martincighab173e92019-09-05 12:02:04 +010033 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010034 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;
Matteo Martincighab173e92019-09-05 12:02:04 +010051 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010052}
Matteo Martincighab173e92019-09-05 12:02:04 +010053
Matteo Martincigh6db5f202019-09-05 12:02:04 +010054} // Anonymous namespace
55
56uint16_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
Keith Davise394bd92019-12-02 15:12:19 +000076std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores)
Matteo Martincigh6db5f202019-09-05 12:02:04 +010077{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010078 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
Keith Davise394bd92019-12-02 15:12:19 +000079 ThrowIfCantGenerateNextUid(firstUid, cores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +010080
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 {
Keith Davise394bd92019-12-02 15:12:19 +000086 counterUids[i] = firstUid++;
Matteo Martincigh6db5f202019-09-05 12:02:04 +010087 }
88 return counterUids;
Matteo Martincighab173e92019-09-05 12:02:04 +010089}
90
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000091void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
92{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010093 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000094
95 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
96}
97
Keith Davis3201eea2019-10-24 17:30:41 +010098uint32_t ConstructHeader(uint32_t packetFamily,
99 uint32_t packetId)
100{
Keith Davis33ed2212020-03-30 10:43:41 +0100101 return (( packetFamily & 0x0000003F ) << 26 )|
102 (( packetId & 0x000003FF ) << 16 );
Keith Davis3201eea2019-10-24 17:30:41 +0100103}
104
105void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100106{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100107 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100108
109 WriteUint64(packetBuffer->GetWritableData(), offset, value);
110}
111
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000112void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100113{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100114 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100115
116 WriteUint32(packetBuffer->GetWritableData(), offset, value);
117}
118
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000119void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100120{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100121 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100122
123 WriteUint16(packetBuffer->GetWritableData(), offset, value);
124}
125
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000126void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
127{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100128 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000129
130 WriteUint8(packetBuffer->GetWritableData(), offset, value);
131}
132
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000133void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
134{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100135 ARMNN_ASSERT(buffer);
136 ARMNN_ASSERT(value);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000137
138 for (unsigned int i = 0; i < valueSize; i++, offset++)
139 {
140 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
141 }
142}
143
Francis Murtagh3a161982019-09-04 15:25:02 +0100144void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
145{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100146 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100147
148 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
149 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
150 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
151 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
152 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
153 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
154 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
155 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
156}
157
Ferran Balaguer73882172019-09-02 16:39:42 +0100158void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
159{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100160 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100161
Matteo Martincigh149528e2019-09-05 12:02:04 +0100162 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100163 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
164 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
165 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
166}
167
168void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
169{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100170 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100171
Matteo Martincigh149528e2019-09-05 12:02:04 +0100172 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100173 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
174}
175
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000176void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
177{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100178 ARMNN_ASSERT(buffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000179
180 buffer[offset] = static_cast<unsigned char>(value);
181}
182
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000183void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
184{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100185 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000186
187 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
188}
189
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000190uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100191{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100192 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100193
194 return ReadUint64(packetBuffer->GetReadableData(), offset);
195}
196
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000197uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100198{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100199 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100200
201 return ReadUint32(packetBuffer->GetReadableData(), offset);
202}
203
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000204uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100205{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100206 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100207
208 return ReadUint16(packetBuffer->GetReadableData(), offset);
209}
210
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000211uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100212{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100213 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100214
215 return ReadUint8(packetBuffer->GetReadableData(), offset);
216}
217
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000218void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
219{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100220 ARMNN_ASSERT(buffer);
221 ARMNN_ASSERT(outValue);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000222
223 for (unsigned int i = 0; i < valueSize; i++, offset++)
224 {
225 outValue[i] = static_cast<uint8_t>(buffer[offset]);
226 }
227}
228
Francis Murtagh3a161982019-09-04 15:25:02 +0100229uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
230{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100231 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100232
233 uint64_t value = 0;
Matteo Martincighab173e92019-09-05 12:02:04 +0100234 value = static_cast<uint64_t>(buffer[offset]);
Francis Murtagh3a161982019-09-04 15:25:02 +0100235 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
236 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
237 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
238 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
239 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
240 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
241 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
242
243 return value;
244}
245
Ferran Balaguer73882172019-09-02 16:39:42 +0100246uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
247{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100248 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100249
250 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100251 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100252 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
253 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
254 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
255 return value;
256}
257
258uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
259{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100260 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100261
262 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100263 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100264 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
265 return static_cast<uint16_t>(value);
266}
267
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100268uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
269{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100270 ARMNN_ASSERT(buffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100271
272 return buffer[offset];
273}
274
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100275std::string GetSoftwareInfo()
276{
277 return std::string("ArmNN");
278}
279
280std::string GetHardwareVersion()
281{
282 return std::string();
283}
284
285std::string GetSoftwareVersion()
286{
Francis Murtaghd36c5282020-05-22 12:49:25 +0100287 std::string result = "Armnn " + std::to_string(ARMNN_MAJOR_VERSION) + "." + std::to_string(ARMNN_MINOR_VERSION);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100288 return result;
289}
290
291std::string GetProcessName()
292{
293 std::ifstream comm("/proc/self/comm");
294 std::string name;
295 getline(comm, name);
296 return name;
297}
298
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000299// Calculate the actual length an SwString will be including the terminating null character
300// padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
301// the size to allow the offset to be correctly updated when decoding a binary packet.
302uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
303{
304 std::vector<uint32_t> swTraceString;
305 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
306 unsigned int uint32_t_size = sizeof(uint32_t);
307 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
308 return size;
309}
310
311// Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
312SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
313{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100314 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000315
316 unsigned int uint32_t_size = sizeof(uint32_t);
317
318 SwTraceMessage swTraceMessage;
319
320 // Read the decl_id
321 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
322 swTraceMessage.m_Id = readDeclId;
323
324 // SWTrace "namestring" format
325 // length of the string (first 4 bytes) + string + null terminator
326
327 // Check the decl_name
328 offset += uint32_t_size;
329 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
330
331 offset += uint32_t_size;
332 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
333 std::memcpy(swTraceStringBuffer.data(),
334 packetBuffer + offset, swTraceStringBuffer.size());
335
336 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
337
338 // Check the ui_name
339 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
340 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
341
342 offset += uint32_t_size;
343 swTraceStringBuffer.resize(swTraceUINameLength - 1);
344 std::memcpy(swTraceStringBuffer.data(),
345 packetBuffer + offset, swTraceStringBuffer.size());
346
347 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
348
349 // Check arg_types
350 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
351 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
352
353 offset += uint32_t_size;
354 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
355 std::memcpy(swTraceStringBuffer.data(),
356 packetBuffer + offset, swTraceStringBuffer.size());
357
358 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
359
360 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
361
362 // Check arg_names
363 offset += CalculateSizeOfPaddedSwString(swTraceString);
364 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
365
366 offset += uint32_t_size;
367 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
368 std::memcpy(swTraceStringBuffer.data(),
369 packetBuffer + offset, swTraceStringBuffer.size());
370
371 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
372 std::stringstream stringStream(swTraceString);
373 std::string argName;
374 while (std::getline(stringStream, argName, ','))
375 {
376 swTraceMessage.m_ArgNames.push_back(argName);
377 }
378
379 offset += CalculateSizeOfPaddedSwString(swTraceString);
380
381 return swTraceMessage;
382}
383
Jan Eilers92fa15b2019-10-15 15:23:25 +0100384/// Creates a timeline packet header
385///
386/// \params
387/// packetFamiliy Timeline Packet Family
388/// packetClass Timeline Packet Class
389/// packetType Timeline Packet Type
390/// streamId Stream identifier
391/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100392/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
Jan Eilers92fa15b2019-10-15 15:23:25 +0100393///
394/// \returns
395/// Pair of uint32_t containing word0 and word1 of the header
396std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
397 uint32_t packetClass,
398 uint32_t packetType,
399 uint32_t streamId,
400 uint32_t sequenceNumbered,
401 uint32_t dataLength)
402{
403 // Packet header word 0:
404 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
405 // 19:25 [7] packet_class: packet class
406 // 16:18 [3] packet_type: packet type
407 // 8:15 [8] reserved: all zeros
408 // 0:7 [8] stream_id: stream identifier
409 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
410 ((packetClass & 0x0000007F) << 19) |
411 ((packetType & 0x00000007) << 16) |
412 ((streamId & 0x00000007) << 0);
413
414 // Packet header word 1:
415 // 25:31 [7] reserved: all zeros
416 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
417 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
418 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
419 ((dataLength & 0x00FFFFFF) << 0);
420
421 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
422}
423
424/// Creates a packet header for the timeline messages:
425/// * declareLabel
426/// * declareEntity
427/// * declareEventClass
428/// * declareRelationship
429/// * declareEvent
430///
431/// \param
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100432/// dataLength The length of the message body in bytes
Jan Eilers92fa15b2019-10-15 15:23:25 +0100433///
434/// \returns
435/// Pair of uint32_t containing word0 and word1 of the header
436std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
437{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100438 return CreateTimelinePacketHeader(1, // Packet family
439 0, // Packet class
440 1, // Packet type
441 0, // Stream id
442 0, // Sequence number
443 dataLength); // Data length
Jan Eilers92fa15b2019-10-15 15:23:25 +0100444}
445
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100446TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
447 const std::string& label,
448 unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000449 unsigned int remainingBufferSize,
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100450 unsigned int& numberOfBytesWritten)
451{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100452 // Initialize the output value
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100453 numberOfBytesWritten = 0;
454
455 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000456 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100457 {
458 return TimelinePacketStatus::BufferExhaustion;
459 }
460
461 // Utils
462 unsigned int uint32_t_size = sizeof(uint32_t);
463 unsigned int uint64_t_size = sizeof(uint64_t);
464
465 // Convert the label into a SWTrace string
466 std::vector<uint32_t> swTraceLabel;
467 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
468 if (!result)
469 {
470 return TimelinePacketStatus::Error;
471 }
472
473 // Calculate the size of the SWTrace string label (in bytes)
474 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
475
476 // Calculate the length of the data (in bytes)
Jan Eilersb884ea42019-10-16 09:54:15 +0100477 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
478 uint64_t_size + // Profiling GUID
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100479 swTraceLabelSize; // Label
480
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100481 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000482 if (timelineLabelPacketDataLength > remainingBufferSize)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100483 {
484 return TimelinePacketStatus::BufferExhaustion;
485 }
486
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100487 // Initialize the offset for writing in the buffer
488 unsigned int offset = 0;
489
Jan Eilersb884ea42019-10-16 09:54:15 +0100490 // Write decl_Id to the buffer
491 WriteUint32(buffer, offset, 0u);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100492 offset += uint32_t_size;
493
494 // Write the timeline binary packet payload to the buffer
495 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
496 offset += uint64_t_size;
497 for (uint32_t swTraceLabelWord : swTraceLabel)
498 {
499 WriteUint32(buffer, offset, swTraceLabelWord); // Label
500 offset += uint32_t_size;
501 }
502
503 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000504 numberOfBytesWritten = timelineLabelPacketDataLength;
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100505
506 return TimelinePacketStatus::Ok;
507}
508
Keith Davis97da5e22020-03-05 16:25:28 +0000509TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
510 unsigned char* buffer,
511 unsigned int remainingBufferSize,
512 unsigned int& numberOfBytesWritten)
David Monahanf21f6062019-10-07 15:11:15 +0100513{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100514 // Initialize the output value
David Monahanf21f6062019-10-07 15:11:15 +0100515 numberOfBytesWritten = 0;
516
517 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000518 if (buffer == nullptr || remainingBufferSize == 0)
David Monahanf21f6062019-10-07 15:11:15 +0100519 {
520 return TimelinePacketStatus::BufferExhaustion;
521 }
522
523 // Utils
524 unsigned int uint32_t_size = sizeof(uint32_t);
525 unsigned int uint64_t_size = sizeof(uint64_t);
526
527 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000528 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
David Monahanf21f6062019-10-07 15:11:15 +0100529
530 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000531 if (timelineEntityDataLength > remainingBufferSize)
David Monahanf21f6062019-10-07 15:11:15 +0100532 {
533 return TimelinePacketStatus::BufferExhaustion;
534 }
535
David Monahanf21f6062019-10-07 15:11:15 +0100536 // Initialize the offset for writing in the buffer
537 unsigned int offset = 0;
538
Jan Eilersb884ea42019-10-16 09:54:15 +0100539 // Write the decl_Id to the buffer
540 WriteUint32(buffer, offset, 1u);
David Monahanf21f6062019-10-07 15:11:15 +0100541 offset += uint32_t_size;
542
543 // Write the timeline binary packet payload to the buffer
544 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
545
546 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000547 numberOfBytesWritten = timelineEntityDataLength;
David Monahanf21f6062019-10-07 15:11:15 +0100548
549 return TimelinePacketStatus::Ok;
550}
551
Keith Davis97da5e22020-03-05 16:25:28 +0000552TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
553 uint64_t relationshipGuid,
554 uint64_t headGuid,
555 uint64_t tailGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +0100556 uint64_t attributeGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000557 unsigned char* buffer,
558 unsigned int remainingBufferSize,
559 unsigned int& numberOfBytesWritten)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100560{
561 // Initialize the output value
562 numberOfBytesWritten = 0;
563
564 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000565 if (buffer == nullptr || remainingBufferSize == 0)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100566 {
567 return TimelinePacketStatus::BufferExhaustion;
568 }
569
570 // Utils
571 unsigned int uint32_t_size = sizeof(uint32_t);
572 unsigned int uint64_t_size = sizeof(uint64_t);
573
574 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000575 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
Finn Williams0a336dc2020-05-11 15:39:58 +0100576 uint64_t_size * 4; // Relationship GUID + Head GUID +
577 // tail GUID + attributeGuid
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100578
Keith Davis97da5e22020-03-05 16:25:28 +0000579 // Check whether the timeline binary fits in the given buffer
580 if (timelineRelationshipDataLength > remainingBufferSize)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100581 {
582 return TimelinePacketStatus::BufferExhaustion;
583 }
584
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100585 // Initialize the offset for writing in the buffer
586 unsigned int offset = 0;
587
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100588 uint32_t relationshipTypeUint = 0;
589
590 switch (relationshipType)
591 {
592 case ProfilingRelationshipType::RetentionLink:
593 relationshipTypeUint = 0;
594 break;
595 case ProfilingRelationshipType::ExecutionLink:
596 relationshipTypeUint = 1;
597 break;
598 case ProfilingRelationshipType::DataLink:
599 relationshipTypeUint = 2;
600 break;
601 case ProfilingRelationshipType::LabelLink:
602 relationshipTypeUint = 3;
603 break;
604 default:
605 throw InvalidArgumentException("Unknown relationship type given.");
606 }
607
Keith Davis97da5e22020-03-05 16:25:28 +0000608 // Write the timeline binary payload to the buffer
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100609 // decl_id of the timeline message
610 uint32_t declId = 3;
611 WriteUint32(buffer, offset, declId); // decl_id
612 offset += uint32_t_size;
613 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
614 offset += uint32_t_size;
615 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
616 offset += uint64_t_size;
617 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
618 offset += uint64_t_size;
619 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
Finn Williams0a336dc2020-05-11 15:39:58 +0100620 offset += uint64_t_size;
621 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
622
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100623
624 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000625 numberOfBytesWritten = timelineRelationshipDataLength;
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100626
627 return TimelinePacketStatus::Ok;
628}
629
Sadik Armagan784db772019-10-08 15:05:38 +0100630TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000631 unsigned int remainingBufferSize,
Sadik Armagan784db772019-10-08 15:05:38 +0100632 unsigned int& numberOfBytesWritten)
633{
634 // Initialize the output value
635 numberOfBytesWritten = 0;
636
637 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000638 if (buffer == nullptr || remainingBufferSize == 0)
Sadik Armagan784db772019-10-08 15:05:38 +0100639 {
640 return TimelinePacketStatus::BufferExhaustion;
641 }
642
643 // Utils
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000644 unsigned int uint8_t_size = sizeof(uint8_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100645 unsigned int uint32_t_size = sizeof(uint32_t);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000646 unsigned int uint64_t_size = sizeof(uint64_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100647
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100648 // The payload/data of the packet consists of swtrace event definitions encoded according
Sadik Armagan784db772019-10-08 15:05:38 +0100649 // to the swtrace directory specification. The messages being the five defined below:
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000650 //
651 // | decl_id | decl_name | ui_name | arg_types | arg_names |
Sadik Armagan784db772019-10-08 15:05:38 +0100652 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
653 // | 0 | declareLabel | declare label | ps | guid,value |
654 // | 1 | declareEntity | declare entity | p | guid |
655 // | 2 | declareEventClass | declare event class | p | guid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100656 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
657 // | | | | | headGuid,tailGuid |
Sadik Armagan784db772019-10-08 15:05:38 +0100658 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100659 std::vector<std::vector<std::string>> timelineDirectoryMessages
660 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000661 { "0", "declareLabel", "declare label", "ps", "guid,value" },
662 { "1", "declareEntity", "declare entity", "p", "guid" },
663 { "2", "declareEventClass", "declare event class", "p", "guid" },
664 { "3", "declareRelationship", "declare relationship", "Ippp",
665 "relationshipType,relationshipGuid,headGuid,tailGuid" },
666 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100667 };
Sadik Armagan784db772019-10-08 15:05:38 +0100668
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000669 // Build the message declarations
670 std::vector<uint32_t> swTraceBuffer;
671 for (const auto& directoryComponent : timelineDirectoryMessages)
Sadik Armagan784db772019-10-08 15:05:38 +0100672 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000673 // decl_id
674 uint32_t declId = 0;
675 try
Sadik Armagan784db772019-10-08 15:05:38 +0100676 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000677 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
Sadik Armagan784db772019-10-08 15:05:38 +0100678 }
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000679 catch (const std::exception&)
680 {
681 return TimelinePacketStatus::Error;
682 }
683 swTraceBuffer.push_back(declId);
684
685 bool result = true;
686 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
687 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
688 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
689 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
690 if (!result)
691 {
692 return TimelinePacketStatus::Error;
693 }
Sadik Armagan784db772019-10-08 15:05:38 +0100694 }
695
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000696 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
697 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
698 uint32_t_size; // Trace directory (5 messages)
699
Sadik Armagan784db772019-10-08 15:05:38 +0100700 // Calculate the timeline directory binary packet size (in bytes)
701 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000702 dataLength; // Payload
Sadik Armagan784db772019-10-08 15:05:38 +0100703
704 // Check whether the timeline directory binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000705 if (timelineDirectoryPacketSize > remainingBufferSize)
Sadik Armagan784db772019-10-08 15:05:38 +0100706 {
707 return TimelinePacketStatus::BufferExhaustion;
708 }
709
Jan Eilersb884ea42019-10-16 09:54:15 +0100710 // Create packet header
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000711 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
Sadik Armagan784db772019-10-08 15:05:38 +0100712
713 // Initialize the offset for writing in the buffer
714 unsigned int offset = 0;
715
716 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100717 WriteUint32(buffer, offset, packetHeader.first);
Sadik Armagan784db772019-10-08 15:05:38 +0100718 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100719 WriteUint32(buffer, offset, packetHeader.second);
Sadik Armagan784db772019-10-08 15:05:38 +0100720 offset += uint32_t_size;
721
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000722 // Write the stream header
723 uint8_t streamVersion = 4;
724 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100725 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000726 switch (threadIdBytes)
Sadik Armagan784db772019-10-08 15:05:38 +0100727 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000728 case 4: // Typically Windows and Android
729 case 8: // Typically Linux
730 break; // Valid values
731 default:
732 return TimelinePacketStatus::Error; // Invalid value
733 }
734 WriteUint8(buffer, offset, streamVersion);
735 offset += uint8_t_size;
736 WriteUint8(buffer, offset, pointerBytes);
737 offset += uint8_t_size;
738 WriteUint8(buffer, offset, threadIdBytes);
739 offset += uint8_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100740
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000741 // Write the SWTrace directory
742 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
743 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
744 offset += uint32_t_size;
745 for (uint32_t i : swTraceBuffer)
746 {
747 WriteUint32(buffer, offset, i); // Message declarations
748 offset += uint32_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100749 }
750
751 // Update the number of bytes written
752 numberOfBytesWritten = timelineDirectoryPacketSize;
753
754 return TimelinePacketStatus::Ok;
755}
756
Keith Davis97da5e22020-03-05 16:25:28 +0000757TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
Jim Flynn1892d212020-05-26 21:10:49 +0100758 uint64_t nameGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000759 unsigned char* buffer,
760 unsigned int remainingBufferSize,
761 unsigned int& numberOfBytesWritten)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100762{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100763 // Initialize the output value
Jan Eilers92fa15b2019-10-15 15:23:25 +0100764 numberOfBytesWritten = 0;
765
766 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000767 if (buffer == nullptr || remainingBufferSize == 0)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100768 {
769 return TimelinePacketStatus::BufferExhaustion;
770 }
771
772 // Utils
773 unsigned int uint32_t_size = sizeof(uint32_t);
774 unsigned int uint64_t_size = sizeof(uint64_t);
775
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100776 // decl_id of the timeline message
777 uint32_t declId = 2;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100778
779 // Calculate the length of the data (in bytes)
Jim Flynn1892d212020-05-26 21:10:49 +0100780 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100781
Keith Davis97da5e22020-03-05 16:25:28 +0000782 // Check whether the timeline binary fits in the given buffer
783 if (dataSize > remainingBufferSize)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100784 {
785 return TimelinePacketStatus::BufferExhaustion;
786 }
787
Jan Eilers92fa15b2019-10-15 15:23:25 +0100788 // Initialize the offset for writing in the buffer
789 unsigned int offset = 0;
790
Keith Davis97da5e22020-03-05 16:25:28 +0000791 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100792 WriteUint32(buffer, offset, declId); // decl_id
Jan Eilers92fa15b2019-10-15 15:23:25 +0100793 offset += uint32_t_size;
794 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
Jim Flynn1892d212020-05-26 21:10:49 +0100795 offset += uint64_t_size;
796 WriteUint64(buffer, offset, nameGuid); // Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100797
798 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000799 numberOfBytesWritten = dataSize;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100800
801 return TimelinePacketStatus::Ok;
802}
803
Keith Davis97da5e22020-03-05 16:25:28 +0000804TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
805 std::thread::id threadId,
806 uint64_t profilingGuid,
807 unsigned char* buffer,
808 unsigned int remainingBufferSize,
809 unsigned int& numberOfBytesWritten)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100810{
811 // Initialize the output value
812 numberOfBytesWritten = 0;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100813 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000814 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100815 {
816 return TimelinePacketStatus::BufferExhaustion;
817 }
818
819 // Utils
820 unsigned int uint32_t_size = sizeof(uint32_t);
821 unsigned int uint64_t_size = sizeof(uint64_t);
822
823 // decl_id of the timeline message
824 uint32_t declId = 4;
825
826 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000827 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
828 uint64_t_size + // Timestamp
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100829 ThreadIdSize + // Thread id
Keith Davis97da5e22020-03-05 16:25:28 +0000830 uint64_t_size; // Profiling GUID
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100831
832 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000833 if (timelineEventDataLength > remainingBufferSize)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100834 {
835 return TimelinePacketStatus::BufferExhaustion;
836 }
837
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100838 // Initialize the offset for writing in the buffer
839 unsigned int offset = 0;
840
Keith Davis97da5e22020-03-05 16:25:28 +0000841 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100842 WriteUint32(buffer, offset, declId); // decl_id
843 offset += uint32_t_size;
844 WriteUint64(buffer, offset, timestamp); // Timestamp
845 offset += uint64_t_size;
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100846 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
847 offset += ThreadIdSize;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100848 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
849 offset += uint64_t_size;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100850 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000851 numberOfBytesWritten = timelineEventDataLength;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100852
853 return TimelinePacketStatus::Ok;
854}
855
Keith Davis3201eea2019-10-24 17:30:41 +0100856std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
857{
858 std::stringstream outputStream, centrePadding;
859 int padding = spacingWidth - static_cast<int>(stringToPass.size());
860
861 for (int i = 0; i < padding / 2; ++i)
862 {
863 centrePadding << " ";
864 }
865
866 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
867
868 if (padding > 0 && padding %2 != 0)
869 {
870 outputStream << " ";
871 }
872
873 return outputStream.str();
874}
875
876void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
877{
878 std::string body;
879
880 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
881 body.append(" | ");
882 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
883 body.append(" | ");
884 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
885 body.append("\n");
886
887 std::cout << std::string(body.size(), '-') << "\n";
888 std::cout<< body;
889}
890
891void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
892{
893 std::string body;
894
895 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
896 body.append(" | ");
897 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
898 body.append(" | ");
899 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
900 body.append("\n");
901
902 std::cout << std::string(body.size(), '-') << "\n";
903
904 std::cout<< body;
905}
906
907void PrintCounterDetails(std::shared_ptr<Counter>& counter)
908{
909 std::string body;
910
911 body.append(CentreAlignFormatting(counter->m_Name, 20));
912 body.append(" | ");
913 body.append(CentreAlignFormatting(counter->m_Description, 50));
914 body.append(" | ");
915 body.append(CentreAlignFormatting(counter->m_Units, 14));
916 body.append(" | ");
917 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
918 body.append(" | ");
919 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
920 body.append(" | ");
921 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
922 body.append(" | ");
923 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
924 body.append(" | ");
925 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
926 body.append(" | ");
927 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
928 body.append(" | ");
929 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
930
931 body.append("\n");
932
933 std::cout << std::string(body.size(), '-') << "\n";
934
935 std::cout << body;
936}
937
938void PrintCategoryDetails(const std::unique_ptr<Category>& category,
939 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
940{
941 std::string categoryBody;
942 std::string categoryHeader;
943
944 categoryHeader.append(CentreAlignFormatting("Name", 20));
945 categoryHeader.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100946 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
947 categoryHeader.append("\n");
948
949 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
950 categoryBody.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100951 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
952
953 std::cout << "\n" << "\n";
954 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
955 std::cout << "\n";
956 std::cout << std::string(categoryHeader.size(), '=') << "\n";
957
958 std::cout << categoryHeader;
959
960 std::cout << std::string(categoryBody.size(), '-') << "\n";
961
962 std::cout << categoryBody;
963
964 std::string counterHeader;
965
966 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
967 counterHeader.append(" | ");
968 counterHeader.append(CentreAlignFormatting("Description", 50));
969 counterHeader.append(" | ");
970 counterHeader.append(CentreAlignFormatting("Units", 14));
971 counterHeader.append(" | ");
972 counterHeader.append(CentreAlignFormatting("UID", 6));
973 counterHeader.append(" | ");
974 counterHeader.append(CentreAlignFormatting("Max UID", 10));
975 counterHeader.append(" | ");
976 counterHeader.append(CentreAlignFormatting("Class", 8));
977 counterHeader.append(" | ");
978 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
979 counterHeader.append(" | ");
980 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
981 counterHeader.append(" | ");
982 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
983 counterHeader.append(" | ");
984 counterHeader.append(CentreAlignFormatting("Device UID", 14));
985 counterHeader.append("\n");
986
987 std::cout << "\n" << "\n";
988 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
989 static_cast<int>(counterHeader.size()));
990 std::cout << "\n";
991 std::cout << std::string(counterHeader.size(), '=') << "\n";
992 std::cout << counterHeader;
993 for (auto& it: category->m_Counters) {
994 auto search = counterMap.find(it);
995 if(search != counterMap.end()) {
996 PrintCounterDetails(search->second);
997 }
998 }
999}
1000
1001void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1002{
1003 std::string devicesHeader;
1004
1005 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1006 devicesHeader.append(" | ");
1007 devicesHeader.append(CentreAlignFormatting("UID", 13));
1008 devicesHeader.append(" | ");
1009 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1010 devicesHeader.append("\n");
1011
1012 std::cout << "\n" << "\n";
1013 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1014 std::cout << "\n";
1015 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1016 std::cout << devicesHeader;
1017 for (auto& it: counterDirectory.GetDevices()) {
1018 PrintDeviceDetails(it);
1019 }
1020
1021 std::string counterSetHeader;
1022
1023 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1024 counterSetHeader.append(" | ");
1025 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1026 counterSetHeader.append(" | ");
1027 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1028 counterSetHeader.append("\n");
1029
1030 std::cout << "\n" << "\n";
1031 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1032 std::cout << "\n";
1033 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1034
1035 std::cout << counterSetHeader;
1036
1037 for (auto& it: counterDirectory.GetCounterSets()) {
1038 PrintCounterSetDetails(it);
1039 }
1040
1041 auto counters = counterDirectory.GetCounters();
1042 for (auto& it: counterDirectory.GetCategories()) {
1043 PrintCategoryDetails(it, counters);
1044 }
1045 std::cout << "\n";
1046}
1047
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001048uint64_t GetTimestamp()
1049{
1050#if USE_CLOCK_MONOTONIC_RAW
1051 using clock = MonotonicClockRaw;
1052#else
1053 using clock = std::chrono::steady_clock;
1054#endif
1055
1056 // Take a timestamp
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001057 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001058
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001059 return static_cast<uint64_t>(timestamp.count());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001060}
Keith Davis3201eea2019-10-24 17:30:41 +01001061
Jim Flynn4e755a52020-03-29 17:48:26 +01001062Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1063{
1064 if (buffer == nullptr)
1065 {
1066 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1067 }
1068 if (length < 8)
1069 {
1070 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1071 }
1072
1073 uint32_t metadataIdentifier = 0;
1074 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1075
1076 uint32_t dataLength = 0;
1077 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1078
1079 std::unique_ptr<unsigned char[]> packetData;
1080 if (dataLength > 0)
1081 {
1082 packetData = std::make_unique<unsigned char[]>(dataLength);
1083 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1084 }
1085
1086 return Packet(metadataIdentifier, dataLength, packetData);
1087}
1088
Ferran Balaguer73882172019-09-02 16:39:42 +01001089} // namespace profiling
1090
Matteo Martincigh149528e2019-09-05 12:02:04 +01001091} // namespace armnn
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001092
1093namespace std
1094{
1095
1096bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
1097{
1098 return std::memcmp(left.data(), &right, left.size()) == 0;
1099}
1100
1101} // namespace std