ArmNN
 20.02
CommandHandler.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. 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 
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 
35 {
36  m_KeepRunning.store(false);
37 
38  if (m_CommandThread.joinable())
39  {
40  m_CommandThread.join();
41  }
42 }
43 
44 void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection)
45 {
46  do
47  {
48  try
49  {
50  Packet packet = profilingConnection.ReadPacket(m_Timeout.load());
51 
52  if (packet.IsEmpty())
53  {
54  // Nothing to do, continue
55  continue;
56  }
57 
58  Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(),
59  packet.GetPacketId());
60 
61  CommandHandlerFunctor* commandHandlerFunctor =
62  m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(),
63  packet.GetPacketId(),
64  version.GetEncodedValue());
65  BOOST_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 Exception& 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  }
89  while (m_KeepRunning.load());
90 
91  m_IsRunning.store(false);
92 }
93 
94 } // namespace profiling
95 
96 } // namespace armnn
void Start(IProfilingConnection &profilingConnection)
Version ResolvePacketVersion(uint32_t familyId, uint32_t packetId) const
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
CommandHandlerFunctor * GetFunctor(uint32_t familyId, uint32_t packetId, uint32_t version) const
Copyright (c) 2020 ARM Limited.
virtual Packet ReadPacket(uint32_t timeout)=0
uint32_t GetPacketFamily() const
Definition: Packet.hpp:70
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
uint32_t GetPacketId() const
Definition: Packet.hpp:71