blob: d38c1751cb052a1fef764eb807fdd3bd944ea210 [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"
Nikhil Raj77fe76b2021-06-09 14:55:32 +01008
9#include <common/include/LabelsAndEventClasses.hpp>
Jim Flynn4e755a52020-03-29 17:48:26 +010010
Jim Flynn4e755a52020-03-29 17:48:26 +010011#include <chrono>
12#include <iostream>
Jim Flynn4e755a52020-03-29 17:48:26 +010013
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000014namespace arm
Jim Flynn4e755a52020-03-29 17:48:26 +010015{
16
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000017namespace pipe
Jim Flynn4e755a52020-03-29 17:48:26 +010018{
19
20std::vector<uint32_t> TestTimelinePacketHandler::GetHeadersAccepted()
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
Jim Flynnbbfe6032020-07-20 16:57:44 +010028void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010029{
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
46void TestTimelinePacketHandler::Stop()
47{
48 m_Connection->Close();
49}
50
51void TestTimelinePacketHandler::WaitOnInferenceCompletion(unsigned int timeout)
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
71void TestTimelinePacketHandler::SetInferenceComplete()
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
Jim Flynnbbfe6032020-07-20 16:57:44 +010080void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010081{
82 m_DirectoryDecoder(packet);
83}
84
Jim Flynnbbfe6032020-07-20 16:57:44 +010085void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010086{
87 m_Decoder(packet);
88}
89
90// TimelineMessageDecoder functions
Jim Flynnbbfe6032020-07-20 16:57:44 +010091arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
Jim Flynn4e755a52020-03-29 17:48:26 +010092{
93 m_TimelineModel.AddEntity(entity.m_Guid);
Jim Flynnbbfe6032020-07-20 16:57:44 +010094 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +010095}
96
Jim Flynnbbfe6032020-07-20 16:57:44 +010097arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
98 const arm::pipe::ITimelineDecoder::EventClass& eventClass)
Jim Flynn4e755a52020-03-29 17:48:26 +010099{
Jim Flynn6398a982020-05-27 17:05:21 +0100100 m_TimelineModel.AddEventClass(eventClass);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100101 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100102}
103
Jim Flynnbbfe6032020-07-20 16:57:44 +0100104arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(
105 const arm::pipe::ITimelineDecoder::Event& event)
Jim Flynn4e755a52020-03-29 17:48:26 +0100106{
Jim Flynn6398a982020-05-27 17:05:21 +0100107 m_TimelineModel.AddEvent(event);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100108 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100109}
110
Jim Flynnbbfe6032020-07-20 16:57:44 +0100111arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(
112 const arm::pipe::ITimelineDecoder::Label& label)
Jim Flynn4e755a52020-03-29 17:48:26 +0100113{
114 m_TimelineModel.AddLabel(label);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100115 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100116}
117
Jim Flynnbbfe6032020-07-20 16:57:44 +0100118arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
119 const arm::pipe::ITimelineDecoder::Relationship& relationship)
Jim Flynn4e755a52020-03-29 17:48:26 +0100120{
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);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000128 if (attributeGuid == LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS)
Jim Flynn6398a982020-05-27 17:05:21 +0100129 {
130 if (m_PacketHandler != nullptr)
131 {
132 m_PacketHandler->SetInferenceComplete();
133 }
134 }
135 }
Jim Flynnbbfe6032020-07-20 16:57:44 +0100136 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100137}
138
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000139} // namespace pipe
Jim Flynn4e755a52020-03-29 17:48:26 +0100140
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000141} // namespace arm