blob: 93fb4b433c7a84e4253881dfd835385648440796 [file] [log] [blame]
Jim Flynn4e755a52020-03-29 17:48:26 +01001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "TestTimelinePacketHandler.hpp"
7#include "IProfilingConnection.hpp"
8
9#include <armnn/utility/IgnoreUnused.hpp>
10
11#include <chrono>
12#include <iostream>
13#include <sstream>
14
15namespace armnn
16{
17
18namespace profiling
19{
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
29void TestTimelinePacketHandler::HandlePacket(const Packet& packet)
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() << "]";
43 throw armnn::Exception(ss.str());
44 }
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";
67 throw armnn::TimeoutException(ss.str());
68 }
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
81void TestTimelinePacketHandler::ProcessDirectoryPacket(const Packet& packet)
82{
83 m_DirectoryDecoder(packet);
84}
85
86void TestTimelinePacketHandler::ProcessMessagePacket(const Packet& packet)
87{
88 m_Decoder(packet);
89}
90
91// TimelineMessageDecoder functions
92ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEntity(const Entity& entity)
93{
94 m_TimelineModel.AddEntity(entity.m_Guid);
95 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
96}
97
98ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEventClass(
99 const ITimelineDecoder::EventClass& eventClass)
100{
101 // for the moment terminate the run here so we can get this code
102 // onto master prior to a major re-organisation
103 if (m_PacketHandler != nullptr)
104 {
105 m_PacketHandler->SetInferenceComplete();
106 }
107 IgnoreUnused(eventClass);
108 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
109}
110
111ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateEvent(const ITimelineDecoder::Event& event)
112{
113 IgnoreUnused(event);
114 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
115}
116
117ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateLabel(const ITimelineDecoder::Label& label)
118{
119 m_TimelineModel.AddLabel(label);
120 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
121}
122
123ITimelineDecoder::TimelineStatus TimelineMessageDecoder::CreateRelationship(
124 const ITimelineDecoder::Relationship& relationship)
125{
126 m_TimelineModel.AddRelationship(relationship);
127 return ITimelineDecoder::TimelineStatus::TimelineStatus_Success;
128}
129
130} // namespace profiling
131
132} // namespace armnn