blob: 7e94af9e7ce442b788684c0cbbdc0d3bf3f7ad04 [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"
Jim Flynn3e9bc192022-03-23 23:01:26 +00007
8#include <client/src/IProfilingConnection.hpp>
Nikhil Raj77fe76b2021-06-09 14:55:32 +01009
10#include <common/include/LabelsAndEventClasses.hpp>
Jim Flynn4e755a52020-03-29 17:48:26 +010011
Jim Flynn4e755a52020-03-29 17:48:26 +010012#include <chrono>
13#include <iostream>
Jim Flynn4e755a52020-03-29 17:48:26 +010014
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace arm
Jim Flynn4e755a52020-03-29 17:48:26 +010016{
17
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000018namespace pipe
Jim Flynn4e755a52020-03-29 17:48:26 +010019{
20
21std::vector<uint32_t> TestTimelinePacketHandler::GetHeadersAccepted()
22{
23 std::vector<uint32_t> headers;
24 headers.push_back(m_DirectoryHeader); // message directory
25 headers.push_back(m_MessageHeader); // message
26 return headers;
27}
28
Jim Flynnbbfe6032020-07-20 16:57:44 +010029void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010030{
31 if (packet.GetHeader() == m_DirectoryHeader)
32 {
33 ProcessDirectoryPacket(packet);
34 }
35 else if (packet.GetHeader() == m_MessageHeader)
36 {
37 ProcessMessagePacket(packet);
38 }
39 else
40 {
41 std::stringstream ss;
42 ss << "Received a packet with unknown header [" << packet.GetHeader() << "]";
Jim Flynnf9db3ef2022-03-08 21:23:44 +000043 throw arm::pipe::ProfilingException(ss.str());
Jim Flynn4e755a52020-03-29 17:48:26 +010044 }
45}
46
47void TestTimelinePacketHandler::Stop()
48{
49 m_Connection->Close();
50}
51
52void TestTimelinePacketHandler::WaitOnInferenceCompletion(unsigned int timeout)
53{
54 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
55
56 auto start = std::chrono::high_resolution_clock::now();
57 // Here we we will go back to sleep after a spurious wake up if
58 // m_InferenceCompleted is not yet true.
59 if (!m_InferenceCompletedConditionVariable.wait_for(lck,
60 std::chrono::milliseconds(timeout),
61 [&]{return m_InferenceCompleted == true;}))
62 {
63 auto finish = std::chrono::high_resolution_clock::now();
64 std::chrono::duration<double, std::milli> elapsed = finish - start;
65 std::stringstream ss;
66 ss << "Timed out waiting on inference completion for " << elapsed.count() << " ms";
Jim Flynnf9db3ef2022-03-08 21:23:44 +000067 throw arm::pipe::TimeoutException(ss.str());
Jim Flynn4e755a52020-03-29 17:48:26 +010068 }
69 return;
70}
71
72void TestTimelinePacketHandler::SetInferenceComplete()
73{
74 { // only lock when we are updating the inference completed variable
75 std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex);
76 m_InferenceCompleted = true;
77 }
78 m_InferenceCompletedConditionVariable.notify_one();
79}
80
Jim Flynnbbfe6032020-07-20 16:57:44 +010081void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010082{
83 m_DirectoryDecoder(packet);
84}
85
Jim Flynnbbfe6032020-07-20 16:57:44 +010086void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet)
Jim Flynn4e755a52020-03-29 17:48:26 +010087{
88 m_Decoder(packet);
89}
90
91// TimelineMessageDecoder functions
Jim Flynnbbfe6032020-07-20 16:57:44 +010092arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
Jim Flynn4e755a52020-03-29 17:48:26 +010093{
94 m_TimelineModel.AddEntity(entity.m_Guid);
Jim Flynnbbfe6032020-07-20 16:57:44 +010095 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +010096}
97
Jim Flynnbbfe6032020-07-20 16:57:44 +010098arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
99 const arm::pipe::ITimelineDecoder::EventClass& eventClass)
Jim Flynn4e755a52020-03-29 17:48:26 +0100100{
Jim Flynn6398a982020-05-27 17:05:21 +0100101 m_TimelineModel.AddEventClass(eventClass);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100102 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100103}
104
Jim Flynnbbfe6032020-07-20 16:57:44 +0100105arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(
106 const arm::pipe::ITimelineDecoder::Event& event)
Jim Flynn4e755a52020-03-29 17:48:26 +0100107{
Jim Flynn6398a982020-05-27 17:05:21 +0100108 m_TimelineModel.AddEvent(event);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100109 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100110}
111
Jim Flynnbbfe6032020-07-20 16:57:44 +0100112arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(
113 const arm::pipe::ITimelineDecoder::Label& label)
Jim Flynn4e755a52020-03-29 17:48:26 +0100114{
115 m_TimelineModel.AddLabel(label);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100116 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100117}
118
Jim Flynnbbfe6032020-07-20 16:57:44 +0100119arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
120 const arm::pipe::ITimelineDecoder::Relationship& relationship)
Jim Flynn4e755a52020-03-29 17:48:26 +0100121{
122 m_TimelineModel.AddRelationship(relationship);
Jim Flynn6398a982020-05-27 17:05:21 +0100123 // check to see if this is an execution link to an inference of event class end of life
124 // if so the inference has completed so send out a notification...
125 if (relationship.m_RelationshipType == RelationshipType::ExecutionLink &&
126 m_TimelineModel.IsInferenceGuid(relationship.m_HeadGuid))
127 {
128 ProfilingStaticGuid attributeGuid(relationship.m_AttributeGuid);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000129 if (attributeGuid == LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS)
Jim Flynn6398a982020-05-27 17:05:21 +0100130 {
131 if (m_PacketHandler != nullptr)
132 {
133 m_PacketHandler->SetInferenceComplete();
134 }
135 }
136 }
Jim Flynnbbfe6032020-07-20 16:57:44 +0100137 return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
Jim Flynn4e755a52020-03-29 17:48:26 +0100138}
139
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000140} // namespace pipe
Jim Flynn4e755a52020-03-29 17:48:26 +0100141
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000142} // namespace arm