From a1dc09faa6a363b9bf0798f93f20fe441fc6db18 Mon Sep 17 00:00:00 2001 From: Marco Roda Date: Sat, 10 Aug 2024 11:35:12 +0200 Subject: [PATCH] Compiling monitoring thread --- include/opmonlib/OpMonManager.hpp | 19 +++++++++---- src/OpMonManager.cpp | 47 ++++++++----------------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/include/opmonlib/OpMonManager.hpp b/include/opmonlib/OpMonManager.hpp index 645be46..7be68c3 100644 --- a/include/opmonlib/OpMonManager.hpp +++ b/include/opmonlib/OpMonManager.hpp @@ -10,11 +10,18 @@ #define OPMONLIB_INCLUDE_OPMONLIB_OPMONMANAGER_HPP_ #include +#include + #include "opmonlib/MonitorableObject.hpp" -#include "utilities/WorkerThread.hpp" namespace dunedaq { + ERS_DECLARE_ISSUE( opmonlib, + ThreadNameTooLong, + "The name " << name << " is too long for a thread name", + ((std::string)name) + ) + ERS_DECLARE_ISSUE( opmonlib, MonitoringThreadNotSet, "Monitoring thread not set", @@ -46,7 +53,7 @@ class OpMonManager : protected MonitorableObject std::string opmon_facility_uri = "stdout") : OpMonManager( session, name, makeOpMonFacility(opmon_facility_uri) ){;} - virtual ~OpMonManager(); + virtual ~OpMonManager() = default; using MonitorableObject::get_opmon_id; using MonitorableObject::get_opmon_level; @@ -55,7 +62,8 @@ class OpMonManager : protected MonitorableObject // data collecting loop void start_monitoring(std::chrono::seconds); - void stop_monitoring(); + // The stop command is not necessary. + // The stop is invoked during the destruction of the thread or at the start of a new one protected: @@ -68,12 +76,11 @@ class OpMonManager : protected MonitorableObject std::string name, facility_ptr_t ); - void run( std::atomic & running, - std::chrono::seconds ); // function used by thread + void run( std::stop_token, std::chrono::seconds ); // function used by the jthread private: - std::unique_ptr m_thread_p; + std::jthread m_thread; }; diff --git a/src/OpMonManager.cpp b/src/OpMonManager.cpp index f21a6af..f954e8f 100644 --- a/src/OpMonManager.cpp +++ b/src/OpMonManager.cpp @@ -22,54 +22,29 @@ OpMonManager::OpMonManager( std::string session, } - -OpMonManager::~OpMonManager() { - try { - stop_monitoring(); - } catch ( ... ) {;} -} - - void OpMonManager::start_monitoring(std::chrono::seconds interval) { - if (m_thread_p) { - try { - m_thread_p->stop_working_thread(); - } catch (const utilities::ThreadingIssue & ti){ - ers::warning( ti ); - } - } - TLOG() << "Starting a new monitoring thread with interval " << interval.count() << " seconds, at level " << get_opmon_level(); - auto running_function = std::bind( & OpMonManager::run, this, std::placeholders::_1, interval); - m_thread_p.reset(new dunedaq::utilities::WorkerThread( running_function ) ); - - m_thread_p->start_working_thread("opmon"); -} - - - -void OpMonManager::stop_monitoring() { - - if ( m_thread_p ) { - m_thread_p->stop_working_thread(); - } else { - throw MonitoringThreadNotSet(ERS_HERE); + auto running_function = std::bind( & OpMonManager::run, this, + std::placeholders::_1, std::placeholders::_2); + m_thread = std::jthread( running_function, interval ); + auto handle = m_thread.native_handle(); + auto thread_name = "opmon"; + auto rc = pthread_setname_np(handle, thread_name); + if (rc != 0) { + ers::warning(ThreadNameTooLong(ERS_HERE, thread_name)); } - } - - -void OpMonManager::run(std::atomic & running, +void OpMonManager::run(std::stop_token stoken, std::chrono::seconds interval) { auto sleep_interval = std::chrono::milliseconds(100); auto last_collection_time = std::chrono::steady_clock::now(); - while( running.load() ) { + while( ! stoken.stop_requested() ) { std::this_thread::sleep_for(sleep_interval); auto time_span = std::chrono::duration_cast( std::chrono::steady_clock::now() - last_collection_time); @@ -81,6 +56,8 @@ void OpMonManager::run(std::atomic & running, // In this way we should garantee the collection of metrics on the system } } + + TLOG() << "Exiting the monitoring thread"; }