blob: 90016d06f3c4910ec28c11997106def857c41f4f [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)
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.
Jim Flynn1fdeb992020-07-09 07:28:37 +010039 void SendTimelineEventBinaryPacket(uint64_t timestamp, int 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.
Jim Flynn1892d212020-05-26 21:10:49 +010042 void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010043
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,
Finn Williams0a336dc2020-05-11 15:39:58 +010054 uint64_t tailGuid,
55 uint64_t attributeGuid) override;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010056private:
57 /// Reserves maximum packet size from buffer
58 void ReserveBuffer();
59
Matteo Martincigh9723d022019-11-13 10:56:41 +000060 template <typename Func, typename ... Params>
61 void ForwardWriteBinaryFunction(Func& func, Params&& ... params);
62
Keith Davis97da5e22020-03-05 16:25:28 +000063 IBufferManager& m_BufferManager;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000064 IPacketBufferPtr m_WriteBuffer;
Keith Davis97da5e22020-03-05 16:25:28 +000065 unsigned int m_Offset;
66 unsigned int m_RemainingBufferSize;
67
68 const unsigned int m_uint32_t_size = sizeof(uint32_t);
69
70 std::pair<uint32_t, uint32_t> m_PacketHeader;
71 uint32_t m_PacketDataLength;
72
73 bool m_DirectoryPackage = false;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010074};
75
Keith Davis97da5e22020-03-05 16:25:28 +000076template<typename Func, typename ... Params>
Matteo Martincigh9723d022019-11-13 10:56:41 +000077void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params)
78{
79 try
80 {
81 ReserveBuffer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010082 ARMNN_ASSERT(m_WriteBuffer);
Matteo Martincigh9723d022019-11-13 10:56:41 +000083 unsigned int numberOfBytesWritten = 0;
Keith Davis97da5e22020-03-05 16:25:28 +000084 // Header will be prepended to the buffer on Commit()
85 while ( true )
Matteo Martincigh9723d022019-11-13 10:56:41 +000086 {
87 TimelinePacketStatus result = func(std::forward<Params>(params)...,
88 &m_WriteBuffer->GetWritableData()[m_Offset],
Keith Davis97da5e22020-03-05 16:25:28 +000089 m_RemainingBufferSize,
Matteo Martincigh9723d022019-11-13 10:56:41 +000090 numberOfBytesWritten);
Keith Davis97da5e22020-03-05 16:25:28 +000091 switch ( result )
Matteo Martincigh9723d022019-11-13 10:56:41 +000092 {
Keith Davis97da5e22020-03-05 16:25:28 +000093 case TimelinePacketStatus::BufferExhaustion:
94 Commit();
95 ReserveBuffer();
96 continue;
Matteo Martincigh9723d022019-11-13 10:56:41 +000097
Keith Davis97da5e22020-03-05 16:25:28 +000098 case TimelinePacketStatus::Error:
Jim Flynn6398a982020-05-27 17:05:21 +010099 throw RuntimeException("Error processing while sending TimelineBinaryPacket", 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 }
Jim Flynn6398a982020-05-27 17:05:21 +0100108 catch (const RuntimeException& ex)
109 {
110 // don't swallow in the catch all block
111 throw ex;
112 }
113 catch (const BufferExhaustion& ex)
114 {
115 // ditto
116 throw ex;
117 }
118 catch (const Exception& ex)
119 {
120 throw ex;
121 }
Keith Davis97da5e22020-03-05 16:25:28 +0000122 catch ( ... )
Matteo Martincigh9723d022019-11-13 10:56:41 +0000123 {
Jim Flynn6398a982020-05-27 17:05:21 +0100124 throw RuntimeException("Unknown Exception thrown while sending TimelineBinaryPacket", CHECK_LOCATION());
Matteo Martincigh9723d022019-11-13 10:56:41 +0000125 }
126}
127
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100128} // namespace profiling
129
130} // namespace armnn