FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 6 | #include "CommandHandler.hpp" |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 7 | #include "ProfilingService.hpp" |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 8 | |
Jim Flynn | 6c9f17d | 2022-03-10 23:13:01 +0000 | [diff] [blame] | 9 | #include <common/include/Logging.hpp> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 10 | |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 11 | #if defined(ARMNN_DISABLE_THREADS) |
| 12 | #include <common/include/IgnoreUnused.hpp> |
| 13 | #endif |
| 14 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 15 | namespace arm |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 16 | { |
| 17 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 18 | namespace pipe |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 19 | { |
| 20 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 21 | void CommandHandler::Start(IProfilingConnection& profilingConnection) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 22 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 23 | if (IsRunning()) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 24 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 25 | return; |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 26 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 27 | |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 28 | #if !defined(ARMNN_DISABLE_THREADS) |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 29 | if (m_CommandThread.joinable()) |
| 30 | { |
| 31 | m_CommandThread.join(); |
| 32 | } |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 33 | #endif |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 34 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 35 | m_IsRunning.store(true); |
| 36 | m_KeepRunning.store(true); |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 37 | #if !defined(ARMNN_DISABLE_THREADS) |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 38 | m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection)); |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 39 | #else |
| 40 | IgnoreUnused(profilingConnection); |
| 41 | #endif |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 44 | void CommandHandler::Stop() |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 45 | { |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 46 | m_KeepRunning.store(false); |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 47 | |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 48 | #if !defined(ARMNN_DISABLE_THREADS) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 49 | if (m_CommandThread.joinable()) |
| 50 | { |
| 51 | m_CommandThread.join(); |
| 52 | } |
Jim Flynn | e195a04 | 2022-04-12 17:19:28 +0100 | [diff] [blame^] | 53 | #endif |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 56 | void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 57 | { |
| 58 | do |
| 59 | { |
| 60 | try |
| 61 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 62 | arm::pipe::Packet packet = profilingConnection.ReadPacket(m_Timeout.load()); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 63 | |
| 64 | if (packet.IsEmpty()) |
| 65 | { |
| 66 | // Nothing to do, continue |
| 67 | continue; |
| 68 | } |
| 69 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 70 | arm::pipe::Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(), |
| 71 | packet.GetPacketId()); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 72 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 73 | arm::pipe::CommandHandlerFunctor* commandHandlerFunctor = |
| 74 | m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(), |
| 75 | packet.GetPacketId(), |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 76 | version.GetEncodedValue()); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 77 | ARM_PIPE_ASSERT(commandHandlerFunctor); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 78 | commandHandlerFunctor->operator()(packet); |
| 79 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 80 | catch (const arm::pipe::TimeoutException&) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 81 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 82 | if (m_StopAfterTimeout.load()) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 83 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 84 | m_KeepRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 85 | } |
| 86 | } |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 87 | catch (const arm::pipe::ProfilingException& e) |
| 88 | { |
| 89 | // Log the error and continue |
Jim Flynn | 6c9f17d | 2022-03-10 23:13:01 +0000 | [diff] [blame] | 90 | ARM_PIPE_LOG(warning) << "An error has occurred when handling a command: " << e.what(); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 91 | // 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 Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 100 | catch (...) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 101 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 102 | // Log the error and continue |
Jim Flynn | 6c9f17d | 2022-03-10 23:13:01 +0000 | [diff] [blame] | 103 | ARM_PIPE_LOG(warning) << "An unknown error has occurred when handling a command"; |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 104 | // 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 Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 112 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 113 | } |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 114 | while (m_KeepRunning.load()); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 115 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 116 | m_IsRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 119 | } // namespace pipe |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 120 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 121 | } // namespace arm |