ArmNN
 22.02
SendTimelinePacket.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "IBufferManager.hpp"
10 #include "ProfilingUtils.hpp"
11 
12 #include <armnn/utility/Assert.hpp>
13 
14 #include <memory>
15 
16 namespace armnn
17 {
18 
19 namespace profiling
20 {
21 
23 {
24 public:
26  : m_BufferManager(bufferManager)
27  , m_WriteBuffer(nullptr)
28  , m_Offset(8u)
29  , m_RemainingBufferSize(0u)
30  , m_PacketDataLength(0u)
31  {}
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.
40  void SendTimelineEventBinaryPacket(uint64_t timestamp, int threadId, uint64_t profilingGuid) override;
41 
42  /// Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.
43  void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override;
44 
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
50 
51  /// Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer.
53  uint64_t relationshipGuid,
54  uint64_t headGuid,
55  uint64_t tailGuid,
56  uint64_t attributeGuid) override;
57 private:
58  /// Reserves maximum packet size from buffer
59  void ReserveBuffer();
60 
61  template <typename Func, typename ... Params>
62  void ForwardWriteBinaryFunction(Func& func, Params&& ... params);
63 
64  IBufferManager& m_BufferManager;
65  IPacketBufferPtr m_WriteBuffer;
66  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;
75 };
76 
77 template<typename Func, typename ... Params>
78 void SendTimelinePacket::ForwardWriteBinaryFunction(Func& func, Params&& ... params)
79 {
80  try
81  {
82  ReserveBuffer();
83  ARMNN_ASSERT(m_WriteBuffer);
84  unsigned int numberOfBytesWritten = 0;
85  // Header will be prepended to the buffer on Commit()
86  while ( true )
87  {
88  TimelinePacketStatus result = func(std::forward<Params>(params)...,
89  &m_WriteBuffer->GetWritableData()[m_Offset],
90  m_RemainingBufferSize,
91  numberOfBytesWritten);
92  switch ( result )
93  {
95  Commit();
96  ReserveBuffer();
97  continue;
98 
100  throw RuntimeException("Error processing while sending TimelineBinaryPacket", CHECK_LOCATION());
101 
102  default:
103  m_Offset += numberOfBytesWritten;
104  m_RemainingBufferSize -= numberOfBytesWritten;
105  return;
106  }
107  }
108  }
109  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  }
123  catch ( ... )
124  {
125  throw RuntimeException("Unknown Exception thrown while sending TimelineBinaryPacket", CHECK_LOCATION());
126  }
127 }
128 
129 } // namespace profiling
130 
131 } // namespace armnn
void SendTimelineMessageDirectoryPackage() override
Create and write a TimelineMessageDirectoryPackage in the buffer.
void SendTimelineLabelBinaryPacket(uint64_t profilingGuid, const std::string &label) override
Create and write a TimelineLabelBinaryPacket from the parameters to the buffer.
void SendTimelineEntityBinaryPacket(uint64_t profilingGuid) override
Create and write a TimelineEntityBinaryPacket from the parameters to the buffer.
Copyright (c) 2021 ARM Limited and Contributors.
virtual void SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType relationshipType, uint64_t relationshipGuid, uint64_t headGuid, uint64_t tailGuid, uint64_t attributeGuid) override
Create and write a TimelineRelationshipBinaryPacket from the parameters to the buffer.
void Commit() override
Commits the current buffer and reset the member variables.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
#define CHECK_LOCATION()
Definition: Exceptions.hpp:209
void SendTimelineEventBinaryPacket(uint64_t timestamp, int threadId, uint64_t profilingGuid) override
Create and write a TimelineEventBinaryPacket from the parameters to the buffer.
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
SendTimelinePacket(IBufferManager &bufferManager)
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
void SendTimelineEventClassBinaryPacket(uint64_t profilingGuid, uint64_t nameGuid) override
Create and write a TimelineEventClassBinaryPacket from the parameters to the buffer.