ArmNN
 22.02
TestTimelinePacketHandler.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
8 
9 #include <common/include/LabelsAndEventClasses.hpp>
10 
11 #include <chrono>
12 #include <iostream>
13 
14 namespace armnn
15 {
16 
17 namespace profiling
18 {
19 
21 {
22  std::vector<uint32_t> headers;
23  headers.push_back(m_DirectoryHeader); // message directory
24  headers.push_back(m_MessageHeader); // message
25  return headers;
26 }
27 
28 void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet)
29 {
30  if (packet.GetHeader() == m_DirectoryHeader)
31  {
32  ProcessDirectoryPacket(packet);
33  }
34  else if (packet.GetHeader() == m_MessageHeader)
35  {
36  ProcessMessagePacket(packet);
37  }
38  else
39  {
40  std::stringstream ss;
41  ss << "Received a packet with unknown header [" << packet.GetHeader() << "]";
42  throw armnn::Exception(ss.str());
43  }
44 }
45 
47 {
48  m_Connection->Close();
49 }
50 
52 {
53  std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
54 
55  auto start = std::chrono::high_resolution_clock::now();
56  // Here we we will go back to sleep after a spurious wake up if
57  // m_InferenceCompleted is not yet true.
58  if (!m_InferenceCompletedConditionVariable.wait_for(lck,
59  std::chrono::milliseconds(timeout),
60  [&]{return m_InferenceCompleted == true;}))
61  {
62  auto finish = std::chrono::high_resolution_clock::now();
63  std::chrono::duration<double, std::milli> elapsed = finish - start;
64  std::stringstream ss;
65  ss << "Timed out waiting on inference completion for " << elapsed.count() << " ms";
66  throw armnn::TimeoutException(ss.str());
67  }
68  return;
69 }
70 
72 {
73  { // only lock when we are updating the inference completed variable
74  std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
75  m_InferenceCompleted = true;
76  }
77  m_InferenceCompletedConditionVariable.notify_one();
78 }
79 
80 void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet)
81 {
82  m_DirectoryDecoder(packet);
83 }
84 
85 void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet)
86 {
87  m_Decoder(packet);
88 }
89 
90 // TimelineMessageDecoder functions
91 arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
92 {
93  m_TimelineModel.AddEntity(entity.m_Guid);
94  return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
95 }
96 
97 arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
98  const arm::pipe::ITimelineDecoder::EventClass& eventClass)
99 {
100  m_TimelineModel.AddEventClass(eventClass);
101  return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
102 }
103 
104 arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(
105  const arm::pipe::ITimelineDecoder::Event& event)
106 {
107  m_TimelineModel.AddEvent(event);
108  return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
109 }
110 
111 arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(
112  const arm::pipe::ITimelineDecoder::Label& label)
113 {
114  m_TimelineModel.AddLabel(label);
115  return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
116 }
117 
118 arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
119  const arm::pipe::ITimelineDecoder::Relationship& relationship)
120 {
121  m_TimelineModel.AddRelationship(relationship);
122  // check to see if this is an execution link to an inference of event class end of life
123  // if so the inference has completed so send out a notification...
124  if (relationship.m_RelationshipType == RelationshipType::ExecutionLink &&
125  m_TimelineModel.IsInferenceGuid(relationship.m_HeadGuid))
126  {
127  ProfilingStaticGuid attributeGuid(relationship.m_AttributeGuid);
128  if (attributeGuid == armnn::profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS)
129  {
130  if (m_PacketHandler != nullptr)
131  {
132  m_PacketHandler->SetInferenceComplete();
133  }
134  }
135  }
136  return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
137 }
138 
139 } // namespace profiling
140 
141 } // namespace armnn
void AddLabel(const arm::pipe::ITimelineDecoder::Label &label)
bool IsInferenceGuid(uint64_t guid) const
Copyright (c) 2021 ARM Limited and Contributors.
void AddEventClass(const arm::pipe::ITimelineDecoder::EventClass &eventClass)
virtual std::vector< uint32_t > GetHeadersAccepted() override
void AddEvent(const arm::pipe::ITimelineDecoder::Event &event)
virtual TimelineStatus CreateLabel(const Label &) override
virtual TimelineStatus CreateEventClass(const EventClass &) override
virtual TimelineStatus CreateEvent(const Event &) override
virtual TimelineStatus CreateRelationship(const Relationship &) override
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
virtual void HandlePacket(const arm::pipe::Packet &packet) override
process the packet
void AddRelationship(const arm::pipe::ITimelineDecoder::Relationship &relationship)
virtual TimelineStatus CreateEntity(const Entity &) override