blob: d9722b31836816f5efae6cbc929ed2c346c4639d [file] [log] [blame]
FinnWilliamsArm4833cea2019-09-17 16:53:53 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincigh8a837172019-10-04 17:01:07 +01006#include "CommandHandler.hpp"
Colm Donelan9ea77002019-10-17 14:02:44 +01007#include "ProfilingService.hpp"
FinnWilliamsArm4833cea2019-09-17 16:53:53 +01008
Matteo Martincighd0613b52019-10-09 16:47:04 +01009#include <boost/log/trivial.hpp>
10
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010011namespace armnn
12{
13
14namespace profiling
15{
16
Matteo Martincigh8a837172019-10-04 17:01:07 +010017void CommandHandler::Start(IProfilingConnection& profilingConnection)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010018{
Matteo Martincigh88813932019-10-04 14:40:04 +010019 if (IsRunning())
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010020 {
Matteo Martincigh88813932019-10-04 14:40:04 +010021 return;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010022 }
Matteo Martincigh88813932019-10-04 14:40:04 +010023
Colm Donelan9ea77002019-10-17 14:02:44 +010024 if (m_CommandThread.joinable())
25 {
26 m_CommandThread.join();
27 }
28
Matteo Martincighc2728f92019-10-07 12:35:21 +010029 m_IsRunning.store(true);
30 m_KeepRunning.store(true);
Matteo Martincigh8a837172019-10-04 17:01:07 +010031 m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection));
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010032}
33
Matteo Martincigh8a837172019-10-04 17:01:07 +010034void CommandHandler::Stop()
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010035{
Matteo Martincighc2728f92019-10-07 12:35:21 +010036 m_KeepRunning.store(false);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010037
Matteo Martincigh88813932019-10-04 14:40:04 +010038 if (m_CommandThread.joinable())
39 {
40 m_CommandThread.join();
41 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010042}
43
Matteo Martincigh8a837172019-10-04 17:01:07 +010044void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection)
Matteo Martincigh88813932019-10-04 14:40:04 +010045{
46 do
47 {
48 try
49 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010050 Packet packet = profilingConnection.ReadPacket(m_Timeout.load());
51
52 if (packet.IsEmpty())
53 {
54 // Nothing to do, continue
55 continue;
56 }
57
Jim Flynned25e0e2019-10-18 13:21:43 +010058 Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(),
59 packet.GetPacketId());
Matteo Martincigh88813932019-10-04 14:40:04 +010060
61 CommandHandlerFunctor* commandHandlerFunctor =
Jim Flynn397043f2019-10-17 17:37:10 +010062 m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(),
63 packet.GetPacketId(),
64 version.GetEncodedValue());
Matteo Martincigh88813932019-10-04 14:40:04 +010065 BOOST_ASSERT(commandHandlerFunctor);
66 commandHandlerFunctor->operator()(packet);
67 }
68 catch (const armnn::TimeoutException&)
69 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010070 if (m_StopAfterTimeout.load())
Matteo Martincigh88813932019-10-04 14:40:04 +010071 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010072 m_KeepRunning.store(false);
Matteo Martincigh88813932019-10-04 14:40:04 +010073 }
74 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +010075 catch (const Exception& e)
Matteo Martincigh88813932019-10-04 14:40:04 +010076 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010077 // Log the error and continue
78 BOOST_LOG_TRIVIAL(warning) << "An error has occurred when handling a command: " << e.what() << std::endl;
Colm Donelan9ea77002019-10-17 14:02:44 +010079 // 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 }
Matteo Martincigh88813932019-10-04 14:40:04 +010087 }
Matteo Martincigh88813932019-10-04 14:40:04 +010088 }
Matteo Martincighc2728f92019-10-07 12:35:21 +010089 while (m_KeepRunning.load());
Matteo Martincigh88813932019-10-04 14:40:04 +010090
Matteo Martincighc2728f92019-10-07 12:35:21 +010091 m_IsRunning.store(false);
Matteo Martincigh88813932019-10-04 14:40:04 +010092}
93
94} // namespace profiling
95
96} // namespace armnn