blob: fc9cf6fd25073afee719932d394a0a23cddecca4 [file] [log] [blame]
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01001//
Jim Flynn1fdeb992020-07-09 07:28:37 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01003// 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)
Jim Flynn6e330412021-09-06 17:11:01 +010030 , m_PacketDataLength(0u)
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010031 {}
32
33 /// Commits the current buffer and reset the member variables
34 void Commit() override;
35
36 /// Create and write a TimelineEntityBinaryPacket from the parameters to the buffer.
37 void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override;
38
39 /// Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
Jim Flynn1fdeb992020-07-09 07:28:37 +010040 void SendTimelineEventBinaryPacket(uint64_t timestamp, int threadId, uint64_t profilingGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010041
42 /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
Jim Flynn1892d212020-05-26 21:10:49 +010043 void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010044
45 /// Create and write a TimelineLabelBinaryPacket from the parameters to the buffer.
46 void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string& label) override;
47
48 /// Create and write a TimelineMessageDirectoryPackage in the buffer
49 void SendTimelineMessageDirectoryPackage() override;
50
51 /// Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer.
52 virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType,
53 uint64_t relationshipGuid,
54 uint64_t headGuid,
Finn Williams0a336dc2020-05-11 15:39:58 +010055 uint64_t tailGuid,
56 uint64_t attributeGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010057private:
58 /// Reserves maximum packet size from buffer
59 void ReserveBuffer();
60
Matteo Martincigh9723d022019-11-13 10:56:41 +000061 template <typename Func, typename ... Params>
62 void ForwardWriteBinaryFunction(Func& func, Params&& ... params);
63
Keith Davis97da5e22020-03-05 16:25:28 +000064 IBufferManager& m_BufferManager;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000065 IPacketBufferPtr m_WriteBuffer;
Keith Davis97da5e22020-03-05 16:25:28 +000066 unsigned int m_Offset;
67 unsigned int m_RemainingBufferSize;
68
69 const unsigned int m_uint32_t_size = sizeof(uint32_t);
70
71 std::pair<uint32_t, uint32_t> m_PacketHeader;
72 uint32_t m_PacketDataLength;
73
74 bool m_DirectoryPackage = false;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010075};
76
Keith Davis97da5e22020-03-05 16:25:28 +000077template<typename Func, typename ... Params>
Matteo Martincigh9723d022019-11-13 10:56:41 +000078void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params)
79{
80 try
81 {
82 ReserveBuffer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010083 ARMNN_ASSERT(m_WriteBuffer);
Matteo Martincigh9723d022019-11-13 10:56:41 +000084 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +000085 // Header will be prepended to the buffer on Commit()
86 while ( true )
Matteo Martincigh9723d022019-11-13 10:56:41 +000087 {
88 TimelinePacketStatus result = func(std::forward<Params>(params)...,
89 &m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +000090 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +000091 numberOfBytesWritten);
Keith Davis97da5e22020-03-05 16:25:28 +000092 switch ( result )
Matteo Martincigh9723d022019-11-13 10:56:41 +000093 {
Keith Davis97da5e22020-03-05 16:25:28 +000094 case TimelinePacketStatus::BufferExhaustion:
95 Commit();
96 ReserveBuffer();
97 continue;
Matteo Martincigh9723d022019-11-13 10:56:41 +000098
Keith Davis97da5e22020-03-05 16:25:28 +000099 case TimelinePacketStatus::Error:
Jim Flynn6398a982020-05-27 17:05:21 +0100100 throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000101
Keith Davis5238aff2020-03-11 12:17:05 +0000102 default:
103 m_Offset += numberOfBytesWritten;
Keith Davis97da5e22020-03-05 16:25:28 +0000104 m_RemainingBufferSize -= numberOfBytesWritten;
105 return;
Matteo Martincigh9723d022019-11-13 10:56:41 +0000106 }
107 }
108 }
Jim Flynn6398a982020-05-27 17:05:21 +0100109 catch (const RuntimeException& ex)
110 {
111 // don't swallow in the catch all block
112 throw ex;
113 }
114 catch (const BufferExhaustion& ex)
115 {
116 // ditto
117 throw ex;
118 }
119 catch (const Exception& ex)
120 {
121 throw ex;
122 }
Keith Davis97da5e22020-03-05 16:25:28 +0000123 catch ( ... )
Matteo Martincigh9723d022019-11-13 10:56:41 +0000124 {
Jim Flynn6398a982020-05-27 17:05:21 +0100125 throw RuntimeException("Unknown Exception thrown while sending TimelineBinaryPacket", CHECK_LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000126 }
127}
128
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100129} // namespace profiling
130
131} // namespace armnn