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 | |
| 10 | #include <armnn/utility/IgnoreUnused.hpp> |
| 11 | |
| 12 | #include <chrono> |
| 13 | #include <iostream> |
| 14 | #include <sstream> |
| 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | namespace profiling |
| 20 | { |
| 21 | |
| 22 | std::vector<uint32_t> TestTimelinePacketHandler::GetHeadersAccepted() |
| 23 | { |
| 24 | std::vector<uint32_t> headers; |
| 25 | headers.push_back(m_DirectoryHeader); // message directory |
| 26 | headers.push_back(m_MessageHeader); // message |
| 27 | return headers; |
| 28 | } |
| 29 | |
| 30 | void TestTimelinePacketHandler::HandlePacket(const Packet& packet) |
| 31 | { |
| 32 | if (packet.GetHeader() == m_DirectoryHeader) |
| 33 | { |
| 34 | ProcessDirectoryPacket(packet); |
| 35 | } |
| 36 | else if (packet.GetHeader() == m_MessageHeader) |
| 37 | { |
| 38 | ProcessMessagePacket(packet); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | std::stringstream ss; |
| 43 | ss << "Received a packet with unknown header [" << packet.GetHeader() << "]"; |
| 44 | throw armnn::Exception(ss.str()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void TestTimelinePacketHandler::Stop() |
| 49 | { |
| 50 | m_Connection->Close(); |
| 51 | } |
| 52 | |
| 53 | void TestTimelinePacketHandler::WaitOnInferenceCompletion(unsigned int timeout) |
| 54 | { |
| 55 | std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex); |
| 56 | |
| 57 | auto start = std::chrono::high_resolution_clock::now(); |
| 58 | // Here we we will go back to sleep after a spurious wake up if |
| 59 | // m_InferenceCompleted is not yet true. |
| 60 | if (!m_InferenceCompletedConditionVariable.wait_for(lck, |
| 61 | std::chrono::milliseconds(timeout), |
| 62 | [&]{return m_InferenceCompleted == true;})) |
| 63 | { |
| 64 | auto finish = std::chrono::high_resolution_clock::now(); |
| 65 | std::chrono::duration<double, std::milli> elapsed = finish - start; |
| 66 | std::stringstream ss; |
| 67 | ss << "Timed out waiting on inference completion for " << elapsed.count() << " ms"; |
| 68 | throw armnn::TimeoutException(ss.str()); |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | void TestTimelinePacketHandler::SetInferenceComplete() |
| 74 | { |
| 75 | { // only lock when we are updating the inference completed variable |
| 76 | std::unique_lock<std::mutex> lck(m_InferenceCompletedMutex); |
| 77 | m_InferenceCompleted = true; |
| 78 | } |
| 79 | m_InferenceCompletedConditionVariable.notify_one(); |
| 80 | } |
| 81 | |
| 82 | void TestTimelinePacketHandler::ProcessDirectoryPacket(const Packet& packet) |
| 83 | { |
| 84 | m_DirectoryDecoder(packet); |
| 85 | } |
| 86 | |
| 87 | void TestTimelinePacketHandler::ProcessMessagePacket(const Packet& packet) |
| 88 | { |
| 89 | m_Decoder(packet); |
| 90 | } |
| 91 | |
| 92 | // TimelineMessageDecoder functions |
| 93 | ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity) |
| 94 | { |
| 95 | m_TimelineModel.AddEntity(entity.m_Guid); |
| 96 | return ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
| 97 | } |
| 98 | |
| 99 | ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass( |
| 100 | const ITimelineDecoder::EventClass& eventClass) |
| 101 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 102 | m_TimelineModel.AddEventClass(eventClass); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 103 | return ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
| 104 | } |
| 105 | |
| 106 | ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(const ITimelineDecoder::Event& event) |
| 107 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 108 | m_TimelineModel.AddEvent(event); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 109 | return ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
| 110 | } |
| 111 | |
| 112 | ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(const ITimelineDecoder::Label& label) |
| 113 | { |
| 114 | m_TimelineModel.AddLabel(label); |
| 115 | return ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
| 116 | } |
| 117 | |
| 118 | ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship( |
| 119 | const ITimelineDecoder::Relationship& relationship) |
| 120 | { |
| 121 | m_TimelineModel.AddRelationship(relationship); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 122 | // 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); |
| 128 | if (attributeGuid == armnn::profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS) |
| 129 | { |
| 130 | if (m_PacketHandler != nullptr) |
| 131 | { |
| 132 | m_PacketHandler->SetInferenceComplete(); |
| 133 | } |
| 134 | } |
| 135 | } |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 136 | return ITimelineDecoder::TimelineStatus::TimelineStatus_Success; |
| 137 | } |
| 138 | |
| 139 | } // namespace profiling |
| 140 | |
| 141 | } // namespace armnn |