blob: df847d4c7c2fb50ebd5f02094fbf4716337b5430 [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
Jim Flynn4e755a52020-03-29 17:48:26 +010010#include <chrono>
11#include <iostream>
Jim Flynn4e755a52020-03-29 17:48:26 +010012
13namespace armnn
14{
15
16namespace profiling
17{
18
19std::vector<uint32_t> TestTimelinePacketHandler::GetHeadersAccepted()
20{
21 std::vector<uint32_t> headers;
22 headers.push_back(m_DirectoryHeader); // message directory
23 headers.push_back(m_MessageHeader); // message
24 return headers;
25}
26
Jim Flynnbbfe6032020-07-20 16:57:44 +010027void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010028{
29 if (packet.GetHeader() == m_DirectoryHeader)
30 {
31 ProcessDirectoryPacket(packet);
32 }
33 else if (packet.GetHeader() == m_MessageHeader)
34 {
35 ProcessMessagePacket(packet);
36 }
37 else
38 {
39 std::stringstream ss;
40 ss << "Received a packet with unknown header [" << packet.GetHeader() << "]";
41 throw armnn::Exception(ss.str());
42 }
43}
44
45void TestTimelinePacketHandler::Stop()
46{
47 m_Connection->Close();
48}
49
50void TestTimelinePacketHandler::WaitOnInferenceCompletion(unsigned int timeout)
51{
52 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
53
54 auto start = std::chrono::high_resolution_clock::now();
55 // Here we we will go back to sleep after a spurious wake up if
56 // m_InferenceCompleted is not yet true.
57 if (!m_InferenceCompletedConditionVariable.wait_for(lck,
58 std::chrono::milliseconds(timeout),
59 [&]{return m_InferenceCompleted == true;}))
60 {
61 auto finish = std::chrono::high_resolution_clock::now();
62 std::chrono::duration<double, std::milli> elapsed = finish - start;
63 std::stringstream ss;
64 ss << "Timed out waiting on inference completion for " << elapsed.count() << " ms";
65 throw armnn::TimeoutException(ss.str());
66 }
67 return;
68}
69
70void TestTimelinePacketHandler::SetInferenceComplete()
71{
72 { // only lock when we are updating the inference completed variable
73 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
74 m_InferenceCompleted = true;
75 }
76 m_InferenceCompletedConditionVariable.notify_one();
77}
78
Jim Flynnbbfe6032020-07-20 16:57:44 +010079void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010080{
81 m_DirectoryDecoder(packet);
82}
83
Jim Flynnbbfe6032020-07-20 16:57:44 +010084void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010085{
86 m_Decoder(packet);
87}
88
89// TimelineMessageDecoder functions
Jim Flynnbbfe6032020-07-20 16:57:44 +010090arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
Jim Flynn4e755a52020-03-29 17:48:26 +010091{
92 m_TimelineModel.AddEntity(entity.m_Guid);
Jim Flynnbbfe6032020-07-20 16:57:44 +010093 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +010094}
95
Jim Flynnbbfe6032020-07-20 16:57:44 +010096arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
97 const arm::pipe::ITimelineDecoder::EventClass& eventClass)
Jim Flynn4e755a52020-03-29 17:48:26 +010098{
Jim Flynn6398a982020-05-27 17:05:21 +010099 m_TimelineModel.AddEventClass(eventClass);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100100 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100101}
102
Jim Flynnbbfe6032020-07-20 16:57:44 +0100103arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(
104 const arm::pipe::ITimelineDecoder::Event& event)
Jim Flynn4e755a52020-03-29 17:48:26 +0100105{
Jim Flynn6398a982020-05-27 17:05:21 +0100106 m_TimelineModel.AddEvent(event);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100107 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100108}
109
Jim Flynnbbfe6032020-07-20 16:57:44 +0100110arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(
111 const arm::pipe::ITimelineDecoder::Label& label)
Jim Flynn4e755a52020-03-29 17:48:26 +0100112{
113 m_TimelineModel.AddLabel(label);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100114 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100115}
116
Jim Flynnbbfe6032020-07-20 16:57:44 +0100117arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
118 const arm::pipe::ITimelineDecoder::Relationship& relationship)
Jim Flynn4e755a52020-03-29 17:48:26 +0100119{
120 m_TimelineModel.AddRelationship(relationship);
Jim Flynn6398a982020-05-27 17:05:21 +0100121 // check to see if this is an execution link to an inference of event class end of life
122 // if so the inference has completed so send out a notification...
123 if (relationship.m_RelationshipType == RelationshipType::ExecutionLink &&
124 m_TimelineModel.IsInferenceGuid(relationship.m_HeadGuid))
125 {
126 ProfilingStaticGuid attributeGuid(relationship.m_AttributeGuid);
127 if (attributeGuid == armnn::profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS)
128 {
129 if (m_PacketHandler != nullptr)
130 {
131 m_PacketHandler->SetInferenceComplete();
132 }
133 }
134 }
Jim Flynnbbfe6032020-07-20 16:57:44 +0100135 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100136}
137
138} // namespace profiling
139
140} // namespace armnn