aboutsummaryrefslogtreecommitdiff
path: root/profiling/client/src/CommandHandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'profiling/client/src/CommandHandler.cpp')
-rw-r--r--profiling/client/src/CommandHandler.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/profiling/client/src/CommandHandler.cpp b/profiling/client/src/CommandHandler.cpp
new file mode 100644
index 0000000000..6ba49c227d
--- /dev/null
+++ b/profiling/client/src/CommandHandler.cpp
@@ -0,0 +1,109 @@
+//
+// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "CommandHandler.hpp"
+#include "ProfilingService.hpp"
+
+#include <common/include/Logging.hpp>
+
+namespace arm
+{
+
+namespace pipe
+{
+
+void CommandHandler::Start(IProfilingConnection& profilingConnection)
+{
+ if (IsRunning())
+ {
+ return;
+ }
+
+ if (m_CommandThread.joinable())
+ {
+ m_CommandThread.join();
+ }
+
+ m_IsRunning.store(true);
+ m_KeepRunning.store(true);
+ m_CommandThread = std::thread(&CommandHandler::HandleCommands, this, std::ref(profilingConnection));
+}
+
+void CommandHandler::Stop()
+{
+ m_KeepRunning.store(false);
+
+ if (m_CommandThread.joinable())
+ {
+ m_CommandThread.join();
+ }
+}
+
+void CommandHandler::HandleCommands(IProfilingConnection& profilingConnection)
+{
+ do
+ {
+ try
+ {
+ arm::pipe::Packet packet = profilingConnection.ReadPacket(m_Timeout.load());
+
+ if (packet.IsEmpty())
+ {
+ // Nothing to do, continue
+ continue;
+ }
+
+ arm::pipe::Version version = m_PacketVersionResolver.ResolvePacketVersion(packet.GetPacketFamily(),
+ packet.GetPacketId());
+
+ arm::pipe::CommandHandlerFunctor* commandHandlerFunctor =
+ m_CommandHandlerRegistry.GetFunctor(packet.GetPacketFamily(),
+ packet.GetPacketId(),
+ version.GetEncodedValue());
+ ARM_PIPE_ASSERT(commandHandlerFunctor);
+ commandHandlerFunctor->operator()(packet);
+ }
+ catch (const arm::pipe::TimeoutException&)
+ {
+ if (m_StopAfterTimeout.load())
+ {
+ m_KeepRunning.store(false);
+ }
+ }
+ catch (const arm::pipe::ProfilingException& e)
+ {
+ // Log the error and continue
+ ARM_PIPE_LOG(warning) << "An error has occurred when handling a command: " << e.what();
+ // Did we get here because the socket failed?
+ if ( !profilingConnection.IsOpen() )
+ {
+ // We're going to stop processing commands.
+ // This will leave the thread idle. There is no mechanism to restart the profiling service when the
+ // connection is lost.
+ m_KeepRunning.store(false);
+ }
+ }
+ catch (...)
+ {
+ // Log the error and continue
+ ARM_PIPE_LOG(warning) << "An unknown error has occurred when handling a command";
+ // Did we get here because the socket failed?
+ if ( !profilingConnection.IsOpen() )
+ {
+ // We're going to stop processing commands.
+ // This will leave the thread idle. There is no mechanism to restart the profiling service when the
+ // connection is lost.
+ m_KeepRunning.store(false);
+ }
+ }
+ }
+ while (m_KeepRunning.load());
+
+ m_IsRunning.store(false);
+}
+
+} // namespace pipe
+
+} // namespace arm