blob: 9954bd9a0467d49a94752a13320e7b2b20947d5f [file] [log] [blame]
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "IBufferManager.hpp"
Colm Donelan5ccb33d2020-01-24 16:27:02 +00009#include "armnn/profiling/ISendTimelinePacket.hpp"
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010010#include "ProfilingUtils.hpp"
11
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010012#include <armnn/utility/Assert.hpp>
Matteo Martincigh9723d022019-11-13 10:56:41 +000013
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010014#include <memory>
15
16namespace armnn
17{
18
19namespace profiling
20{
21
22class SendTimelinePacket : public ISendTimelinePacket
23{
24public:
25 SendTimelinePacket(IBufferManager& bufferManager)
26 : m_BufferManager(bufferManager)
27 , m_WriteBuffer(nullptr)
Keith Davis97da5e22020-03-05 16:25:28 +000028 , m_Offset(8u)
29 , m_RemainingBufferSize(0u)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010030 {}
31
32 /// Commits the current buffer and reset the member variables
33 void Commit() override;
34
35 /// Create and write a TimelineEntityBinaryPacket from the parameters to the buffer.
36 void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override;
37
38 /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
Matteo Martincigh378bbfc2019-11-04 14:05:28 +000039 void SendTimelineEventBinaryPacket(uint64_t timestamp, std::thread::id threadId, uint64_t profilingGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010040
41 /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
42 void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid) override;
43
44 /// Create and write a TimelineLabelBinaryPacket from the parameters to the buffer.
45 void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label) override;
46
47 /// Create and write a TimelineMessageDirectoryPackage in the buffer
48 void SendTimelineMessageDirectoryPackage() override;
49
50 /// Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer.
51 virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
52 uint64_t relationshipGuid,
53 uint64_t headGuid,
54 uint64_t tailGuid) override;
55private:
56 /// Reserves maximum packet size from buffer
57 void ReserveBuffer();
58
Matteo Martincigh9723d022019-11-13 10:56:41 +000059 template <typename Func, typename ... Params>
60 void ForwardWriteBinaryFunction(Func& func, Params&& ... params);
61
Keith Davis97da5e22020-03-05 16:25:28 +000062 IBufferManager& m_BufferManager;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000063 IPacketBufferPtr m_WriteBuffer;
Keith Davis97da5e22020-03-05 16:25:28 +000064 unsigned int m_Offset;
65 unsigned int m_RemainingBufferSize;
66
67 const unsigned int m_uint32_t_size = sizeof(uint32_t);
68
69 std::pair<uint32_t, uint32_t> m_PacketHeader;
70 uint32_t m_PacketDataLength;
71
72 bool m_DirectoryPackage = false;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010073};
74
Keith Davis97da5e22020-03-05 16:25:28 +000075template<typename Func, typename ... Params>
Matteo Martincigh9723d022019-11-13 10:56:41 +000076void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params)
77{
78 try
79 {
80 ReserveBuffer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010081 ARMNN_ASSERT(m_WriteBuffer);
Matteo Martincigh9723d022019-11-13 10:56:41 +000082 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +000083 // Header will be prepended to the buffer on Commit()
84 while ( true )
Matteo Martincigh9723d022019-11-13 10:56:41 +000085 {
86 TimelinePacketStatus result = func(std::forward<Params>(params)...,
87 &m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +000088 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +000089 numberOfBytesWritten);
Keith Davis97da5e22020-03-05 16:25:28 +000090 switch ( result )
Matteo Martincigh9723d022019-11-13 10:56:41 +000091 {
Keith Davis97da5e22020-03-05 16:25:28 +000092 case TimelinePacketStatus::BufferExhaustion:
93 Commit();
94 ReserveBuffer();
95 continue;
Matteo Martincigh9723d022019-11-13 10:56:41 +000096
Keith Davis97da5e22020-03-05 16:25:28 +000097 case TimelinePacketStatus::Error:
98 throw RuntimeException("Error processing while sending TimelineBinaryPacket",
99 CHECK_LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000100
Keith Davis5238aff2020-03-11 12:17:05 +0000101 default:
102 m_Offset += numberOfBytesWritten;
Keith Davis97da5e22020-03-05 16:25:28 +0000103 m_RemainingBufferSize -= numberOfBytesWritten;
104 return;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000105 }
106 }
107 }
Keith Davis97da5e22020-03-05 16:25:28 +0000108 catch ( ... )
Matteo Martincigh9723d022019-11-13 10:56:41 +0000109 {
110 throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION());
111 }
112}
113
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100114} // namespace profiling
115
116} // namespace armnn