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 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 11 | namespace arm |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 12 | { |
| 13 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 14 | namespace pipe |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 15 | { |
| 16 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 17 | void CommandHandler::Start(IProfilingConnection& profilingConnection) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 18 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 19 | if (IsRunning()) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 20 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 21 | return; |
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 | |
Colm Donelan | 9ea7700 | 2019-10-17 14:02:44 +0100 | [diff] [blame] | 24 | if (m_CommandThread.joinable()) |
| 25 | { |
| 26 | m_CommandThread.join(); |
| 27 | } |
| 28 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 29 | m_IsRunning.store(true); |
| 30 | m_KeepRunning.store(true); |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 31 | m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection)); |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 34 | void CommandHandler::Stop() |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 35 | { |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 36 | m_KeepRunning.store(false); |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 37 | |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 38 | if (m_CommandThread.joinable()) |
| 39 | { |
| 40 | m_CommandThread.join(); |
| 41 | } |
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::HandleCommands(IProfilingConnection& profilingConnection) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 45 | { |
| 46 | do |
| 47 | { |
| 48 | try |
| 49 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 50 | arm::pipe::Packet packet = profilingConnection.ReadPacket(m_Timeout.load()); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 51 | |
| 52 | if (packet.IsEmpty()) |
| 53 | { |
| 54 | // Nothing to do, continue |
| 55 | continue; |
| 56 | } |
| 57 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 58 | arm::pipe::Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(), |
| 59 | packet.GetPacketId()); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 60 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 61 | arm::pipe::CommandHandlerFunctor* commandHandlerFunctor = |
| 62 | m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(), |
| 63 | packet.GetPacketId(), |
Jim Flynn | 397043f | 2019-10-17 17:37:10 +0100 | [diff] [blame] | 64 | version.GetEncodedValue()); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 65 | ARM_PIPE_ASSERT(commandHandlerFunctor); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 66 | commandHandlerFunctor->operator()(packet); |
| 67 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 68 | catch (const arm::pipe::TimeoutException&) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 69 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 70 | if (m_StopAfterTimeout.load()) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 71 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 72 | m_KeepRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 75 | catch (const arm::pipe::ProfilingException& e) |
| 76 | { |
| 77 | // Log the error and continue |
Jim Flynn | 6c9f17d | 2022-03-10 23:13:01 +0000 | [diff] [blame] | 78 | 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] | 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 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 88 | catch (...) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 89 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 90 | // Log the error and continue |
Jim Flynn | 6c9f17d | 2022-03-10 23:13:01 +0000 | [diff] [blame] | 91 | 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] | 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 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 100 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 101 | } |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 102 | while (m_KeepRunning.load()); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 103 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 104 | m_IsRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 107 | } // namespace pipe |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 108 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 109 | } // namespace arm |