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" |
| 7 | #include "IProfilingConnection.hpp" |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 8 | #include <LabelsAndEventClasses.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 9 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <iostream> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | |
| 16 | namespace profiling |
| 17 | { |
| 18 | |
| 19 | std::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 Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 27 | void TestTimelinePacketHandler::HandlePacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 28 | { |
| 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 | |
| 45 | void TestTimelinePacketHandler::Stop() |
| 46 | { |
| 47 | m_Connection->Close(); |
| 48 | } |
| 49 | |
| 50 | void 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 | |
| 70 | void 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 Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 79 | void TestTimelinePacketHandler::ProcessDirectoryPacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 80 | { |
| 81 | m_DirectoryDecoder(packet); |
| 82 | } |
| 83 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 84 | void TestTimelinePacketHandler::ProcessMessagePacket(const arm::pipe::Packet& packet) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 85 | { |
| 86 | m_Decoder(packet); |
| 87 | } |
| 88 | |
| 89 | // TimelineMessageDecoder functions |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 90 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 91 | { |
| 92 | m_TimelineModel.AddEntity(entity.m_Guid); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 93 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 96 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass( |
| 97 | const arm::pipe::ITimelineDecoder::EventClass& eventClass) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 98 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 99 | m_TimelineModel.AddEventClass(eventClass); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 100 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 103 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent( |
| 104 | const arm::pipe::ITimelineDecoder::Event& event) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 105 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 106 | m_TimelineModel.AddEvent(event); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 107 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 110 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel( |
| 111 | const arm::pipe::ITimelineDecoder::Label& label) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 112 | { |
| 113 | m_TimelineModel.AddLabel(label); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 114 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 117 | arm::pipe::ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship( |
| 118 | const arm::pipe::ITimelineDecoder::Relationship& relationship) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 119 | { |
| 120 | m_TimelineModel.AddRelationship(relationship); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 121 | // 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 Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 135 | return arm::pipe::ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace profiling |
| 139 | |
| 140 | } // namespace armnn |