blob: d86adbc051c634cc668390108576206a6bbab1ee [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Ferran Balaguer73882172019-09-02 16:39:42 +01003// 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 )|
Jim Flynn83d08a92020-07-09 13:48:16 +0100102 (( packetId & 0x000003FF ) << 16 );
103}
104
105uint32_t ConstructHeader(uint32_t packetFamily, uint32_t packetClass, uint32_t packetType)
106{
107 return ((packetFamily & 0x0000003F) << 26) |
108 ((packetClass & 0x0000007F) << 19) |
109 ((packetType & 0x00000007) << 16);
Keith Davis3201eea2019-10-24 17:30:41 +0100110}
111
112void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_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 WriteUint64(packetBuffer->GetWritableData(), offset, value);
117}
118
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000119void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_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 WriteUint32(packetBuffer->GetWritableData(), offset, value);
124}
125
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000126void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100127{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100128 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100129
130 WriteUint16(packetBuffer->GetWritableData(), offset, value);
131}
132
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000133void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
134{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100135 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000136
137 WriteUint8(packetBuffer->GetWritableData(), offset, value);
138}
139
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000140void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
141{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100142 ARMNN_ASSERT(buffer);
143 ARMNN_ASSERT(value);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000144
145 for (unsigned int i = 0; i < valueSize; i++, offset++)
146 {
147 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
148 }
149}
150
Francis Murtagh3a161982019-09-04 15:25:02 +0100151void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
152{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100153 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100154
155 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
156 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
157 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
158 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
159 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
160 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
161 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
162 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
163}
164
Ferran Balaguer73882172019-09-02 16:39:42 +0100165void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
166{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100167 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100168
Matteo Martincigh149528e2019-09-05 12:02:04 +0100169 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100170 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
171 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
172 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
173}
174
175void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
176{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100177 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100178
Matteo Martincigh149528e2019-09-05 12:02:04 +0100179 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100180 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
181}
182
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000183void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
184{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100185 ARMNN_ASSERT(buffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000186
187 buffer[offset] = static_cast<unsigned char>(value);
188}
189
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000190void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
191{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100192 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000193
194 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
195}
196
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000197uint64_t ReadUint64(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 ReadUint64(packetBuffer->GetReadableData(), offset);
202}
203
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000204uint32_t ReadUint32(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 ReadUint32(packetBuffer->GetReadableData(), offset);
209}
210
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000211uint16_t ReadUint16(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 ReadUint16(packetBuffer->GetReadableData(), offset);
216}
217
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000218uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100219{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100220 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100221
222 return ReadUint8(packetBuffer->GetReadableData(), offset);
223}
224
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000225void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
226{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100227 ARMNN_ASSERT(buffer);
228 ARMNN_ASSERT(outValue);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000229
230 for (unsigned int i = 0; i < valueSize; i++, offset++)
231 {
232 outValue[i] = static_cast<uint8_t>(buffer[offset]);
233 }
234}
235
Francis Murtagh3a161982019-09-04 15:25:02 +0100236uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
237{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100238 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100239
240 uint64_t value = 0;
Matteo Martincighab173e92019-09-05 12:02:04 +0100241 value = static_cast<uint64_t>(buffer[offset]);
Francis Murtagh3a161982019-09-04 15:25:02 +0100242 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
243 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
244 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
245 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
246 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
247 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
248 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
249
250 return value;
251}
252
Ferran Balaguer73882172019-09-02 16:39:42 +0100253uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
254{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100255 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100256
257 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100258 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100259 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
260 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
261 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
262 return value;
263}
264
265uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
266{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100267 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100268
269 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100270 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100271 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
272 return static_cast<uint16_t>(value);
273}
274
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100275uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
276{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100277 ARMNN_ASSERT(buffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100278
279 return buffer[offset];
280}
281
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100282std::string GetSoftwareInfo()
283{
284 return std::string("ArmNN");
285}
286
287std::string GetHardwareVersion()
288{
289 return std::string();
290}
291
292std::string GetSoftwareVersion()
293{
Francis Murtaghd36c5282020-05-22 12:49:25 +0100294 std::string result = "Armnn " + std::to_string(ARMNN_MAJOR_VERSION) + "." + std::to_string(ARMNN_MINOR_VERSION);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100295 return result;
296}
297
298std::string GetProcessName()
299{
300 std::ifstream comm("/proc/self/comm");
301 std::string name;
302 getline(comm, name);
303 return name;
304}
305
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000306// Calculate the actual length an SwString will be including the terminating null character
307// padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
308// the size to allow the offset to be correctly updated when decoding a binary packet.
309uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
310{
311 std::vector<uint32_t> swTraceString;
312 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
313 unsigned int uint32_t_size = sizeof(uint32_t);
314 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
315 return size;
316}
317
318// Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
319SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
320{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100321 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000322
323 unsigned int uint32_t_size = sizeof(uint32_t);
324
325 SwTraceMessage swTraceMessage;
326
327 // Read the decl_id
328 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
329 swTraceMessage.m_Id = readDeclId;
330
331 // SWTrace "namestring" format
332 // length of the string (first 4 bytes) + string + null terminator
333
334 // Check the decl_name
335 offset += uint32_t_size;
336 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
337
338 offset += uint32_t_size;
339 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
340 std::memcpy(swTraceStringBuffer.data(),
341 packetBuffer + offset, swTraceStringBuffer.size());
342
343 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
344
345 // Check the ui_name
346 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
347 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
348
349 offset += uint32_t_size;
350 swTraceStringBuffer.resize(swTraceUINameLength - 1);
351 std::memcpy(swTraceStringBuffer.data(),
352 packetBuffer + offset, swTraceStringBuffer.size());
353
354 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
355
356 // Check arg_types
357 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
358 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
359
360 offset += uint32_t_size;
361 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
362 std::memcpy(swTraceStringBuffer.data(),
363 packetBuffer + offset, swTraceStringBuffer.size());
364
365 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
366
367 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
368
369 // Check arg_names
370 offset += CalculateSizeOfPaddedSwString(swTraceString);
371 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
372
373 offset += uint32_t_size;
374 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
375 std::memcpy(swTraceStringBuffer.data(),
376 packetBuffer + offset, swTraceStringBuffer.size());
377
378 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
379 std::stringstream stringStream(swTraceString);
380 std::string argName;
381 while (std::getline(stringStream, argName, ','))
382 {
383 swTraceMessage.m_ArgNames.push_back(argName);
384 }
385
386 offset += CalculateSizeOfPaddedSwString(swTraceString);
387
388 return swTraceMessage;
389}
390
Jan Eilers92fa15b2019-10-15 15:23:25 +0100391/// Creates a timeline packet header
392///
393/// \params
394/// packetFamiliy Timeline Packet Family
395/// packetClass Timeline Packet Class
396/// packetType Timeline Packet Type
397/// streamId Stream identifier
398/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100399/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
Jan Eilers92fa15b2019-10-15 15:23:25 +0100400///
401/// \returns
402/// Pair of uint32_t containing word0 and word1 of the header
403std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
404 uint32_t packetClass,
405 uint32_t packetType,
406 uint32_t streamId,
407 uint32_t sequenceNumbered,
408 uint32_t dataLength)
409{
410 // Packet header word 0:
411 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
412 // 19:25 [7] packet_class: packet class
413 // 16:18 [3] packet_type: packet type
414 // 8:15 [8] reserved: all zeros
415 // 0:7 [8] stream_id: stream identifier
416 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
417 ((packetClass & 0x0000007F) << 19) |
418 ((packetType & 0x00000007) << 16) |
419 ((streamId & 0x00000007) << 0);
420
421 // Packet header word 1:
422 // 25:31 [7] reserved: all zeros
423 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
424 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
425 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
426 ((dataLength & 0x00FFFFFF) << 0);
427
428 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
429}
430
431/// Creates a packet header for the timeline messages:
432/// * declareLabel
433/// * declareEntity
434/// * declareEventClass
435/// * declareRelationship
436/// * declareEvent
437///
438/// \param
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100439/// dataLength The length of the message body in bytes
Jan Eilers92fa15b2019-10-15 15:23:25 +0100440///
441/// \returns
442/// Pair of uint32_t containing word0 and word1 of the header
443std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
444{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100445 return CreateTimelinePacketHeader(1, // Packet family
446 0, // Packet class
447 1, // Packet type
448 0, // Stream id
449 0, // Sequence number
450 dataLength); // Data length
Jan Eilers92fa15b2019-10-15 15:23:25 +0100451}
452
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100453TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
454 const std::string& label,
455 unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000456 unsigned int remainingBufferSize,
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100457 unsigned int& numberOfBytesWritten)
458{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100459 // Initialize the output value
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100460 numberOfBytesWritten = 0;
461
462 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000463 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100464 {
465 return TimelinePacketStatus::BufferExhaustion;
466 }
467
468 // Utils
469 unsigned int uint32_t_size = sizeof(uint32_t);
470 unsigned int uint64_t_size = sizeof(uint64_t);
471
472 // Convert the label into a SWTrace string
473 std::vector<uint32_t> swTraceLabel;
474 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
475 if (!result)
476 {
477 return TimelinePacketStatus::Error;
478 }
479
480 // Calculate the size of the SWTrace string label (in bytes)
481 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
482
483 // Calculate the length of the data (in bytes)
Jan Eilersb884ea42019-10-16 09:54:15 +0100484 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
485 uint64_t_size + // Profiling GUID
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100486 swTraceLabelSize; // Label
487
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100488 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000489 if (timelineLabelPacketDataLength > remainingBufferSize)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100490 {
491 return TimelinePacketStatus::BufferExhaustion;
492 }
493
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100494 // Initialize the offset for writing in the buffer
495 unsigned int offset = 0;
496
Jan Eilersb884ea42019-10-16 09:54:15 +0100497 // Write decl_Id to the buffer
498 WriteUint32(buffer, offset, 0u);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100499 offset += uint32_t_size;
500
501 // Write the timeline binary packet payload to the buffer
502 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
503 offset += uint64_t_size;
504 for (uint32_t swTraceLabelWord : swTraceLabel)
505 {
506 WriteUint32(buffer, offset, swTraceLabelWord); // Label
507 offset += uint32_t_size;
508 }
509
510 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000511 numberOfBytesWritten = timelineLabelPacketDataLength;
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100512
513 return TimelinePacketStatus::Ok;
514}
515
Keith Davis97da5e22020-03-05 16:25:28 +0000516TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
517 unsigned char* buffer,
518 unsigned int remainingBufferSize,
519 unsigned int& numberOfBytesWritten)
David Monahanf21f6062019-10-07 15:11:15 +0100520{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100521 // Initialize the output value
David Monahanf21f6062019-10-07 15:11:15 +0100522 numberOfBytesWritten = 0;
523
524 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000525 if (buffer == nullptr || remainingBufferSize == 0)
David Monahanf21f6062019-10-07 15:11:15 +0100526 {
527 return TimelinePacketStatus::BufferExhaustion;
528 }
529
530 // Utils
531 unsigned int uint32_t_size = sizeof(uint32_t);
532 unsigned int uint64_t_size = sizeof(uint64_t);
533
534 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000535 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
David Monahanf21f6062019-10-07 15:11:15 +0100536
537 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000538 if (timelineEntityDataLength > remainingBufferSize)
David Monahanf21f6062019-10-07 15:11:15 +0100539 {
540 return TimelinePacketStatus::BufferExhaustion;
541 }
542
David Monahanf21f6062019-10-07 15:11:15 +0100543 // Initialize the offset for writing in the buffer
544 unsigned int offset = 0;
545
Jan Eilersb884ea42019-10-16 09:54:15 +0100546 // Write the decl_Id to the buffer
547 WriteUint32(buffer, offset, 1u);
David Monahanf21f6062019-10-07 15:11:15 +0100548 offset += uint32_t_size;
549
550 // Write the timeline binary packet payload to the buffer
551 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
552
553 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000554 numberOfBytesWritten = timelineEntityDataLength;
David Monahanf21f6062019-10-07 15:11:15 +0100555
556 return TimelinePacketStatus::Ok;
557}
558
Keith Davis97da5e22020-03-05 16:25:28 +0000559TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
560 uint64_t relationshipGuid,
561 uint64_t headGuid,
562 uint64_t tailGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +0100563 uint64_t attributeGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000564 unsigned char* buffer,
565 unsigned int remainingBufferSize,
566 unsigned int& numberOfBytesWritten)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100567{
568 // Initialize the output value
569 numberOfBytesWritten = 0;
570
571 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000572 if (buffer == nullptr || remainingBufferSize == 0)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100573 {
574 return TimelinePacketStatus::BufferExhaustion;
575 }
576
577 // Utils
578 unsigned int uint32_t_size = sizeof(uint32_t);
579 unsigned int uint64_t_size = sizeof(uint64_t);
580
581 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000582 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
Finn Williams0a336dc2020-05-11 15:39:58 +0100583 uint64_t_size * 4; // Relationship GUID + Head GUID +
584 // tail GUID + attributeGuid
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100585
Keith Davis97da5e22020-03-05 16:25:28 +0000586 // Check whether the timeline binary fits in the given buffer
587 if (timelineRelationshipDataLength > remainingBufferSize)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100588 {
589 return TimelinePacketStatus::BufferExhaustion;
590 }
591
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100592 // Initialize the offset for writing in the buffer
593 unsigned int offset = 0;
594
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100595 uint32_t relationshipTypeUint = 0;
596
597 switch (relationshipType)
598 {
599 case ProfilingRelationshipType::RetentionLink:
600 relationshipTypeUint = 0;
601 break;
602 case ProfilingRelationshipType::ExecutionLink:
603 relationshipTypeUint = 1;
604 break;
605 case ProfilingRelationshipType::DataLink:
606 relationshipTypeUint = 2;
607 break;
608 case ProfilingRelationshipType::LabelLink:
609 relationshipTypeUint = 3;
610 break;
611 default:
612 throw InvalidArgumentException("Unknown relationship type given.");
613 }
614
Keith Davis97da5e22020-03-05 16:25:28 +0000615 // Write the timeline binary payload to the buffer
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100616 // decl_id of the timeline message
617 uint32_t declId = 3;
618 WriteUint32(buffer, offset, declId); // decl_id
619 offset += uint32_t_size;
620 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
621 offset += uint32_t_size;
622 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
623 offset += uint64_t_size;
624 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
625 offset += uint64_t_size;
626 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
Finn Williams0a336dc2020-05-11 15:39:58 +0100627 offset += uint64_t_size;
628 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
629
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100630
631 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000632 numberOfBytesWritten = timelineRelationshipDataLength;
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100633
634 return TimelinePacketStatus::Ok;
635}
636
Sadik Armagan784db772019-10-08 15:05:38 +0100637TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000638 unsigned int remainingBufferSize,
Sadik Armagan784db772019-10-08 15:05:38 +0100639 unsigned int& numberOfBytesWritten)
640{
641 // Initialize the output value
642 numberOfBytesWritten = 0;
643
644 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000645 if (buffer == nullptr || remainingBufferSize == 0)
Sadik Armagan784db772019-10-08 15:05:38 +0100646 {
647 return TimelinePacketStatus::BufferExhaustion;
648 }
649
650 // Utils
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000651 unsigned int uint8_t_size = sizeof(uint8_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100652 unsigned int uint32_t_size = sizeof(uint32_t);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000653 unsigned int uint64_t_size = sizeof(uint64_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100654
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100655 // The payload/data of the packet consists of swtrace event definitions encoded according
Sadik Armagan784db772019-10-08 15:05:38 +0100656 // to the swtrace directory specification. The messages being the five defined below:
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000657 //
658 // | decl_id | decl_name | ui_name | arg_types | arg_names |
Sadik Armagan784db772019-10-08 15:05:38 +0100659 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
660 // | 0 | declareLabel | declare label | ps | guid,value |
661 // | 1 | declareEntity | declare entity | p | guid |
Jim Flynn6398a982020-05-27 17:05:21 +0100662 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
663 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
664 // | | | | | headGuid,tailGuid,attributeGuid |
Sadik Armagan784db772019-10-08 15:05:38 +0100665 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100666 std::vector<std::vector<std::string>> timelineDirectoryMessages
667 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000668 { "0", "declareLabel", "declare label", "ps", "guid,value" },
669 { "1", "declareEntity", "declare entity", "p", "guid" },
Jim Flynn6398a982020-05-27 17:05:21 +0100670 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
671 { "3", "declareRelationship", "declare relationship", "Ipppp",
672 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000673 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100674 };
Sadik Armagan784db772019-10-08 15:05:38 +0100675
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000676 // Build the message declarations
677 std::vector<uint32_t> swTraceBuffer;
678 for (const auto& directoryComponent : timelineDirectoryMessages)
Sadik Armagan784db772019-10-08 15:05:38 +0100679 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000680 // decl_id
681 uint32_t declId = 0;
682 try
Sadik Armagan784db772019-10-08 15:05:38 +0100683 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000684 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
Sadik Armagan784db772019-10-08 15:05:38 +0100685 }
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000686 catch (const std::exception&)
687 {
688 return TimelinePacketStatus::Error;
689 }
690 swTraceBuffer.push_back(declId);
691
692 bool result = true;
693 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
694 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
695 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
696 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
697 if (!result)
698 {
699 return TimelinePacketStatus::Error;
700 }
Sadik Armagan784db772019-10-08 15:05:38 +0100701 }
702
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000703 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
704 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
705 uint32_t_size; // Trace directory (5 messages)
706
Sadik Armagan784db772019-10-08 15:05:38 +0100707 // Calculate the timeline directory binary packet size (in bytes)
708 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000709 dataLength; // Payload
Sadik Armagan784db772019-10-08 15:05:38 +0100710
711 // Check whether the timeline directory binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000712 if (timelineDirectoryPacketSize > remainingBufferSize)
Sadik Armagan784db772019-10-08 15:05:38 +0100713 {
714 return TimelinePacketStatus::BufferExhaustion;
715 }
716
Jan Eilersb884ea42019-10-16 09:54:15 +0100717 // Create packet header
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000718 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
Sadik Armagan784db772019-10-08 15:05:38 +0100719
720 // Initialize the offset for writing in the buffer
721 unsigned int offset = 0;
722
723 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100724 WriteUint32(buffer, offset, packetHeader.first);
Sadik Armagan784db772019-10-08 15:05:38 +0100725 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100726 WriteUint32(buffer, offset, packetHeader.second);
Sadik Armagan784db772019-10-08 15:05:38 +0100727 offset += uint32_t_size;
728
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000729 // Write the stream header
730 uint8_t streamVersion = 4;
731 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100732 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000733 switch (threadIdBytes)
Sadik Armagan784db772019-10-08 15:05:38 +0100734 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000735 case 4: // Typically Windows and Android
736 case 8: // Typically Linux
737 break; // Valid values
738 default:
739 return TimelinePacketStatus::Error; // Invalid value
740 }
741 WriteUint8(buffer, offset, streamVersion);
742 offset += uint8_t_size;
743 WriteUint8(buffer, offset, pointerBytes);
744 offset += uint8_t_size;
745 WriteUint8(buffer, offset, threadIdBytes);
746 offset += uint8_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100747
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000748 // Write the SWTrace directory
749 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
750 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
751 offset += uint32_t_size;
752 for (uint32_t i : swTraceBuffer)
753 {
754 WriteUint32(buffer, offset, i); // Message declarations
755 offset += uint32_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100756 }
757
758 // Update the number of bytes written
759 numberOfBytesWritten = timelineDirectoryPacketSize;
760
761 return TimelinePacketStatus::Ok;
762}
763
Keith Davis97da5e22020-03-05 16:25:28 +0000764TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
Jim Flynn1892d212020-05-26 21:10:49 +0100765 uint64_t nameGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000766 unsigned char* buffer,
767 unsigned int remainingBufferSize,
768 unsigned int& numberOfBytesWritten)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100769{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100770 // Initialize the output value
Jan Eilers92fa15b2019-10-15 15:23:25 +0100771 numberOfBytesWritten = 0;
772
773 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000774 if (buffer == nullptr || remainingBufferSize == 0)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100775 {
776 return TimelinePacketStatus::BufferExhaustion;
777 }
778
779 // Utils
780 unsigned int uint32_t_size = sizeof(uint32_t);
781 unsigned int uint64_t_size = sizeof(uint64_t);
782
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100783 // decl_id of the timeline message
784 uint32_t declId = 2;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100785
786 // Calculate the length of the data (in bytes)
Jim Flynn1892d212020-05-26 21:10:49 +0100787 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100788
Keith Davis97da5e22020-03-05 16:25:28 +0000789 // Check whether the timeline binary fits in the given buffer
790 if (dataSize > remainingBufferSize)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100791 {
792 return TimelinePacketStatus::BufferExhaustion;
793 }
794
Jan Eilers92fa15b2019-10-15 15:23:25 +0100795 // Initialize the offset for writing in the buffer
796 unsigned int offset = 0;
797
Keith Davis97da5e22020-03-05 16:25:28 +0000798 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100799 WriteUint32(buffer, offset, declId); // decl_id
Jan Eilers92fa15b2019-10-15 15:23:25 +0100800 offset += uint32_t_size;
801 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
Jim Flynn1892d212020-05-26 21:10:49 +0100802 offset += uint64_t_size;
803 WriteUint64(buffer, offset, nameGuid); // Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100804
805 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000806 numberOfBytesWritten = dataSize;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100807
808 return TimelinePacketStatus::Ok;
809}
810
Keith Davis97da5e22020-03-05 16:25:28 +0000811TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
Jim Flynn1fdeb992020-07-09 07:28:37 +0100812 int threadId,
Keith Davis97da5e22020-03-05 16:25:28 +0000813 uint64_t profilingGuid,
814 unsigned char* buffer,
815 unsigned int remainingBufferSize,
816 unsigned int& numberOfBytesWritten)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100817{
818 // Initialize the output value
819 numberOfBytesWritten = 0;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100820 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000821 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100822 {
823 return TimelinePacketStatus::BufferExhaustion;
824 }
825
826 // Utils
827 unsigned int uint32_t_size = sizeof(uint32_t);
828 unsigned int uint64_t_size = sizeof(uint64_t);
829
830 // decl_id of the timeline message
831 uint32_t declId = 4;
832
833 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000834 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
835 uint64_t_size + // Timestamp
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100836 ThreadIdSize + // Thread id
Keith Davis97da5e22020-03-05 16:25:28 +0000837 uint64_t_size; // Profiling GUID
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100838
839 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000840 if (timelineEventDataLength > remainingBufferSize)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100841 {
842 return TimelinePacketStatus::BufferExhaustion;
843 }
844
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100845 // Initialize the offset for writing in the buffer
846 unsigned int offset = 0;
847
Keith Davis97da5e22020-03-05 16:25:28 +0000848 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100849 WriteUint32(buffer, offset, declId); // decl_id
850 offset += uint32_t_size;
851 WriteUint64(buffer, offset, timestamp); // Timestamp
852 offset += uint64_t_size;
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100853 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
854 offset += ThreadIdSize;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100855 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
856 offset += uint64_t_size;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100857 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000858 numberOfBytesWritten = timelineEventDataLength;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100859
860 return TimelinePacketStatus::Ok;
861}
862
Keith Davis3201eea2019-10-24 17:30:41 +0100863std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
864{
865 std::stringstream outputStream, centrePadding;
866 int padding = spacingWidth - static_cast<int>(stringToPass.size());
867
868 for (int i = 0; i < padding / 2; ++i)
869 {
870 centrePadding << " ";
871 }
872
873 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
874
875 if (padding > 0 && padding %2 != 0)
876 {
877 outputStream << " ";
878 }
879
880 return outputStream.str();
881}
882
883void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
884{
885 std::string body;
886
887 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
888 body.append(" | ");
889 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
890 body.append(" | ");
891 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
892 body.append("\n");
893
894 std::cout << std::string(body.size(), '-') << "\n";
895 std::cout<< body;
896}
897
898void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
899{
900 std::string body;
901
902 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
903 body.append(" | ");
904 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
905 body.append(" | ");
906 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
907 body.append("\n");
908
909 std::cout << std::string(body.size(), '-') << "\n";
910
911 std::cout<< body;
912}
913
914void PrintCounterDetails(std::shared_ptr<Counter>& counter)
915{
916 std::string body;
917
918 body.append(CentreAlignFormatting(counter->m_Name, 20));
919 body.append(" | ");
920 body.append(CentreAlignFormatting(counter->m_Description, 50));
921 body.append(" | ");
922 body.append(CentreAlignFormatting(counter->m_Units, 14));
923 body.append(" | ");
924 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
925 body.append(" | ");
926 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
927 body.append(" | ");
928 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
929 body.append(" | ");
930 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
931 body.append(" | ");
932 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
933 body.append(" | ");
934 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
935 body.append(" | ");
936 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
937
938 body.append("\n");
939
940 std::cout << std::string(body.size(), '-') << "\n";
941
942 std::cout << body;
943}
944
945void PrintCategoryDetails(const std::unique_ptr<Category>& category,
946 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
947{
948 std::string categoryBody;
949 std::string categoryHeader;
950
951 categoryHeader.append(CentreAlignFormatting("Name", 20));
952 categoryHeader.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100953 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
954 categoryHeader.append("\n");
955
956 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
957 categoryBody.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100958 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
959
960 std::cout << "\n" << "\n";
961 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
962 std::cout << "\n";
963 std::cout << std::string(categoryHeader.size(), '=') << "\n";
964
965 std::cout << categoryHeader;
966
967 std::cout << std::string(categoryBody.size(), '-') << "\n";
968
969 std::cout << categoryBody;
970
971 std::string counterHeader;
972
973 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
974 counterHeader.append(" | ");
975 counterHeader.append(CentreAlignFormatting("Description", 50));
976 counterHeader.append(" | ");
977 counterHeader.append(CentreAlignFormatting("Units", 14));
978 counterHeader.append(" | ");
979 counterHeader.append(CentreAlignFormatting("UID", 6));
980 counterHeader.append(" | ");
981 counterHeader.append(CentreAlignFormatting("Max UID", 10));
982 counterHeader.append(" | ");
983 counterHeader.append(CentreAlignFormatting("Class", 8));
984 counterHeader.append(" | ");
985 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
986 counterHeader.append(" | ");
987 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
988 counterHeader.append(" | ");
989 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
990 counterHeader.append(" | ");
991 counterHeader.append(CentreAlignFormatting("Device UID", 14));
992 counterHeader.append("\n");
993
994 std::cout << "\n" << "\n";
995 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
996 static_cast<int>(counterHeader.size()));
997 std::cout << "\n";
998 std::cout << std::string(counterHeader.size(), '=') << "\n";
999 std::cout << counterHeader;
1000 for (auto& it: category->m_Counters) {
1001 auto search = counterMap.find(it);
1002 if(search != counterMap.end()) {
1003 PrintCounterDetails(search->second);
1004 }
1005 }
1006}
1007
1008void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1009{
1010 std::string devicesHeader;
1011
1012 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1013 devicesHeader.append(" | ");
1014 devicesHeader.append(CentreAlignFormatting("UID", 13));
1015 devicesHeader.append(" | ");
1016 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1017 devicesHeader.append("\n");
1018
1019 std::cout << "\n" << "\n";
1020 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1021 std::cout << "\n";
1022 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1023 std::cout << devicesHeader;
1024 for (auto& it: counterDirectory.GetDevices()) {
1025 PrintDeviceDetails(it);
1026 }
1027
1028 std::string counterSetHeader;
1029
1030 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1031 counterSetHeader.append(" | ");
1032 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1033 counterSetHeader.append(" | ");
1034 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1035 counterSetHeader.append("\n");
1036
1037 std::cout << "\n" << "\n";
1038 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1039 std::cout << "\n";
1040 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1041
1042 std::cout << counterSetHeader;
1043
1044 for (auto& it: counterDirectory.GetCounterSets()) {
1045 PrintCounterSetDetails(it);
1046 }
1047
1048 auto counters = counterDirectory.GetCounters();
1049 for (auto& it: counterDirectory.GetCategories()) {
1050 PrintCategoryDetails(it, counters);
1051 }
1052 std::cout << "\n";
1053}
1054
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001055uint64_t GetTimestamp()
1056{
1057#if USE_CLOCK_MONOTONIC_RAW
1058 using clock = MonotonicClockRaw;
1059#else
1060 using clock = std::chrono::steady_clock;
1061#endif
1062
1063 // Take a timestamp
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001064 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001065
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001066 return static_cast<uint64_t>(timestamp.count());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001067}
Keith Davis3201eea2019-10-24 17:30:41 +01001068
Jim Flynn4e755a52020-03-29 17:48:26 +01001069Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1070{
1071 if (buffer == nullptr)
1072 {
1073 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1074 }
1075 if (length < 8)
1076 {
1077 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1078 }
1079
1080 uint32_t metadataIdentifier = 0;
1081 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1082
1083 uint32_t dataLength = 0;
1084 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1085
1086 std::unique_ptr<unsigned char[]> packetData;
1087 if (dataLength > 0)
1088 {
1089 packetData = std::make_unique<unsigned char[]>(dataLength);
1090 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1091 }
1092
1093 return Packet(metadataIdentifier, dataLength, packetData);
1094}
1095
Ferran Balaguer73882172019-09-02 16:39:42 +01001096} // namespace profiling
1097
Matteo Martincigh149528e2019-09-05 12:02:04 +01001098} // namespace armnn
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001099
1100namespace std
1101{
1102
Jim Flynn1fdeb992020-07-09 07:28:37 +01001103bool operator==(const std::vector<uint8_t>& left, int right)
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001104{
1105 return std::memcmp(left.data(), &right, left.size()) == 0;
1106}
1107
1108} // namespace std