blob: ccb806b2380379009c533ec00119dabefc25f26d [file] [log] [blame]
Jim Flynn4e755a52020-03-29 17:48:26 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Jim Flynn4e755a52020-03-29 17:48:26 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "TestTimelinePacketHandler.hpp"
7#include "IProfilingConnection.hpp"
Jim Flynn6398a982020-05-27 17:05:21 +01008#include <LabelsAndEventClasses.hpp>
Jim Flynn4e755a52020-03-29 17:48:26 +01009
10#include <armnn/utility/IgnoreUnused.hpp>
11
12#include <chrono>
13#include <iostream>
14#include <sstream>
15
16namespace armnn
17{
18
19namespace profiling
20{
21
22std::vector<uint32_t> TestTimelinePacketHandler::GetHeadersAccepted()
23{
24 std::vector<uint32_t> headers;
25 headers.push_back(m_DirectoryHeader); // message directory
26 headers.push_back(m_MessageHeader); // message
27 return headers;
28}
29
30void TestTimelinePacketHandler::HandlePacket(const Packet& packet)
31{
32 if (packet.GetHeader() == m_DirectoryHeader)
33 {
34 ProcessDirectoryPacket(packet);
35 }
36 else if (packet.GetHeader() == m_MessageHeader)
37 {
38 ProcessMessagePacket(packet);
39 }
40 else
41 {
42 std::stringstream ss;
43 ss << "Received a packet with unknown header [" << packet.GetHeader() << "]";
44 throw armnn::Exception(ss.str());
45 }
46}
47
48void TestTimelinePacketHandler::Stop()
49{
50 m_Connection->Close();
51}
52
53void TestTimelinePacketHandler::WaitOnInferenceCompletion(unsigned int timeout)
54{
55 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
56
57 auto start = std::chrono::high_resolution_clock::now();
58 // Here we we will go back to sleep after a spurious wake up if
59 // m_InferenceCompleted is not yet true.
60 if (!m_InferenceCompletedConditionVariable.wait_for(lck,
61 std::chrono::milliseconds(timeout),
62 [&]{return m_InferenceCompleted == true;}))
63 {
64 auto finish = std::chrono::high_resolution_clock::now();
65 std::chrono::duration<double, std::milli> elapsed = finish - start;
66 std::stringstream ss;
67 ss << "Timed out waiting on inference completion for " << elapsed.count() << " ms";
68 throw armnn::TimeoutException(ss.str());
69 }
70 return;
71}
72
73void TestTimelinePacketHandler::SetInferenceComplete()
74{
75 { // only lock when we are updating the inference completed variable
76 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
77 m_InferenceCompleted = true;
78 }
79 m_InferenceCompletedConditionVariable.notify_one();
80}
81
82void TestTimelinePacketHandler::ProcessDirectoryPacket(const Packet& packet)
83{
84 m_DirectoryDecoder(packet);
85}
86
87void TestTimelinePacketHandler::ProcessMessagePacket(const Packet& packet)
88{
89 m_Decoder(packet);
90}
91
92// TimelineMessageDecoder functions
93ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
94{
95 m_TimelineModel.AddEntity(entity.m_Guid);
96 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
97}
98
99ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
100 const ITimelineDecoder::EventClass& eventClass)
101{
Jim Flynn6398a982020-05-27 17:05:21 +0100102 m_TimelineModel.AddEventClass(eventClass);
Jim Flynn4e755a52020-03-29 17:48:26 +0100103 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
104}
105
106ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(const ITimelineDecoder::Event& event)
107{
Jim Flynn6398a982020-05-27 17:05:21 +0100108 m_TimelineModel.AddEvent(event);
Jim Flynn4e755a52020-03-29 17:48:26 +0100109 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
110}
111
112ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(const ITimelineDecoder::Label& label)
113{
114 m_TimelineModel.AddLabel(label);
115 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
116}
117
118ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
119 const ITimelineDecoder::Relationship& relationship)
120{
121 m_TimelineModel.AddRelationship(relationship);
Jim Flynn6398a982020-05-27 17:05:21 +0100122 // 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 }
Jim Flynn4e755a52020-03-29 17:48:26 +0100136 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
137}
138
139} // namespace profiling
140
141} // namespace armnn