FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 6 | #include "CommandHandler.hpp" |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 7 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 8 | #include <boost/log/trivial.hpp> |
| 9 | |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 10 | namespace armnn |
| 11 | { |
| 12 | |
| 13 | namespace profiling |
| 14 | { |
| 15 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 16 | void CommandHandler::Start(IProfilingConnection& profilingConnection) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 17 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 18 | if (IsRunning()) |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 19 | { |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 20 | return; |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 21 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 22 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 23 | m_IsRunning.store(true); |
| 24 | m_KeepRunning.store(true); |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 25 | m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection)); |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 26 | } |
| 27 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 28 | void CommandHandler::Stop() |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 29 | { |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 30 | m_KeepRunning.store(false); |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 31 | |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 32 | if (m_CommandThread.joinable()) |
| 33 | { |
| 34 | m_CommandThread.join(); |
| 35 | } |
FinnWilliamsArm | 4833cea | 2019-09-17 16:53:53 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Matteo Martincigh | 8a83717 | 2019-10-04 17:01:07 +0100 | [diff] [blame] | 38 | void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 39 | { |
| 40 | do |
| 41 | { |
| 42 | try |
| 43 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 44 | Packet packet = profilingConnection.ReadPacket(m_Timeout.load()); |
| 45 | |
| 46 | if (packet.IsEmpty()) |
| 47 | { |
| 48 | // Nothing to do, continue |
| 49 | continue; |
| 50 | } |
| 51 | |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 52 | Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketId()); |
| 53 | |
| 54 | CommandHandlerFunctor* commandHandlerFunctor = |
| 55 | m_CommandHandlerRegistry.GetFunctor(packet.GetPacketId(), version.GetEncodedValue()); |
| 56 | BOOST_ASSERT(commandHandlerFunctor); |
| 57 | commandHandlerFunctor->operator()(packet); |
| 58 | } |
| 59 | catch (const armnn::TimeoutException&) |
| 60 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 61 | if (m_StopAfterTimeout.load()) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 62 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 63 | m_KeepRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 66 | catch (const Exception& e) |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 67 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 68 | // Log the error and continue |
| 69 | BOOST_LOG_TRIVIAL(warning) << "An error has occurred when handling a command: " << e.what() << std::endl; |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 70 | } |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 71 | } |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 72 | while (m_KeepRunning.load()); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 73 | |
Matteo Martincigh | c2728f9 | 2019-10-07 12:35:21 +0100 | [diff] [blame] | 74 | m_IsRunning.store(false); |
Matteo Martincigh | 8881393 | 2019-10-04 14:40:04 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace profiling |
| 78 | |
| 79 | } // namespace armnn |