Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TestTimelinePacketHandler.hpp" |
Jim Flynn | 3e9bc19 | 2022-03-23 23:01:26 +0000 | [diff] [blame] | 7 | |
| 8 | #include <client/src/IProfilingConnection.hpp> |
Nikhil Raj | 77fe76b | 2021-06-09 14:55:32 +0100 | [diff] [blame] | 9 | |
| 10 | #include <common/include/LabelsAndEventClasses.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 11 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 12 | #include <chrono> |
| 13 | #include <iostream> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 14 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 15 | namespace arm |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 16 | { |
| 17 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 18 | namespace pipe |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 19 | { |
| 20 | |
| 21 | std::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 Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 29 | void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 30 | { |
| 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 Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 43 | throw arm::pipe::ProfilingException(ss.str()); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | void TestTimelinePacketHandler::Stop() |
| 48 | { |
| 49 | m_Connection->Close(); |
| 50 | } |
| 51 | |
| 52 | void 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 Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 67 | throw arm::pipe::TimeoutException(ss.str()); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 68 | } |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | void 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 Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 81 | void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 82 | { |
| 83 | m_DirectoryDecoder(packet); |
| 84 | } |
| 85 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 86 | void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 87 | { |
| 88 | m_Decoder(packet); |
| 89 | } |
| 90 | |
| 91 | // TimelineMessageDecoder functions |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 92 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 93 | { |
| 94 | m_TimelineModel.AddEntity(entity.m_Guid); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 95 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 98 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass( |
| 99 | const arm::pipe::ITimelineDecoder::EventClass& eventClass) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 100 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 101 | m_TimelineModel.AddEventClass(eventClass); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 102 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 105 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent( |
| 106 | const arm::pipe::ITimelineDecoder::Event& event) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 107 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 108 | m_TimelineModel.AddEvent(event); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 109 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 112 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel( |
| 113 | const arm::pipe::ITimelineDecoder::Label& label) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 114 | { |
| 115 | m_TimelineModel.AddLabel(label); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 116 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 119 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship( |
| 120 | const arm::pipe::ITimelineDecoder::Relationship& relationship) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 121 | { |
| 122 | m_TimelineModel.AddRelationship(relationship); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 123 | // 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 Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 129 | if (attributeGuid == LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS) |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 130 | { |
| 131 | if (m_PacketHandler != nullptr) |
| 132 | { |
| 133 | m_PacketHandler->SetInferenceComplete(); |
| 134 | } |
| 135 | } |
| 136 | } |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 137 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 140 | } // namespace pipe |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 141 | |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 142 | } // namespace arm |