blob: 6d2565c610c56b542841945854dd487f71b38cc4 [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ProfilingUtils.hpp"
7
Jim Flynn4e755a52020-03-29 17:48:26 +01008#include "common/include/ProfilingException.hpp"
9
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010010#include <armnn/Version.hpp>
11
Matteo Martincigh5dc816e2019-11-04 14:05:28 +000012#include <WallClockTimer.hpp>
13
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010014#include <armnn/utility/Assert.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010015
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010016#include <fstream>
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <iostream>
Matteo Martincighab173e92019-09-05 12:02:04 +010018#include <limits>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010019
Ferran Balaguer73882172019-09-02 16:39:42 +010020namespace armnn
21{
22
23namespace profiling
24{
25
Matteo Martincigh6db5f202019-09-05 12:02:04 +010026namespace
Matteo Martincighab173e92019-09-05 12:02:04 +010027{
Matteo Martincighab173e92019-09-05 12:02:04 +010028
Matteo Martincigh6db5f202019-09-05 12:02:04 +010029void ThrowIfCantGenerateNextUid(uint16_t uid, uint16_t cores = 0)
30{
Matteo Martincighab173e92019-09-05 12:02:04 +010031 // Check that it is possible to generate the next UID without causing an overflow
Matteo Martincigh6db5f202019-09-05 12:02:04 +010032 switch (cores)
Matteo Martincighab173e92019-09-05 12:02:04 +010033 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010034 case 0:
35 case 1:
36 // Number of cores not specified or set to 1 (a value of zero indicates the device is not capable of
37 // running multiple parallel workloads and will not provide multiple streams of data for each event)
38 if (uid == std::numeric_limits<uint16_t>::max())
39 {
40 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
41 }
42 break;
43 default: // cores > 1
44 // Multiple cores available, as max_counter_uid has to be set to: counter_uid + cores - 1, the maximum
45 // allowed value for a counter UID is consequently: uint16_t_max - cores + 1
46 if (uid >= std::numeric_limits<uint16_t>::max() - cores + 1)
47 {
48 throw RuntimeException("Generating the next UID for profiling would result in an overflow");
49 }
50 break;
Matteo Martincighab173e92019-09-05 12:02:04 +010051 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010052}
Matteo Martincighab173e92019-09-05 12:02:04 +010053
Matteo Martincigh6db5f202019-09-05 12:02:04 +010054} // Anonymous namespace
55
56uint16_t GetNextUid(bool peekOnly)
57{
58 // The UID used for profiling objects and events. The first valid UID is 1, as 0 is a reserved value
59 static uint16_t uid = 1;
60
61 // Check that it is possible to generate the next UID without causing an overflow (throws in case of error)
62 ThrowIfCantGenerateNextUid(uid);
63
64 if (peekOnly)
65 {
66 // Peek only
67 return uid;
68 }
69 else
70 {
71 // Get the next UID
72 return uid++;
73 }
74}
75
Keith Davise394bd92019-12-02 15:12:19 +000076std::vector<uint16_t> GetNextCounterUids(uint16_t firstUid, uint16_t cores)
Matteo Martincigh6db5f202019-09-05 12:02:04 +010077{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010078 // Check that it is possible to generate the next counter UID without causing an overflow (throws in case of error)
Keith Davise394bd92019-12-02 15:12:19 +000079 ThrowIfCantGenerateNextUid(firstUid, cores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +010080
81 // Get the next counter UIDs
82 size_t counterUidsSize = cores == 0 ? 1 : cores;
83 std::vector<uint16_t> counterUids(counterUidsSize, 0);
84 for (size_t i = 0; i < counterUidsSize; i++)
85 {
Keith Davise394bd92019-12-02 15:12:19 +000086 counterUids[i] = firstUid++;
Matteo Martincigh6db5f202019-09-05 12:02:04 +010087 }
88 return counterUids;
Matteo Martincighab173e92019-09-05 12:02:04 +010089}
90
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000091void WriteBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, const void* value, unsigned int valueSize)
92{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010093 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000094
95 WriteBytes(packetBuffer->GetWritableData(), offset, value, valueSize);
96}
97
Keith Davis3201eea2019-10-24 17:30:41 +010098uint32_t ConstructHeader(uint32_t packetFamily,
99 uint32_t packetId)
100{
Keith Davis33ed2212020-03-30 10:43:41 +0100101 return (( packetFamily & 0x0000003F ) << 26 )|
102 (( packetId & 0x000003FF ) << 16 );
Keith Davis3201eea2019-10-24 17:30:41 +0100103}
104
105void WriteUint64(const std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int offset, uint64_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100106{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100107 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100108
109 WriteUint64(packetBuffer->GetWritableData(), offset, value);
110}
111
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000112void WriteUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint32_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100113{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100114 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100115
116 WriteUint32(packetBuffer->GetWritableData(), offset, value);
117}
118
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000119void WriteUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint16_t value)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100120{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100121 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100122
123 WriteUint16(packetBuffer->GetWritableData(), offset, value);
124}
125
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000126void WriteUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset, uint8_t value)
127{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100128 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000129
130 WriteUint8(packetBuffer->GetWritableData(), offset, value);
131}
132
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000133void WriteBytes(unsigned char* buffer, unsigned int offset, const void* value, unsigned int valueSize)
134{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100135 ARMNN_ASSERT(buffer);
136 ARMNN_ASSERT(value);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000137
138 for (unsigned int i = 0; i < valueSize; i++, offset++)
139 {
140 buffer[offset] = *(reinterpret_cast<const unsigned char*>(value) + i);
141 }
142}
143
Francis Murtagh3a161982019-09-04 15:25:02 +0100144void WriteUint64(unsigned char* buffer, unsigned int offset, uint64_t value)
145{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100146 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100147
148 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
149 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
150 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
151 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
152 buffer[offset + 4] = static_cast<unsigned char>((value >> 32) & 0xFF);
153 buffer[offset + 5] = static_cast<unsigned char>((value >> 40) & 0xFF);
154 buffer[offset + 6] = static_cast<unsigned char>((value >> 48) & 0xFF);
155 buffer[offset + 7] = static_cast<unsigned char>((value >> 56) & 0xFF);
156}
157
Ferran Balaguer73882172019-09-02 16:39:42 +0100158void WriteUint32(unsigned char* buffer, unsigned int offset, uint32_t value)
159{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100160 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100161
Matteo Martincigh149528e2019-09-05 12:02:04 +0100162 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100163 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
164 buffer[offset + 2] = static_cast<unsigned char>((value >> 16) & 0xFF);
165 buffer[offset + 3] = static_cast<unsigned char>((value >> 24) & 0xFF);
166}
167
168void WriteUint16(unsigned char* buffer, unsigned int offset, uint16_t value)
169{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100170 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100171
Matteo Martincigh149528e2019-09-05 12:02:04 +0100172 buffer[offset] = static_cast<unsigned char>(value & 0xFF);
Ferran Balaguer73882172019-09-02 16:39:42 +0100173 buffer[offset + 1] = static_cast<unsigned char>((value >> 8) & 0xFF);
174}
175
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000176void WriteUint8(unsigned char* buffer, unsigned int offset, uint8_t value)
177{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100178 ARMNN_ASSERT(buffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000179
180 buffer[offset] = static_cast<unsigned char>(value);
181}
182
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000183void ReadBytes(const IPacketBufferPtr& packetBuffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
184{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100185 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000186
187 ReadBytes(packetBuffer->GetReadableData(), offset, valueSize, outValue);
188}
189
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000190uint64_t ReadUint64(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100191{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100192 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100193
194 return ReadUint64(packetBuffer->GetReadableData(), offset);
195}
196
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000197uint32_t ReadUint32(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100198{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100199 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100200
201 return ReadUint32(packetBuffer->GetReadableData(), offset);
202}
203
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000204uint16_t ReadUint16(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100205{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100206 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100207
208 return ReadUint16(packetBuffer->GetReadableData(), offset);
209}
210
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000211uint8_t ReadUint8(const IPacketBufferPtr& packetBuffer, unsigned int offset)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100212{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100213 ARMNN_ASSERT(packetBuffer);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100214
215 return ReadUint8(packetBuffer->GetReadableData(), offset);
216}
217
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000218void ReadBytes(const unsigned char* buffer, unsigned int offset, unsigned int valueSize, uint8_t outValue[])
219{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100220 ARMNN_ASSERT(buffer);
221 ARMNN_ASSERT(outValue);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000222
223 for (unsigned int i = 0; i < valueSize; i++, offset++)
224 {
225 outValue[i] = static_cast<uint8_t>(buffer[offset]);
226 }
227}
228
Francis Murtagh3a161982019-09-04 15:25:02 +0100229uint64_t ReadUint64(const unsigned char* buffer, unsigned int offset)
230{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100231 ARMNN_ASSERT(buffer);
Francis Murtagh3a161982019-09-04 15:25:02 +0100232
233 uint64_t value = 0;
Matteo Martincighab173e92019-09-05 12:02:04 +0100234 value = static_cast<uint64_t>(buffer[offset]);
Francis Murtagh3a161982019-09-04 15:25:02 +0100235 value |= static_cast<uint64_t>(buffer[offset + 1]) << 8;
236 value |= static_cast<uint64_t>(buffer[offset + 2]) << 16;
237 value |= static_cast<uint64_t>(buffer[offset + 3]) << 24;
238 value |= static_cast<uint64_t>(buffer[offset + 4]) << 32;
239 value |= static_cast<uint64_t>(buffer[offset + 5]) << 40;
240 value |= static_cast<uint64_t>(buffer[offset + 6]) << 48;
241 value |= static_cast<uint64_t>(buffer[offset + 7]) << 56;
242
243 return value;
244}
245
Ferran Balaguer73882172019-09-02 16:39:42 +0100246uint32_t ReadUint32(const unsigned char* buffer, unsigned int offset)
247{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100248 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100249
250 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100251 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100252 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
253 value |= static_cast<uint32_t>(buffer[offset + 2]) << 16;
254 value |= static_cast<uint32_t>(buffer[offset + 3]) << 24;
255 return value;
256}
257
258uint16_t ReadUint16(const unsigned char* buffer, unsigned int offset)
259{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100260 ARMNN_ASSERT(buffer);
Ferran Balaguer73882172019-09-02 16:39:42 +0100261
262 uint32_t value = 0;
Matteo Martincigh149528e2019-09-05 12:02:04 +0100263 value = static_cast<uint32_t>(buffer[offset]);
Ferran Balaguer73882172019-09-02 16:39:42 +0100264 value |= static_cast<uint32_t>(buffer[offset + 1]) << 8;
265 return static_cast<uint16_t>(value);
266}
267
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100268uint8_t ReadUint8(const unsigned char* buffer, unsigned int offset)
269{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100270 ARMNN_ASSERT(buffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100271
272 return buffer[offset];
273}
274
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100275std::string GetSoftwareInfo()
276{
277 return std::string("ArmNN");
278}
279
280std::string GetHardwareVersion()
281{
282 return std::string();
283}
284
285std::string GetSoftwareVersion()
286{
287 std::string armnnVersion(ARMNN_VERSION);
288 std::string result = "Armnn " + armnnVersion.substr(2,2) + "." + armnnVersion.substr(4,2);
289 return result;
290}
291
292std::string GetProcessName()
293{
294 std::ifstream comm("/proc/self/comm");
295 std::string name;
296 getline(comm, name);
297 return name;
298}
299
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000300// Calculate the actual length an SwString will be including the terminating null character
301// padding to bring it to the next uint32_t boundary but minus the leading uint32_t encoding
302// the size to allow the offset to be correctly updated when decoding a binary packet.
303uint32_t CalculateSizeOfPaddedSwString(const std::string& str)
304{
305 std::vector<uint32_t> swTraceString;
306 StringToSwTraceString<SwTraceCharPolicy>(str, swTraceString);
307 unsigned int uint32_t_size = sizeof(uint32_t);
308 uint32_t size = (boost::numeric_cast<uint32_t>(swTraceString.size()) - 1) * uint32_t_size;
309 return size;
310}
311
312// Read TimelineMessageDirectoryPacket from given IPacketBuffer and offset
313SwTraceMessage ReadSwTraceMessage(const unsigned char* packetBuffer, unsigned int& offset)
314{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100315 ARMNN_ASSERT(packetBuffer);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000316
317 unsigned int uint32_t_size = sizeof(uint32_t);
318
319 SwTraceMessage swTraceMessage;
320
321 // Read the decl_id
322 uint32_t readDeclId = ReadUint32(packetBuffer, offset);
323 swTraceMessage.m_Id = readDeclId;
324
325 // SWTrace "namestring" format
326 // length of the string (first 4 bytes) + string + null terminator
327
328 // Check the decl_name
329 offset += uint32_t_size;
330 uint32_t swTraceDeclNameLength = ReadUint32(packetBuffer, offset);
331
332 offset += uint32_t_size;
333 std::vector<unsigned char> swTraceStringBuffer(swTraceDeclNameLength - 1);
334 std::memcpy(swTraceStringBuffer.data(),
335 packetBuffer + offset, swTraceStringBuffer.size());
336
337 swTraceMessage.m_Name.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // name
338
339 // Check the ui_name
340 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_Name);
341 uint32_t swTraceUINameLength = ReadUint32(packetBuffer, offset);
342
343 offset += uint32_t_size;
344 swTraceStringBuffer.resize(swTraceUINameLength - 1);
345 std::memcpy(swTraceStringBuffer.data(),
346 packetBuffer + offset, swTraceStringBuffer.size());
347
348 swTraceMessage.m_UiName.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // ui_name
349
350 // Check arg_types
351 offset += CalculateSizeOfPaddedSwString(swTraceMessage.m_UiName);
352 uint32_t swTraceArgTypesLength = ReadUint32(packetBuffer, offset);
353
354 offset += uint32_t_size;
355 swTraceStringBuffer.resize(swTraceArgTypesLength - 1);
356 std::memcpy(swTraceStringBuffer.data(),
357 packetBuffer + offset, swTraceStringBuffer.size());
358
359 swTraceMessage.m_ArgTypes.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end()); // arg_types
360
361 std::string swTraceString(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
362
363 // Check arg_names
364 offset += CalculateSizeOfPaddedSwString(swTraceString);
365 uint32_t swTraceArgNamesLength = ReadUint32(packetBuffer, offset);
366
367 offset += uint32_t_size;
368 swTraceStringBuffer.resize(swTraceArgNamesLength - 1);
369 std::memcpy(swTraceStringBuffer.data(),
370 packetBuffer + offset, swTraceStringBuffer.size());
371
372 swTraceString.assign(swTraceStringBuffer.begin(), swTraceStringBuffer.end());
373 std::stringstream stringStream(swTraceString);
374 std::string argName;
375 while (std::getline(stringStream, argName, ','))
376 {
377 swTraceMessage.m_ArgNames.push_back(argName);
378 }
379
380 offset += CalculateSizeOfPaddedSwString(swTraceString);
381
382 return swTraceMessage;
383}
384
Jan Eilers92fa15b2019-10-15 15:23:25 +0100385/// Creates a timeline packet header
386///
387/// \params
388/// packetFamiliy Timeline Packet Family
389/// packetClass Timeline Packet Class
390/// packetType Timeline Packet Type
391/// streamId Stream identifier
392/// seqeunceNumbered When non-zero the 4 bytes following the header is a u32 sequence number
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100393/// dataLength Unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
Jan Eilers92fa15b2019-10-15 15:23:25 +0100394///
395/// \returns
396/// Pair of uint32_t containing word0 and word1 of the header
397std::pair<uint32_t, uint32_t> CreateTimelinePacketHeader(uint32_t packetFamily,
398 uint32_t packetClass,
399 uint32_t packetType,
400 uint32_t streamId,
401 uint32_t sequenceNumbered,
402 uint32_t dataLength)
403{
404 // Packet header word 0:
405 // 26:31 [6] packet_family: timeline Packet Family, value 0b000001
406 // 19:25 [7] packet_class: packet class
407 // 16:18 [3] packet_type: packet type
408 // 8:15 [8] reserved: all zeros
409 // 0:7 [8] stream_id: stream identifier
410 uint32_t packetHeaderWord0 = ((packetFamily & 0x0000003F) << 26) |
411 ((packetClass & 0x0000007F) << 19) |
412 ((packetType & 0x00000007) << 16) |
413 ((streamId & 0x00000007) << 0);
414
415 // Packet header word 1:
416 // 25:31 [7] reserved: all zeros
417 // 24 [1] sequence_numbered: when non-zero the 4 bytes following the header is a u32 sequence number
418 // 0:23 [24] data_length: unsigned 24-bit integer. Length of data, in bytes. Zero is permitted
419 uint32_t packetHeaderWord1 = ((sequenceNumbered & 0x00000001) << 24) |
420 ((dataLength & 0x00FFFFFF) << 0);
421
422 return std::make_pair(packetHeaderWord0, packetHeaderWord1);
423}
424
425/// Creates a packet header for the timeline messages:
426/// * declareLabel
427/// * declareEntity
428/// * declareEventClass
429/// * declareRelationship
430/// * declareEvent
431///
432/// \param
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100433/// dataLength The length of the message body in bytes
Jan Eilers92fa15b2019-10-15 15:23:25 +0100434///
435/// \returns
436/// Pair of uint32_t containing word0 and word1 of the header
437std::pair<uint32_t, uint32_t> CreateTimelineMessagePacketHeader(unsigned int dataLength)
438{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100439 return CreateTimelinePacketHeader(1, // Packet family
440 0, // Packet class
441 1, // Packet type
442 0, // Stream id
443 0, // Sequence number
444 dataLength); // Data length
Jan Eilers92fa15b2019-10-15 15:23:25 +0100445}
446
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100447TimelinePacketStatus WriteTimelineLabelBinaryPacket(uint64_t profilingGuid,
448 const std::string& label,
449 unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000450 unsigned int remainingBufferSize,
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100451 unsigned int& numberOfBytesWritten)
452{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100453 // Initialize the output value
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100454 numberOfBytesWritten = 0;
455
456 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000457 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100458 {
459 return TimelinePacketStatus::BufferExhaustion;
460 }
461
462 // Utils
463 unsigned int uint32_t_size = sizeof(uint32_t);
464 unsigned int uint64_t_size = sizeof(uint64_t);
465
466 // Convert the label into a SWTrace string
467 std::vector<uint32_t> swTraceLabel;
468 bool result = StringToSwTraceString<SwTraceCharPolicy>(label, swTraceLabel);
469 if (!result)
470 {
471 return TimelinePacketStatus::Error;
472 }
473
474 // Calculate the size of the SWTrace string label (in bytes)
475 unsigned int swTraceLabelSize = boost::numeric_cast<unsigned int>(swTraceLabel.size()) * uint32_t_size;
476
477 // Calculate the length of the data (in bytes)
Jan Eilersb884ea42019-10-16 09:54:15 +0100478 unsigned int timelineLabelPacketDataLength = uint32_t_size + // decl_Id
479 uint64_t_size + // Profiling GUID
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100480 swTraceLabelSize; // Label
481
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100482 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000483 if (timelineLabelPacketDataLength > remainingBufferSize)
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100484 {
485 return TimelinePacketStatus::BufferExhaustion;
486 }
487
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100488 // Initialize the offset for writing in the buffer
489 unsigned int offset = 0;
490
Jan Eilersb884ea42019-10-16 09:54:15 +0100491 // Write decl_Id to the buffer
492 WriteUint32(buffer, offset, 0u);
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100493 offset += uint32_t_size;
494
495 // Write the timeline binary packet payload to the buffer
496 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
497 offset += uint64_t_size;
498 for (uint32_t swTraceLabelWord : swTraceLabel)
499 {
500 WriteUint32(buffer, offset, swTraceLabelWord); // Label
501 offset += uint32_t_size;
502 }
503
504 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000505 numberOfBytesWritten = timelineLabelPacketDataLength;
Matteo Martincigh0aed4f92019-10-01 14:25:34 +0100506
507 return TimelinePacketStatus::Ok;
508}
509
Keith Davis97da5e22020-03-05 16:25:28 +0000510TimelinePacketStatus WriteTimelineEntityBinary(uint64_t profilingGuid,
511 unsigned char* buffer,
512 unsigned int remainingBufferSize,
513 unsigned int& numberOfBytesWritten)
David Monahanf21f6062019-10-07 15:11:15 +0100514{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100515 // Initialize the output value
David Monahanf21f6062019-10-07 15:11:15 +0100516 numberOfBytesWritten = 0;
517
518 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000519 if (buffer == nullptr || remainingBufferSize == 0)
David Monahanf21f6062019-10-07 15:11:15 +0100520 {
521 return TimelinePacketStatus::BufferExhaustion;
522 }
523
524 // Utils
525 unsigned int uint32_t_size = sizeof(uint32_t);
526 unsigned int uint64_t_size = sizeof(uint64_t);
527
528 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000529 unsigned int timelineEntityDataLength = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
David Monahanf21f6062019-10-07 15:11:15 +0100530
531 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000532 if (timelineEntityDataLength > remainingBufferSize)
David Monahanf21f6062019-10-07 15:11:15 +0100533 {
534 return TimelinePacketStatus::BufferExhaustion;
535 }
536
David Monahanf21f6062019-10-07 15:11:15 +0100537 // Initialize the offset for writing in the buffer
538 unsigned int offset = 0;
539
Jan Eilersb884ea42019-10-16 09:54:15 +0100540 // Write the decl_Id to the buffer
541 WriteUint32(buffer, offset, 1u);
David Monahanf21f6062019-10-07 15:11:15 +0100542 offset += uint32_t_size;
543
544 // Write the timeline binary packet payload to the buffer
545 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
546
547 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000548 numberOfBytesWritten = timelineEntityDataLength;
David Monahanf21f6062019-10-07 15:11:15 +0100549
550 return TimelinePacketStatus::Ok;
551}
552
Keith Davis97da5e22020-03-05 16:25:28 +0000553TimelinePacketStatus WriteTimelineRelationshipBinary(ProfilingRelationshipType relationshipType,
554 uint64_t relationshipGuid,
555 uint64_t headGuid,
556 uint64_t tailGuid,
557 unsigned char* buffer,
558 unsigned int remainingBufferSize,
559 unsigned int& numberOfBytesWritten)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100560{
561 // Initialize the output value
562 numberOfBytesWritten = 0;
563
564 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000565 if (buffer == nullptr || remainingBufferSize == 0)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100566 {
567 return TimelinePacketStatus::BufferExhaustion;
568 }
569
570 // Utils
571 unsigned int uint32_t_size = sizeof(uint32_t);
572 unsigned int uint64_t_size = sizeof(uint64_t);
573
574 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000575 unsigned int timelineRelationshipDataLength = uint32_t_size * 2 + // decl_id + Relationship Type
576 uint64_t_size * 3; // Relationship GUID + Head GUID + tail GUID
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100577
Keith Davis97da5e22020-03-05 16:25:28 +0000578 // Check whether the timeline binary fits in the given buffer
579 if (timelineRelationshipDataLength > remainingBufferSize)
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100580 {
581 return TimelinePacketStatus::BufferExhaustion;
582 }
583
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100584 // Initialize the offset for writing in the buffer
585 unsigned int offset = 0;
586
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100587 uint32_t relationshipTypeUint = 0;
588
589 switch (relationshipType)
590 {
591 case ProfilingRelationshipType::RetentionLink:
592 relationshipTypeUint = 0;
593 break;
594 case ProfilingRelationshipType::ExecutionLink:
595 relationshipTypeUint = 1;
596 break;
597 case ProfilingRelationshipType::DataLink:
598 relationshipTypeUint = 2;
599 break;
600 case ProfilingRelationshipType::LabelLink:
601 relationshipTypeUint = 3;
602 break;
603 default:
604 throw InvalidArgumentException("Unknown relationship type given.");
605 }
606
Keith Davis97da5e22020-03-05 16:25:28 +0000607 // Write the timeline binary payload to the buffer
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100608 // decl_id of the timeline message
609 uint32_t declId = 3;
610 WriteUint32(buffer, offset, declId); // decl_id
611 offset += uint32_t_size;
612 WriteUint32(buffer, offset, relationshipTypeUint); // Relationship Type
613 offset += uint32_t_size;
614 WriteUint64(buffer, offset, relationshipGuid); // GUID of this relationship
615 offset += uint64_t_size;
616 WriteUint64(buffer, offset, headGuid); // head of relationship GUID
617 offset += uint64_t_size;
618 WriteUint64(buffer, offset, tailGuid); // tail of relationship GUID
619
620 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000621 numberOfBytesWritten = timelineRelationshipDataLength;
Narumol Prangnawarat7e5eec72019-10-16 12:16:26 +0100622
623 return TimelinePacketStatus::Ok;
624}
625
Sadik Armagan784db772019-10-08 15:05:38 +0100626TimelinePacketStatus WriteTimelineMessageDirectoryPackage(unsigned char* buffer,
Keith Davis97da5e22020-03-05 16:25:28 +0000627 unsigned int remainingBufferSize,
Sadik Armagan784db772019-10-08 15:05:38 +0100628 unsigned int& numberOfBytesWritten)
629{
630 // Initialize the output value
631 numberOfBytesWritten = 0;
632
633 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000634 if (buffer == nullptr || remainingBufferSize == 0)
Sadik Armagan784db772019-10-08 15:05:38 +0100635 {
636 return TimelinePacketStatus::BufferExhaustion;
637 }
638
639 // Utils
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000640 unsigned int uint8_t_size = sizeof(uint8_t);
Sadik Armagan784db772019-10-08 15:05:38 +0100641 unsigned int uint32_t_size = sizeof(uint32_t);
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000642 unsigned int uint64_t_size = sizeof(uint64_t);
643 unsigned int threadId_size = sizeof(std::thread::id);
Sadik Armagan784db772019-10-08 15:05:38 +0100644
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100645 // The payload/data of the packet consists of swtrace event definitions encoded according
Sadik Armagan784db772019-10-08 15:05:38 +0100646 // to the swtrace directory specification. The messages being the five defined below:
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000647 //
648 // | decl_id | decl_name | ui_name | arg_types | arg_names |
Sadik Armagan784db772019-10-08 15:05:38 +0100649 // |-----------|---------------------|-----------------------|-------------|-------------------------------------|
650 // | 0 | declareLabel | declare label | ps | guid,value |
651 // | 1 | declareEntity | declare entity | p | guid |
652 // | 2 | declareEventClass | declare event class | p | guid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100653 // | 3 | declareRelationship | declare relationship | Ippp | relationshipType,relationshipGuid, |
654 // | | | | | headGuid,tailGuid |
Sadik Armagan784db772019-10-08 15:05:38 +0100655 // | 4 | declareEvent | declare event | @tp | timestamp,threadId,eventGuid |
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100656 std::vector<std::vector<std::string>> timelineDirectoryMessages
657 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000658 { "0", "declareLabel", "declare label", "ps", "guid,value" },
659 { "1", "declareEntity", "declare entity", "p", "guid" },
660 { "2", "declareEventClass", "declare event class", "p", "guid" },
661 { "3", "declareRelationship", "declare relationship", "Ippp",
662 "relationshipType,relationshipGuid,headGuid,tailGuid" },
663 { "4", "declareEvent", "declare event", "@tp", "timestamp,threadId,eventGuid" }
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100664 };
Sadik Armagan784db772019-10-08 15:05:38 +0100665
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000666 // Build the message declarations
667 std::vector<uint32_t> swTraceBuffer;
668 for (const auto& directoryComponent : timelineDirectoryMessages)
Sadik Armagan784db772019-10-08 15:05:38 +0100669 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000670 // decl_id
671 uint32_t declId = 0;
672 try
Sadik Armagan784db772019-10-08 15:05:38 +0100673 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000674 declId = boost::numeric_cast<uint32_t>(std::stoul(directoryComponent[0]));
Sadik Armagan784db772019-10-08 15:05:38 +0100675 }
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000676 catch (const std::exception&)
677 {
678 return TimelinePacketStatus::Error;
679 }
680 swTraceBuffer.push_back(declId);
681
682 bool result = true;
683 result &= ConvertDirectoryComponent<SwTraceNameCharPolicy>(directoryComponent[1], swTraceBuffer); // decl_name
684 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[2], swTraceBuffer); // ui_name
685 result &= ConvertDirectoryComponent<SwTraceTypeCharPolicy>(directoryComponent[3], swTraceBuffer); // arg_types
686 result &= ConvertDirectoryComponent<SwTraceCharPolicy> (directoryComponent[4], swTraceBuffer); // arg_names
687 if (!result)
688 {
689 return TimelinePacketStatus::Error;
690 }
Sadik Armagan784db772019-10-08 15:05:38 +0100691 }
692
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000693 unsigned int dataLength = 3 * uint8_t_size + // Stream header (3 bytes)
694 boost::numeric_cast<unsigned int>(swTraceBuffer.size()) *
695 uint32_t_size; // Trace directory (5 messages)
696
Sadik Armagan784db772019-10-08 15:05:38 +0100697 // Calculate the timeline directory binary packet size (in bytes)
698 unsigned int timelineDirectoryPacketSize = 2 * uint32_t_size + // Header (2 words)
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000699 dataLength; // Payload
Sadik Armagan784db772019-10-08 15:05:38 +0100700
701 // Check whether the timeline directory binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000702 if (timelineDirectoryPacketSize > remainingBufferSize)
Sadik Armagan784db772019-10-08 15:05:38 +0100703 {
704 return TimelinePacketStatus::BufferExhaustion;
705 }
706
Jan Eilersb884ea42019-10-16 09:54:15 +0100707 // Create packet header
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000708 auto packetHeader = CreateTimelinePacketHeader(1, 0, 0, 0, 0, boost::numeric_cast<uint32_t>(dataLength));
Sadik Armagan784db772019-10-08 15:05:38 +0100709
710 // Initialize the offset for writing in the buffer
711 unsigned int offset = 0;
712
713 // Write the timeline binary packet header to the buffer
Jan Eilersb884ea42019-10-16 09:54:15 +0100714 WriteUint32(buffer, offset, packetHeader.first);
Sadik Armagan784db772019-10-08 15:05:38 +0100715 offset += uint32_t_size;
Jan Eilersb884ea42019-10-16 09:54:15 +0100716 WriteUint32(buffer, offset, packetHeader.second);
Sadik Armagan784db772019-10-08 15:05:38 +0100717 offset += uint32_t_size;
718
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000719 // Write the stream header
720 uint8_t streamVersion = 4;
721 uint8_t pointerBytes = boost::numeric_cast<uint8_t>(uint64_t_size); // All GUIDs are uint64_t
722 uint8_t threadIdBytes = boost::numeric_cast<uint8_t>(threadId_size);
723 switch (threadIdBytes)
Sadik Armagan784db772019-10-08 15:05:38 +0100724 {
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000725 case 4: // Typically Windows and Android
726 case 8: // Typically Linux
727 break; // Valid values
728 default:
729 return TimelinePacketStatus::Error; // Invalid value
730 }
731 WriteUint8(buffer, offset, streamVersion);
732 offset += uint8_t_size;
733 WriteUint8(buffer, offset, pointerBytes);
734 offset += uint8_t_size;
735 WriteUint8(buffer, offset, threadIdBytes);
736 offset += uint8_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100737
Matteo Martincigh34a407d2019-11-06 15:30:54 +0000738 // Write the SWTrace directory
739 uint32_t numberOfDeclarations = boost::numeric_cast<uint32_t>(timelineDirectoryMessages.size());
740 WriteUint32(buffer, offset, numberOfDeclarations); // Number of declarations
741 offset += uint32_t_size;
742 for (uint32_t i : swTraceBuffer)
743 {
744 WriteUint32(buffer, offset, i); // Message declarations
745 offset += uint32_t_size;
Sadik Armagan784db772019-10-08 15:05:38 +0100746 }
747
748 // Update the number of bytes written
749 numberOfBytesWritten = timelineDirectoryPacketSize;
750
751 return TimelinePacketStatus::Ok;
752}
753
Keith Davis97da5e22020-03-05 16:25:28 +0000754TimelinePacketStatus WriteTimelineEventClassBinary(uint64_t profilingGuid,
755 unsigned char* buffer,
756 unsigned int remainingBufferSize,
757 unsigned int& numberOfBytesWritten)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100758{
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100759 // Initialize the output value
Jan Eilers92fa15b2019-10-15 15:23:25 +0100760 numberOfBytesWritten = 0;
761
762 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000763 if (buffer == nullptr || remainingBufferSize == 0)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100764 {
765 return TimelinePacketStatus::BufferExhaustion;
766 }
767
768 // Utils
769 unsigned int uint32_t_size = sizeof(uint32_t);
770 unsigned int uint64_t_size = sizeof(uint64_t);
771
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100772 // decl_id of the timeline message
773 uint32_t declId = 2;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100774
775 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000776 unsigned int dataSize = uint32_t_size + uint64_t_size; // decl_id + Profiling GUID
Jan Eilers92fa15b2019-10-15 15:23:25 +0100777
Keith Davis97da5e22020-03-05 16:25:28 +0000778 // Check whether the timeline binary fits in the given buffer
779 if (dataSize > remainingBufferSize)
Jan Eilers92fa15b2019-10-15 15:23:25 +0100780 {
781 return TimelinePacketStatus::BufferExhaustion;
782 }
783
Jan Eilers92fa15b2019-10-15 15:23:25 +0100784 // Initialize the offset for writing in the buffer
785 unsigned int offset = 0;
786
Keith Davis97da5e22020-03-05 16:25:28 +0000787 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100788 WriteUint32(buffer, offset, declId); // decl_id
Jan Eilers92fa15b2019-10-15 15:23:25 +0100789 offset += uint32_t_size;
790 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
791
792 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000793 numberOfBytesWritten = dataSize;
Jan Eilers92fa15b2019-10-15 15:23:25 +0100794
795 return TimelinePacketStatus::Ok;
796}
797
Keith Davis97da5e22020-03-05 16:25:28 +0000798TimelinePacketStatus WriteTimelineEventBinary(uint64_t timestamp,
799 std::thread::id threadId,
800 uint64_t profilingGuid,
801 unsigned char* buffer,
802 unsigned int remainingBufferSize,
803 unsigned int& numberOfBytesWritten)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100804{
805 // Initialize the output value
806 numberOfBytesWritten = 0;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100807 // Check that the given buffer is valid
Keith Davis97da5e22020-03-05 16:25:28 +0000808 if (buffer == nullptr || remainingBufferSize == 0)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100809 {
810 return TimelinePacketStatus::BufferExhaustion;
811 }
812
813 // Utils
814 unsigned int uint32_t_size = sizeof(uint32_t);
815 unsigned int uint64_t_size = sizeof(uint64_t);
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000816 unsigned int threadId_size = sizeof(std::thread::id);
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100817
818 // decl_id of the timeline message
819 uint32_t declId = 4;
820
821 // Calculate the length of the data (in bytes)
Keith Davis97da5e22020-03-05 16:25:28 +0000822 unsigned int timelineEventDataLength = uint32_t_size + // decl_id
823 uint64_t_size + // Timestamp
824 threadId_size + // Thread id
825 uint64_t_size; // Profiling GUID
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100826
827 // Check whether the timeline binary packet fits in the given buffer
Keith Davis97da5e22020-03-05 16:25:28 +0000828 if (timelineEventDataLength > remainingBufferSize)
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100829 {
830 return TimelinePacketStatus::BufferExhaustion;
831 }
832
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100833 // Initialize the offset for writing in the buffer
834 unsigned int offset = 0;
835
Keith Davis97da5e22020-03-05 16:25:28 +0000836 // Write the timeline binary payload to the buffer
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100837 WriteUint32(buffer, offset, declId); // decl_id
838 offset += uint32_t_size;
839 WriteUint64(buffer, offset, timestamp); // Timestamp
840 offset += uint64_t_size;
Matteo Martincigh378bbfc2019-11-04 14:05:28 +0000841 WriteBytes(buffer, offset, &threadId, threadId_size); // Thread id
842 offset += threadId_size;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100843 WriteUint64(buffer, offset, profilingGuid); // Profiling GUID
844 offset += uint64_t_size;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100845 // Update the number of bytes written
Keith Davis97da5e22020-03-05 16:25:28 +0000846 numberOfBytesWritten = timelineEventDataLength;
Matteo Martincigh8844c2f2019-10-16 10:29:17 +0100847
848 return TimelinePacketStatus::Ok;
849}
850
Keith Davis3201eea2019-10-24 17:30:41 +0100851std::string CentreAlignFormatting(const std::string& stringToPass, const int spacingWidth)
852{
853 std::stringstream outputStream, centrePadding;
854 int padding = spacingWidth - static_cast<int>(stringToPass.size());
855
856 for (int i = 0; i < padding / 2; ++i)
857 {
858 centrePadding << " ";
859 }
860
861 outputStream << centrePadding.str() << stringToPass << centrePadding.str();
862
863 if (padding > 0 && padding %2 != 0)
864 {
865 outputStream << " ";
866 }
867
868 return outputStream.str();
869}
870
871void PrintDeviceDetails(const std::pair<const unsigned short, std::unique_ptr<Device>>& devicePair)
872{
873 std::string body;
874
875 body.append(CentreAlignFormatting(devicePair.second->m_Name, 20));
876 body.append(" | ");
877 body.append(CentreAlignFormatting(std::to_string(devicePair.first), 13));
878 body.append(" | ");
879 body.append(CentreAlignFormatting(std::to_string(devicePair.second->m_Cores), 10));
880 body.append("\n");
881
882 std::cout << std::string(body.size(), '-') << "\n";
883 std::cout<< body;
884}
885
886void PrintCounterSetDetails(const std::pair<const unsigned short, std::unique_ptr<CounterSet>>& counterSetPair)
887{
888 std::string body;
889
890 body.append(CentreAlignFormatting(counterSetPair.second->m_Name, 20));
891 body.append(" | ");
892 body.append(CentreAlignFormatting(std::to_string(counterSetPair.first), 13));
893 body.append(" | ");
894 body.append(CentreAlignFormatting(std::to_string(counterSetPair.second->m_Count), 10));
895 body.append("\n");
896
897 std::cout << std::string(body.size(), '-') << "\n";
898
899 std::cout<< body;
900}
901
902void PrintCounterDetails(std::shared_ptr<Counter>& counter)
903{
904 std::string body;
905
906 body.append(CentreAlignFormatting(counter->m_Name, 20));
907 body.append(" | ");
908 body.append(CentreAlignFormatting(counter->m_Description, 50));
909 body.append(" | ");
910 body.append(CentreAlignFormatting(counter->m_Units, 14));
911 body.append(" | ");
912 body.append(CentreAlignFormatting(std::to_string(counter->m_Uid), 6));
913 body.append(" | ");
914 body.append(CentreAlignFormatting(std::to_string(counter->m_MaxCounterUid), 10));
915 body.append(" | ");
916 body.append(CentreAlignFormatting(std::to_string(counter->m_Class), 8));
917 body.append(" | ");
918 body.append(CentreAlignFormatting(std::to_string(counter->m_Interpolation), 14));
919 body.append(" | ");
920 body.append(CentreAlignFormatting(std::to_string(counter->m_Multiplier), 20));
921 body.append(" | ");
922 body.append(CentreAlignFormatting(std::to_string(counter->m_CounterSetUid), 16));
923 body.append(" | ");
924 body.append(CentreAlignFormatting(std::to_string(counter->m_DeviceUid), 14));
925
926 body.append("\n");
927
928 std::cout << std::string(body.size(), '-') << "\n";
929
930 std::cout << body;
931}
932
933void PrintCategoryDetails(const std::unique_ptr<Category>& category,
934 std::unordered_map<unsigned short, std::shared_ptr<Counter>> counterMap)
935{
936 std::string categoryBody;
937 std::string categoryHeader;
938
939 categoryHeader.append(CentreAlignFormatting("Name", 20));
940 categoryHeader.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100941 categoryHeader.append(CentreAlignFormatting("Event Count", 14));
942 categoryHeader.append("\n");
943
944 categoryBody.append(CentreAlignFormatting(category->m_Name, 20));
945 categoryBody.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100946 categoryBody.append(CentreAlignFormatting(std::to_string(category->m_Counters.size()), 14));
947
948 std::cout << "\n" << "\n";
949 std::cout << CentreAlignFormatting("CATEGORY", static_cast<int>(categoryHeader.size()));
950 std::cout << "\n";
951 std::cout << std::string(categoryHeader.size(), '=') << "\n";
952
953 std::cout << categoryHeader;
954
955 std::cout << std::string(categoryBody.size(), '-') << "\n";
956
957 std::cout << categoryBody;
958
959 std::string counterHeader;
960
961 counterHeader.append(CentreAlignFormatting("Counter Name", 20));
962 counterHeader.append(" | ");
963 counterHeader.append(CentreAlignFormatting("Description", 50));
964 counterHeader.append(" | ");
965 counterHeader.append(CentreAlignFormatting("Units", 14));
966 counterHeader.append(" | ");
967 counterHeader.append(CentreAlignFormatting("UID", 6));
968 counterHeader.append(" | ");
969 counterHeader.append(CentreAlignFormatting("Max UID", 10));
970 counterHeader.append(" | ");
971 counterHeader.append(CentreAlignFormatting("Class", 8));
972 counterHeader.append(" | ");
973 counterHeader.append(CentreAlignFormatting("Interpolation", 14));
974 counterHeader.append(" | ");
975 counterHeader.append(CentreAlignFormatting("Multiplier", 20));
976 counterHeader.append(" | ");
977 counterHeader.append(CentreAlignFormatting("Counter set UID", 16));
978 counterHeader.append(" | ");
979 counterHeader.append(CentreAlignFormatting("Device UID", 14));
980 counterHeader.append("\n");
981
982 std::cout << "\n" << "\n";
983 std::cout << CentreAlignFormatting("EVENTS IN CATEGORY: " + category->m_Name,
984 static_cast<int>(counterHeader.size()));
985 std::cout << "\n";
986 std::cout << std::string(counterHeader.size(), '=') << "\n";
987 std::cout << counterHeader;
988 for (auto& it: category->m_Counters) {
989 auto search = counterMap.find(it);
990 if(search != counterMap.end()) {
991 PrintCounterDetails(search->second);
992 }
993 }
994}
995
996void PrintCounterDirectory(ICounterDirectory& counterDirectory)
997{
998 std::string devicesHeader;
999
1000 devicesHeader.append(CentreAlignFormatting("Device name", 20));
1001 devicesHeader.append(" | ");
1002 devicesHeader.append(CentreAlignFormatting("UID", 13));
1003 devicesHeader.append(" | ");
1004 devicesHeader.append(CentreAlignFormatting("Cores", 10));
1005 devicesHeader.append("\n");
1006
1007 std::cout << "\n" << "\n";
1008 std::cout << CentreAlignFormatting("DEVICES", static_cast<int>(devicesHeader.size()));
1009 std::cout << "\n";
1010 std::cout << std::string(devicesHeader.size(), '=') << "\n";
1011 std::cout << devicesHeader;
1012 for (auto& it: counterDirectory.GetDevices()) {
1013 PrintDeviceDetails(it);
1014 }
1015
1016 std::string counterSetHeader;
1017
1018 counterSetHeader.append(CentreAlignFormatting("Counter set name", 20));
1019 counterSetHeader.append(" | ");
1020 counterSetHeader.append(CentreAlignFormatting("UID", 13));
1021 counterSetHeader.append(" | ");
1022 counterSetHeader.append(CentreAlignFormatting("Count", 10));
1023 counterSetHeader.append("\n");
1024
1025 std::cout << "\n" << "\n";
1026 std::cout << CentreAlignFormatting("COUNTER SETS", static_cast<int>(counterSetHeader.size()));
1027 std::cout << "\n";
1028 std::cout << std::string(counterSetHeader.size(), '=') << "\n";
1029
1030 std::cout << counterSetHeader;
1031
1032 for (auto& it: counterDirectory.GetCounterSets()) {
1033 PrintCounterSetDetails(it);
1034 }
1035
1036 auto counters = counterDirectory.GetCounters();
1037 for (auto& it: counterDirectory.GetCategories()) {
1038 PrintCategoryDetails(it, counters);
1039 }
1040 std::cout << "\n";
1041}
1042
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001043uint64_t GetTimestamp()
1044{
1045#if USE_CLOCK_MONOTONIC_RAW
1046 using clock = MonotonicClockRaw;
1047#else
1048 using clock = std::chrono::steady_clock;
1049#endif
1050
1051 // Take a timestamp
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001052 auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now().time_since_epoch());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001053
Finn Williamsd9ba1a72020-04-16 15:32:28 +01001054 return static_cast<uint64_t>(timestamp.count());
Matteo Martincigh5dc816e2019-11-04 14:05:28 +00001055}
Keith Davis3201eea2019-10-24 17:30:41 +01001056
Jim Flynn4e755a52020-03-29 17:48:26 +01001057Packet ReceivePacket(const unsigned char* buffer, uint32_t length)
1058{
1059 if (buffer == nullptr)
1060 {
1061 throw armnnProfiling::ProfilingException("data buffer is nullptr");
1062 }
1063 if (length < 8)
1064 {
1065 throw armnnProfiling::ProfilingException("length of data buffer is less than 8");
1066 }
1067
1068 uint32_t metadataIdentifier = 0;
1069 std::memcpy(&metadataIdentifier, buffer, sizeof(metadataIdentifier));
1070
1071 uint32_t dataLength = 0;
1072 std::memcpy(&dataLength, buffer + 4u, sizeof(dataLength));
1073
1074 std::unique_ptr<unsigned char[]> packetData;
1075 if (dataLength > 0)
1076 {
1077 packetData = std::make_unique<unsigned char[]>(dataLength);
1078 std::memcpy(packetData.get(), buffer + 8u, dataLength);
1079 }
1080
1081 return Packet(metadataIdentifier, dataLength, packetData);
1082}
1083
Ferran Balaguer73882172019-09-02 16:39:42 +01001084} // namespace profiling
1085
Matteo Martincigh149528e2019-09-05 12:02:04 +01001086} // namespace armnn
Matteo Martincigh378bbfc2019-11-04 14:05:28 +00001087
1088namespace std
1089{
1090
1091bool operator==(const std::vector<uint8_t>& left, std::thread::id right)
1092{
1093 return std::memcmp(left.data(), &right, left.size()) == 0;
1094}
1095
1096} // namespace std