blob: b5a7551d9a62e6a7e6f571ce1604dd1859a69371 [file] [log] [blame]
FinnWilliamsArm4833cea2019-09-17 16:53:53 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
FinnWilliamsArm4833cea2019-09-17 16:53:53 +01003// 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
Jim Flynn6c9f17d2022-03-10 23:13:01 +00009#include <common/include/Logging.hpp>
Matteo Martincighd0613b52019-10-09 16:47:04 +010010
Jim Flynne195a042022-04-12 17:19:28 +010011#if defined(ARMNN_DISABLE_THREADS)
12#include <common/include/IgnoreUnused.hpp>
13#endif
14
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace arm
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010016{
17
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000018namespace pipe
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010019{
20
Matteo Martincigh8a837172019-10-04 17:01:07 +010021void CommandHandler::Start(IProfilingConnection& profilingConnection)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010022{
Matteo Martincigh88813932019-10-04 14:40:04 +010023 if (IsRunning())
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010024 {
Matteo Martincigh88813932019-10-04 14:40:04 +010025 return;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010026 }
Matteo Martincigh88813932019-10-04 14:40:04 +010027
Jim Flynne195a042022-04-12 17:19:28 +010028#if !defined(ARMNN_DISABLE_THREADS)
Colm Donelan9ea77002019-10-17 14:02:44 +010029 if (m_CommandThread.joinable())
30 {
31 m_CommandThread.join();
32 }
Jim Flynne195a042022-04-12 17:19:28 +010033#endif
Colm Donelan9ea77002019-10-17 14:02:44 +010034
Matteo Martincighc2728f92019-10-07 12:35:21 +010035 m_IsRunning.store(true);
36 m_KeepRunning.store(true);
Jim Flynne195a042022-04-12 17:19:28 +010037#if !defined(ARMNN_DISABLE_THREADS)
Matteo Martincigh8a837172019-10-04 17:01:07 +010038 m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection));
Jim Flynne195a042022-04-12 17:19:28 +010039#else
40 IgnoreUnused(profilingConnection);
41#endif
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010042}
43
Matteo Martincigh8a837172019-10-04 17:01:07 +010044void CommandHandler::Stop()
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010045{
Matteo Martincighc2728f92019-10-07 12:35:21 +010046 m_KeepRunning.store(false);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010047
Jim Flynne195a042022-04-12 17:19:28 +010048#if !defined(ARMNN_DISABLE_THREADS)
Matteo Martincigh88813932019-10-04 14:40:04 +010049 if (m_CommandThread.joinable())
50 {
51 m_CommandThread.join();
52 }
Jim Flynne195a042022-04-12 17:19:28 +010053#endif
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010054}
55
Matteo Martincigh8a837172019-10-04 17:01:07 +010056void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection)
Matteo Martincigh88813932019-10-04 14:40:04 +010057{
58 do
59 {
60 try
61 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010062 arm::pipe::Packet packet = profilingConnection.ReadPacket(m_Timeout.load());
Matteo Martincighd0613b52019-10-09 16:47:04 +010063
64 if (packet.IsEmpty())
65 {
66 // Nothing to do, continue
67 continue;
68 }
69
Jim Flynnbbfe6032020-07-20 16:57:44 +010070 arm::pipe::Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(),
71 packet.GetPacketId());
Matteo Martincigh88813932019-10-04 14:40:04 +010072
Jim Flynnbbfe6032020-07-20 16:57:44 +010073 arm::pipe::CommandHandlerFunctor* commandHandlerFunctor =
74 m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(),
75 packet.GetPacketId(),
Jim Flynn397043f2019-10-17 17:37:10 +010076 version.GetEncodedValue());
Jim Flynn6730fe92022-03-10 22:57:47 +000077 ARM_PIPE_ASSERT(commandHandlerFunctor);
Matteo Martincigh88813932019-10-04 14:40:04 +010078 commandHandlerFunctor->operator()(packet);
79 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +000080 catch (const arm::pipe::TimeoutException&)
Matteo Martincigh88813932019-10-04 14:40:04 +010081 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010082 if (m_StopAfterTimeout.load())
Matteo Martincigh88813932019-10-04 14:40:04 +010083 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010084 m_KeepRunning.store(false);
Matteo Martincigh88813932019-10-04 14:40:04 +010085 }
86 }
Jim Flynnbbfe6032020-07-20 16:57:44 +010087 catch (const arm::pipe::ProfilingException& e)
88 {
89 // Log the error and continue
Jim Flynn6c9f17d2022-03-10 23:13:01 +000090 ARM_PIPE_LOG(warning) << "An error has occurred when handling a command: " << e.what();
Jim Flynnbbfe6032020-07-20 16:57:44 +010091 // Did we get here because the socket failed?
92 if ( !profilingConnection.IsOpen() )
93 {
94 // We're going to stop processing commands.
95 // This will leave the thread idle. There is no mechanism to restart the profiling service when the
96 // connection is lost.
97 m_KeepRunning.store(false);
98 }
99 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000100 catch (...)
Matteo Martincigh88813932019-10-04 14:40:04 +0100101 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100102 // Log the error and continue
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000103 ARM_PIPE_LOG(warning) << "An unknown error has occurred when handling a command";
Colm Donelan9ea77002019-10-17 14:02:44 +0100104 // Did we get here because the socket failed?
105 if ( !profilingConnection.IsOpen() )
106 {
107 // We're going to stop processing commands.
108 // This will leave the thread idle. There is no mechanism to restart the profiling service when the
109 // connection is lost.
110 m_KeepRunning.store(false);
111 }
Matteo Martincigh88813932019-10-04 14:40:04 +0100112 }
Matteo Martincigh88813932019-10-04 14:40:04 +0100113 }
Matteo Martincighc2728f92019-10-07 12:35:21 +0100114 while (m_KeepRunning.load());
Matteo Martincigh88813932019-10-04 14:40:04 +0100115
Matteo Martincighc2728f92019-10-07 12:35:21 +0100116 m_IsRunning.store(false);
Matteo Martincigh88813932019-10-04 14:40:04 +0100117}
118
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000119} // namespace pipe
Matteo Martincigh88813932019-10-04 14:40:04 +0100120
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000121} // namespace arm