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 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 10 | |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 11 | namespace armnn |
| 12 | { |
| 13 | |
| 14 | namespace profiling |
| 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()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 65 | ARMNN_ASSERT(commandHandlerFunctor); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 66 | commandHandlerFunctor->operator()(packet); |
| 67 | } |
| 68 | catch (const armnn::TimeoutException&) |
| 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 |
| 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 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 88 | catch (const Exception& e) |
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 |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 91 | ARMNN_LOG(warning) << "An error has occurred when handling a command: " << e.what(); |
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 | |
| 107 | } // namespace profiling |
| 108 | |
| 109 | } // namespace armnn |