• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "CommandHandler.hpp"
7 #include "ProfilingService.hpp"
8 
9 #include <armnn/Logging.hpp>
10 
11 namespace armnn
12 {
13 
14 namespace profiling
15 {
16 
Start(IProfilingConnection & profilingConnection)17 void CommandHandler::Start(IProfilingConnection& profilingConnection)
18 {
19     if (IsRunning())
20     {
21         return;
22     }
23 
24     if (m_CommandThread.joinable())
25     {
26         m_CommandThread.join();
27     }
28 
29     m_IsRunning.store(true);
30     m_KeepRunning.store(true);
31     m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection));
32 }
33 
Stop()34 void CommandHandler::Stop()
35 {
36     m_KeepRunning.store(false);
37 
38     if (m_CommandThread.joinable())
39     {
40         m_CommandThread.join();
41     }
42 }
43 
HandleCommands(IProfilingConnection & profilingConnection)44 void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection)
45 {
46     do
47     {
48         try
49         {
50             arm::pipe::Packet packet = profilingConnection.ReadPacket(m_Timeout.load());
51 
52             if (packet.IsEmpty())
53             {
54                 // Nothing to do, continue
55                 continue;
56             }
57 
58             arm::pipe::Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(),
59                                                                                       packet.GetPacketId());
60 
61             arm::pipe::CommandHandlerFunctor* commandHandlerFunctor =
62                 m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(),
63                                                     packet.GetPacketId(),
64                                                     version.GetEncodedValue());
65             ARMNN_ASSERT(commandHandlerFunctor);
66             commandHandlerFunctor->operator()(packet);
67         }
68         catch (const armnn::TimeoutException&)
69         {
70             if (m_StopAfterTimeout.load())
71             {
72                 m_KeepRunning.store(false);
73             }
74         }
75         catch (const arm::pipe::ProfilingException& e)
76         {
77             // Log the error and continue
78             ARMNN_LOG(warning) << "An error has occurred when handling a command: " << e.what();
79             // Did we get here because the socket failed?
80             if ( !profilingConnection.IsOpen() )
81             {
82                 // We're going to stop processing commands.
83                 // This will leave the thread idle. There is no mechanism to restart the profiling service when the
84                 // connection is lost.
85                 m_KeepRunning.store(false);
86             }
87         }
88         catch (const Exception& e)
89         {
90             // Log the error and continue
91             ARMNN_LOG(warning) << "An error has occurred when handling a command: " << e.what();
92             // Did we get here because the socket failed?
93             if ( !profilingConnection.IsOpen() )
94             {
95                 // We're going to stop processing commands.
96                 // This will leave the thread idle. There is no mechanism to restart the profiling service when the
97                 // connection is lost.
98                 m_KeepRunning.store(false);
99             }
100         }
101     }
102     while (m_KeepRunning.load());
103 
104     m_IsRunning.store(false);
105 }
106 
107 } // namespace profiling
108 
109 } // namespace armnn
110