blob: 8c43a8cd3a928b12d1071820b1ef6f4ea03ed53e [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
Kevin Mayc1351792020-07-28 11:29:04 +0100319SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer,
320 unsigned int& offset,
321 const unsigned int& packetLength)
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000322{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100323 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000324
325 unsigned int uint32_t_size = sizeof(uint32_t);
326
327 SwTraceMessage swTraceMessage;
328
329 // Read the decl_id
330 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
331 swTraceMessage.m_Id = readDeclId;
332
333 // SWTrace "namestring" format
334 // length of the string (first 4 bytes) + string + null terminator
335
336 // Check the decl_name
337 offset += uint32_t_size;
338 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
339
Kevin Mayc1351792020-07-28 11:29:04 +0100340 if (swTraceDeclNameLength == 0 || swTraceDeclNameLength > packetLength)
341 {
342 throw RuntimeException("Error swTraceDeclNameLength is an invalid size", CHECK_LOCATION());
343 }
344
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000345 offset += uint32_t_size;
346 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
347 std::memcpy(swTraceStringBuffer.data(),
348 packetBuffer + offset, swTraceStringBuffer.size());
349
350 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
351
352 // Check the ui_name
353 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
354 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
355
Kevin Mayc1351792020-07-28 11:29:04 +0100356 if (swTraceUINameLength == 0 || swTraceUINameLength > packetLength)
357 {
358 throw RuntimeException("Error swTraceUINameLength is an invalid size", CHECK_LOCATION());
359 }
360
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000361 offset += uint32_t_size;
362 swTraceStringBuffer.resize(swTraceUINameLength - 1);
363 std::memcpy(swTraceStringBuffer.data(),
364 packetBuffer + offset, swTraceStringBuffer.size());
365
366 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
367
368 // Check arg_types
369 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
370 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
371
Kevin Mayc1351792020-07-28 11:29:04 +0100372 if (swTraceArgTypesLength == 0 || swTraceArgTypesLength > packetLength)
373 {
374 throw RuntimeException("Error swTraceArgTypesLength is an invalid size", CHECK_LOCATION());
375 }
376
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000377 offset += uint32_t_size;
378 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
379 std::memcpy(swTraceStringBuffer.data(),
380 packetBuffer + offset, swTraceStringBuffer.size());
381
382 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
383
384 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
385
386 // Check arg_names
387 offset += CalculateSizeOfPaddedSwString(swTraceString);
388 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
389
Kevin Mayc1351792020-07-28 11:29:04 +0100390 if (swTraceArgNamesLength == 0 || swTraceArgNamesLength > packetLength)
391 {
392 throw RuntimeException("Error swTraceArgNamesLength is an invalid size", CHECK_LOCATION());
393 }
394
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000395 offset += uint32_t_size;
396 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
397 std::memcpy(swTraceStringBuffer.data(),
398 packetBuffer + offset, swTraceStringBuffer.size());
399
400 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
401 std::stringstream stringStream(swTraceString);
402 std::string argName;
403 while (std::getline(stringStream, argName, ','))
404 {
405 swTraceMessage.m_ArgNames.push_back(argName);
406 }
407
408 offset += CalculateSizeOfPaddedSwString(swTraceString);
409
410 return swTraceMessage;
411}
412
Jan Eilers92fa15b2019-10-15 15:23:25 +0100413/// Creates a timeline packet header
414///
415/// \params
416/// packetFamiliy Timeline Packet Family
417/// packetClass Timeline Packet Class
418/// packetType Timeline Packet Type
419/// streamId Stream identifier
420/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100421/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
Jan Eilers92fa15b2019-10-15 15:23:25 +0100422///
423/// \returns
424/// Pair of uint32_t containing word0 and word1 of the header
425std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
426 uint32_t packetClass,
427 uint32_t packetType,
428 uint32_t streamId,
429 uint32_t sequenceNumbered,
430 uint32_t dataLength)
431{
432 // Packet header word 0:
433 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
434 // 19:25 [7] packet_class: packet class
435 // 16:18 [3] packet_type: packet type
436 // 8:15 [8] reserved: all zeros
437 // 0:7 [8] stream_id: stream identifier
438 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
439 ((packetClass & 0x0000007F) << 19) |
440 ((packetType & 0x00000007) << 16) |
441 ((streamId & 0x00000007) << 0);
442
443 // Packet header word 1:
444 // 25:31 [7] reserved: all zeros
445 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
446 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
447 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
448 ((dataLength & 0x00FFFFFF) << 0);
449
450 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
451}
452
453/// Creates a packet header for the timeline messages:
454/// * declareLabel
455/// * declareEntity
456/// * declareEventClass
457/// * declareRelationship
458/// * declareEvent
459///
460/// \param
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100461/// dataLength The length of the message body in bytes
Jan Eilers92fa15b2019-10-15 15:23:25 +0100462///
463/// \returns
464/// Pair of uint32_t containing word0 and word1 of the header
465std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
466{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100467 return CreateTimelinePacketHeader(1, // Packet family
468 0, // Packet class
469 1, // Packet type
470 0, // Stream id
471 0, // Sequence number
472 dataLength); // Data length
Jan Eilers92fa15b2019-10-15 15:23:25 +0100473}
474
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100475TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
476 const std::string& label,
477 unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000478 unsigned int remainingBufferSize,
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100479 unsigned int& numberOfBytesWritten)
480{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100481 // Initialize the output value
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100482 numberOfBytesWritten = 0;
483
484 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000485 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100486 {
487 return TimelinePacketStatus::BufferExhaustion;
488 }
489
490 // Utils
491 unsigned int uint32_t_size = sizeof(uint32_t);
492 unsigned int uint64_t_size = sizeof(uint64_t);
493
494 // Convert the label into a SWTrace string
495 std::vector<uint32_t> swTraceLabel;
496 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
497 if (!result)
498 {
499 return TimelinePacketStatus::Error;
500 }
501
502 // Calculate the size of the SWTrace string label (in bytes)
503 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
504
505 // Calculate the length of the data (in bytes)
Jan Eilersb884ea42019-10-16 09:54:15 +0100506 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
507 uint64_t_size + // Profiling GUID
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100508 swTraceLabelSize; // Label
509
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100510 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000511 if (timelineLabelPacketDataLength > remainingBufferSize)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100512 {
513 return TimelinePacketStatus::BufferExhaustion;
514 }
515
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100516 // Initialize the offset for writing in the buffer
517 unsigned int offset = 0;
518
Jan Eilersb884ea42019-10-16 09:54:15 +0100519 // Write decl_Id to the buffer
520 WriteUint32(buffer, offset, 0u);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100521 offset += uint32_t_size;
522
523 // Write the timeline binary packet payload to the buffer
524 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
525 offset += uint64_t_size;
526 for (uint32_t swTraceLabelWord : swTraceLabel)
527 {
528 WriteUint32(buffer, offset, swTraceLabelWord); // Label
529 offset += uint32_t_size;
530 }
531
532 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000533 numberOfBytesWritten = timelineLabelPacketDataLength;
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100534
535 return TimelinePacketStatus::Ok;
536}
537
Keith Davis97da5e22020-03-05 16:25:28 +0000538TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
539 unsigned char* buffer,
540 unsigned int remainingBufferSize,
541 unsigned int& numberOfBytesWritten)
David Monahanf21f6062019-10-07 15:11:15 +0100542{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100543 // Initialize the output value
David Monahanf21f6062019-10-07 15:11:15 +0100544 numberOfBytesWritten = 0;
545
546 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000547 if (buffer == nullptr || remainingBufferSize == 0)
David Monahanf21f6062019-10-07 15:11:15 +0100548 {
549 return TimelinePacketStatus::BufferExhaustion;
550 }
551
552 // Utils
553 unsigned int uint32_t_size = sizeof(uint32_t);
554 unsigned int uint64_t_size = sizeof(uint64_t);
555
556 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000557 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
David Monahanf21f6062019-10-07 15:11:15 +0100558
559 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000560 if (timelineEntityDataLength > remainingBufferSize)
David Monahanf21f6062019-10-07 15:11:15 +0100561 {
562 return TimelinePacketStatus::BufferExhaustion;
563 }
564
David Monahanf21f6062019-10-07 15:11:15 +0100565 // Initialize the offset for writing in the buffer
566 unsigned int offset = 0;
567
Jan Eilersb884ea42019-10-16 09:54:15 +0100568 // Write the decl_Id to the buffer
569 WriteUint32(buffer, offset, 1u);
David Monahanf21f6062019-10-07 15:11:15 +0100570 offset += uint32_t_size;
571
572 // Write the timeline binary packet payload to the buffer
573 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
574
575 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000576 numberOfBytesWritten = timelineEntityDataLength;
David Monahanf21f6062019-10-07 15:11:15 +0100577
578 return TimelinePacketStatus::Ok;
579}
580
Keith Davis97da5e22020-03-05 16:25:28 +0000581TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
582 uint64_t relationshipGuid,
583 uint64_t headGuid,
584 uint64_t tailGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +0100585 uint64_t attributeGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000586 unsigned char* buffer,
587 unsigned int remainingBufferSize,
588 unsigned int& numberOfBytesWritten)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100589{
590 // Initialize the output value
591 numberOfBytesWritten = 0;
592
593 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000594 if (buffer == nullptr || remainingBufferSize == 0)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100595 {
596 return TimelinePacketStatus::BufferExhaustion;
597 }
598
599 // Utils
600 unsigned int uint32_t_size = sizeof(uint32_t);
601 unsigned int uint64_t_size = sizeof(uint64_t);
602
603 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000604 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
Finn Williams0a336dc2020-05-11 15:39:58 +0100605 uint64_t_size * 4; // Relationship GUID + Head GUID +
606 // tail GUID + attributeGuid
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100607
Keith Davis97da5e22020-03-05 16:25:28 +0000608 // Check whether the timeline binary fits in the given buffer
609 if (timelineRelationshipDataLength > remainingBufferSize)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100610 {
611 return TimelinePacketStatus::BufferExhaustion;
612 }
613
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100614 // Initialize the offset for writing in the buffer
615 unsigned int offset = 0;
616
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100617 uint32_t relationshipTypeUint = 0;
618
619 switch (relationshipType)
620 {
621 case ProfilingRelationshipType::RetentionLink:
622 relationshipTypeUint = 0;
623 break;
624 case ProfilingRelationshipType::ExecutionLink:
625 relationshipTypeUint = 1;
626 break;
627 case ProfilingRelationshipType::DataLink:
628 relationshipTypeUint = 2;
629 break;
630 case ProfilingRelationshipType::LabelLink:
631 relationshipTypeUint = 3;
632 break;
633 default:
634 throw InvalidArgumentException("Unknown relationship type given.");
635 }
636
Keith Davis97da5e22020-03-05 16:25:28 +0000637 // Write the timeline binary payload to the buffer
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100638 // decl_id of the timeline message
639 uint32_t declId = 3;
640 WriteUint32(buffer, offset, declId); // decl_id
641 offset += uint32_t_size;
642 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
643 offset += uint32_t_size;
644 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
645 offset += uint64_t_size;
646 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
647 offset += uint64_t_size;
648 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
Finn Williams0a336dc2020-05-11 15:39:58 +0100649 offset += uint64_t_size;
650 WriteUint64(buffer, offset, attributeGuid); // attribute of relationship GUID
651
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100652
653 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000654 numberOfBytesWritten = timelineRelationshipDataLength;
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100655
656 return TimelinePacketStatus::Ok;
657}
658
Sadik Armagan784db772019-10-08 15:05:38 +0100659TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000660 unsigned int remainingBufferSize,
Sadik Armagan784db772019-10-08 15:05:38 +0100661 unsigned int& numberOfBytesWritten)
662{
663 // Initialize the output value
664 numberOfBytesWritten = 0;
665
666 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000667 if (buffer == nullptr || remainingBufferSize == 0)
Sadik Armagan784db772019-10-08 15:05:38 +0100668 {
669 return TimelinePacketStatus::BufferExhaustion;
670 }
671
672 // Utils
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000673 unsigned int uint8_t_size = sizeof(uint8_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100674 unsigned int uint32_t_size = sizeof(uint32_t);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000675 unsigned int uint64_t_size = sizeof(uint64_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100676
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100677 // The payload/data of the packet consists of swtrace event definitions encoded according
Sadik Armagan784db772019-10-08 15:05:38 +0100678 // to the swtrace directory specification. The messages being the five defined below:
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000679 //
680 // | decl_id | decl_name | ui_name | arg_types | arg_names |
Sadik Armagan784db772019-10-08 15:05:38 +0100681 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
682 // | 0 | declareLabel | declare label | ps | guid,value |
683 // | 1 | declareEntity | declare entity | p | guid |
Jim Flynn6398a982020-05-27 17:05:21 +0100684 // | 2 | declareEventClass | declare event class | pp | guid,nameGuid |
685 // | 3 | declareRelationship | declare relationship | Ipppp | relationshipType,relationshipGuid, |
686 // | | | | | headGuid,tailGuid,attributeGuid |
Sadik Armagan784db772019-10-08 15:05:38 +0100687 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100688 std::vector<std::vector<std::string>> timelineDirectoryMessages
689 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000690 { "0", "declareLabel", "declare label", "ps", "guid,value" },
691 { "1", "declareEntity", "declare entity", "p", "guid" },
Jim Flynn6398a982020-05-27 17:05:21 +0100692 { "2", "declareEventClass", "declare event class", "pp", "guid,nameGuid" },
693 { "3", "declareRelationship", "declare relationship", "Ipppp",
694 "relationshipType,relationshipGuid,headGuid,tailGuid,attributeGuid" },
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000695 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100696 };
Sadik Armagan784db772019-10-08 15:05:38 +0100697
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000698 // Build the message declarations
699 std::vector<uint32_t> swTraceBuffer;
700 for (const auto& directoryComponent : timelineDirectoryMessages)
Sadik Armagan784db772019-10-08 15:05:38 +0100701 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000702 // decl_id
703 uint32_t declId = 0;
704 try
Sadik Armagan784db772019-10-08 15:05:38 +0100705 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000706 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
Sadik Armagan784db772019-10-08 15:05:38 +0100707 }
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000708 catch (const std::exception&)
709 {
710 return TimelinePacketStatus::Error;
711 }
712 swTraceBuffer.push_back(declId);
713
714 bool result = true;
715 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
716 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
717 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
718 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
719 if (!result)
720 {
721 return TimelinePacketStatus::Error;
722 }
Sadik Armagan784db772019-10-08 15:05:38 +0100723 }
724
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000725 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
726 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
727 uint32_t_size; // Trace directory (5 messages)
728
Sadik Armagan784db772019-10-08 15:05:38 +0100729 // Calculate the timeline directory binary packet size (in bytes)
730 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000731 dataLength; // Payload
Sadik Armagan784db772019-10-08 15:05:38 +0100732
733 // Check whether the timeline directory binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000734 if (timelineDirectoryPacketSize > remainingBufferSize)
Sadik Armagan784db772019-10-08 15:05:38 +0100735 {
736 return TimelinePacketStatus::BufferExhaustion;
737 }
738
Jan Eilersb884ea42019-10-16 09:54:15 +0100739 // Create packet header
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000740 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
Sadik Armagan784db772019-10-08 15:05:38 +0100741
742 // Initialize the offset for writing in the buffer
743 unsigned int offset = 0;
744
745 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100746 WriteUint32(buffer, offset, packetHeader.first);
Sadik Armagan784db772019-10-08 15:05:38 +0100747 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100748 WriteUint32(buffer, offset, packetHeader.second);
Sadik Armagan784db772019-10-08 15:05:38 +0100749 offset += uint32_t_size;
750
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000751 // Write the stream header
752 uint8_t streamVersion = 4;
753 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100754 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(ThreadIdSize);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000755 switch (threadIdBytes)
Sadik Armagan784db772019-10-08 15:05:38 +0100756 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000757 case 4: // Typically Windows and Android
758 case 8: // Typically Linux
759 break; // Valid values
760 default:
761 return TimelinePacketStatus::Error; // Invalid value
762 }
763 WriteUint8(buffer, offset, streamVersion);
764 offset += uint8_t_size;
765 WriteUint8(buffer, offset, pointerBytes);
766 offset += uint8_t_size;
767 WriteUint8(buffer, offset, threadIdBytes);
768 offset += uint8_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100769
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000770 // Write the SWTrace directory
771 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
772 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
773 offset += uint32_t_size;
774 for (uint32_t i : swTraceBuffer)
775 {
776 WriteUint32(buffer, offset, i); // Message declarations
777 offset += uint32_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100778 }
779
780 // Update the number of bytes written
781 numberOfBytesWritten = timelineDirectoryPacketSize;
782
783 return TimelinePacketStatus::Ok;
784}
785
Keith Davis97da5e22020-03-05 16:25:28 +0000786TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
Jim Flynn1892d212020-05-26 21:10:49 +0100787 uint64_t nameGuid,
Keith Davis97da5e22020-03-05 16:25:28 +0000788 unsigned char* buffer,
789 unsigned int remainingBufferSize,
790 unsigned int& numberOfBytesWritten)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100791{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100792 // Initialize the output value
Jan Eilers92fa15b2019-10-15 15:23:25 +0100793 numberOfBytesWritten = 0;
794
795 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000796 if (buffer == nullptr || remainingBufferSize == 0)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100797 {
798 return TimelinePacketStatus::BufferExhaustion;
799 }
800
801 // Utils
802 unsigned int uint32_t_size = sizeof(uint32_t);
803 unsigned int uint64_t_size = sizeof(uint64_t);
804
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100805 // decl_id of the timeline message
806 uint32_t declId = 2;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100807
808 // Calculate the length of the data (in bytes)
Jim Flynn1892d212020-05-26 21:10:49 +0100809 unsigned int dataSize = uint32_t_size + (uint64_t_size * 2); // decl_id + Profiling GUID + Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100810
Keith Davis97da5e22020-03-05 16:25:28 +0000811 // Check whether the timeline binary fits in the given buffer
812 if (dataSize > remainingBufferSize)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100813 {
814 return TimelinePacketStatus::BufferExhaustion;
815 }
816
Jan Eilers92fa15b2019-10-15 15:23:25 +0100817 // Initialize the offset for writing in the buffer
818 unsigned int offset = 0;
819
Keith Davis97da5e22020-03-05 16:25:28 +0000820 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100821 WriteUint32(buffer, offset, declId); // decl_id
Jan Eilers92fa15b2019-10-15 15:23:25 +0100822 offset += uint32_t_size;
823 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
Jim Flynn1892d212020-05-26 21:10:49 +0100824 offset += uint64_t_size;
825 WriteUint64(buffer, offset, nameGuid); // Name GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100826
827 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000828 numberOfBytesWritten = dataSize;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100829
830 return TimelinePacketStatus::Ok;
831}
832
Keith Davis97da5e22020-03-05 16:25:28 +0000833TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
Jim Flynn1fdeb992020-07-09 07:28:37 +0100834 int threadId,
Keith Davis97da5e22020-03-05 16:25:28 +0000835 uint64_t profilingGuid,
836 unsigned char* buffer,
837 unsigned int remainingBufferSize,
838 unsigned int& numberOfBytesWritten)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100839{
840 // Initialize the output value
841 numberOfBytesWritten = 0;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100842 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000843 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100844 {
845 return TimelinePacketStatus::BufferExhaustion;
846 }
847
848 // Utils
849 unsigned int uint32_t_size = sizeof(uint32_t);
850 unsigned int uint64_t_size = sizeof(uint64_t);
851
852 // decl_id of the timeline message
853 uint32_t declId = 4;
854
855 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000856 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
857 uint64_t_size + // Timestamp
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100858 ThreadIdSize + // Thread id
Keith Davis97da5e22020-03-05 16:25:28 +0000859 uint64_t_size; // Profiling GUID
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100860
861 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000862 if (timelineEventDataLength > remainingBufferSize)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100863 {
864 return TimelinePacketStatus::BufferExhaustion;
865 }
866
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100867 // Initialize the offset for writing in the buffer
868 unsigned int offset = 0;
869
Keith Davis97da5e22020-03-05 16:25:28 +0000870 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100871 WriteUint32(buffer, offset, declId); // decl_id
872 offset += uint32_t_size;
873 WriteUint64(buffer, offset, timestamp); // Timestamp
874 offset += uint64_t_size;
Colm Donelan5bb3d8a2020-05-12 16:36:46 +0100875 WriteBytes(buffer, offset, &threadId, ThreadIdSize); // Thread id
876 offset += ThreadIdSize;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100877 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
878 offset += uint64_t_size;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100879 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000880 numberOfBytesWritten = timelineEventDataLength;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100881
882 return TimelinePacketStatus::Ok;
883}
884
Keith Davis3201eea2019-10-24 17:30:41 +0100885std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
886{
887 std::stringstream outputStream, centrePadding;
888 int padding = spacingWidth - static_cast<int>(stringToPass.size());
889
890 for (int i = 0; i < padding / 2; ++i)
891 {
892 centrePadding << " ";
893 }
894
895 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
896
897 if (padding > 0 && padding %2 != 0)
898 {
899 outputStream << " ";
900 }
901
902 return outputStream.str();
903}
904
905void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
906{
907 std::string body;
908
909 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
910 body.append(" | ");
911 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
912 body.append(" | ");
913 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
914 body.append("\n");
915
916 std::cout << std::string(body.size(), '-') << "\n";
917 std::cout<< body;
918}
919
920void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
921{
922 std::string body;
923
924 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
925 body.append(" | ");
926 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
927 body.append(" | ");
928 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
929 body.append("\n");
930
931 std::cout << std::string(body.size(), '-') << "\n";
932
933 std::cout<< body;
934}
935
936void PrintCounterDetails(std::shared_ptr<Counter>& counter)
937{
938 std::string body;
939
940 body.append(CentreAlignFormatting(counter->m_Name, 20));
941 body.append(" | ");
942 body.append(CentreAlignFormatting(counter->m_Description, 50));
943 body.append(" | ");
944 body.append(CentreAlignFormatting(counter->m_Units, 14));
945 body.append(" | ");
946 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
947 body.append(" | ");
948 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
949 body.append(" | ");
950 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
951 body.append(" | ");
952 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
953 body.append(" | ");
954 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
955 body.append(" | ");
956 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
957 body.append(" | ");
958 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
959
960 body.append("\n");
961
962 std::cout << std::string(body.size(), '-') << "\n";
963
964 std::cout << body;
965}
966
967void PrintCategoryDetails(const std::unique_ptr<Category>& category,
968 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
969{
970 std::string categoryBody;
971 std::string categoryHeader;
972
973 categoryHeader.append(CentreAlignFormatting("Name", 20));
974 categoryHeader.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100975 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
976 categoryHeader.append("\n");
977
978 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
979 categoryBody.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100980 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
981
982 std::cout << "\n" << "\n";
983 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
984 std::cout << "\n";
985 std::cout << std::string(categoryHeader.size(), '=') << "\n";
986
987 std::cout << categoryHeader;
988
989 std::cout << std::string(categoryBody.size(), '-') << "\n";
990
991 std::cout << categoryBody;
992
993 std::string counterHeader;
994
995 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
996 counterHeader.append(" | ");
997 counterHeader.append(CentreAlignFormatting("Description", 50));
998 counterHeader.append(" | ");
999 counterHeader.append(CentreAlignFormatting("Units", 14));
1000 counterHeader.append(" | ");
1001 counterHeader.append(CentreAlignFormatting("UID", 6));
1002 counterHeader.append(" | ");
1003 counterHeader.append(CentreAlignFormatting("Max UID", 10));
1004 counterHeader.append(" | ");
1005 counterHeader.append(CentreAlignFormatting("Class", 8));
1006 counterHeader.append(" | ");
1007 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
1008 counterHeader.append(" | ");
1009 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
1010 counterHeader.append(" | ");
1011 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
1012 counterHeader.append(" | ");
1013 counterHeader.append(CentreAlignFormatting("Device UID", 14));
1014 counterHeader.append("\n");
1015
1016 std::cout << "\n" << "\n";
1017 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
1018 static_cast<int>(counterHeader.size()));
1019 std::cout << "\n";
1020 std::cout << std::string(counterHeader.size(), '=') << "\n";
1021 std::cout << counterHeader;
1022 for (auto& it: category->m_Counters) {
1023 auto search = counterMap.find(it);
1024 if(search != counterMap.end()) {
1025 PrintCounterDetails(search->second);
1026 }
1027 }
1028}
1029
1030void PrintCounterDirectory(ICounterDirectory& counterDirectory)
1031{
1032 std::string devicesHeader;
1033
1034 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1035 devicesHeader.append(" | ");
1036 devicesHeader.append(CentreAlignFormatting("UID", 13));
1037 devicesHeader.append(" | ");
1038 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1039 devicesHeader.append("\n");
1040
1041 std::cout << "\n" << "\n";
1042 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1043 std::cout << "\n";
1044 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1045 std::cout << devicesHeader;
1046 for (auto& it: counterDirectory.GetDevices()) {
1047 PrintDeviceDetails(it);
1048 }
1049
1050 std::string counterSetHeader;
1051
1052 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1053 counterSetHeader.append(" | ");
1054 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1055 counterSetHeader.append(" | ");
1056 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1057 counterSetHeader.append("\n");
1058
1059 std::cout << "\n" << "\n";
1060 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1061 std::cout << "\n";
1062 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1063
1064 std::cout << counterSetHeader;
1065
1066 for (auto& it: counterDirectory.GetCounterSets()) {
1067 PrintCounterSetDetails(it);
1068 }
1069
1070 auto counters = counterDirectory.GetCounters();
1071 for (auto& it: counterDirectory.GetCategories()) {
1072 PrintCategoryDetails(it, counters);
1073 }
1074 std::cout << "\n";
1075}
1076
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001077uint64_t GetTimestamp()
1078{
1079#if USE_CLOCK_MONOTONIC_RAW
1080 using clock = MonotonicClockRaw;
1081#else
1082 using clock = std::chrono::steady_clock;
1083#endif
1084
1085 // Take a timestamp
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001086 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001087
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001088 return static_cast<uint64_t>(timestamp.count());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001089}
Keith Davis3201eea2019-10-24 17:30:41 +01001090
Jim Flynn4e755a52020-03-29 17:48:26 +01001091Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1092{
1093 if (buffer == nullptr)
1094 {
1095 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1096 }
1097 if (length < 8)
1098 {
1099 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1100 }
1101
1102 uint32_t metadataIdentifier = 0;
1103 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1104
1105 uint32_t dataLength = 0;
1106 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1107
1108 std::unique_ptr<unsigned char[]> packetData;
1109 if (dataLength > 0)
1110 {
1111 packetData = std::make_unique<unsigned char[]>(dataLength);
1112 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1113 }
1114
1115 return Packet(metadataIdentifier, dataLength, packetData);
1116}
1117
Ferran Balaguer73882172019-09-02 16:39:42 +01001118} // namespace profiling
1119
Matteo Martincigh149528e2019-09-05 12:02:04 +01001120} // namespace armnn
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001121
1122namespace std
1123{
1124
Jim Flynn1fdeb992020-07-09 07:28:37 +01001125bool operator==(const std::vector<uint8_t>& left, int right)
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001126{
1127 return std::memcmp(left.data(), &right, left.size()) == 0;
1128}
1129
1130} // namespace std