Skip to content

Commit

Permalink
Compiling monitoring thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mroda88 committed Aug 10, 2024
1 parent 90019f0 commit a1dc09f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
19 changes: 13 additions & 6 deletions include/opmonlib/OpMonManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
#define OPMONLIB_INCLUDE_OPMONLIB_OPMONMANAGER_HPP_

#include <cstddef>
#include <thread>

#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",
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -68,12 +76,11 @@ class OpMonManager : protected MonitorableObject
std::string name,
facility_ptr_t );

void run( std::atomic<bool> & 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<utilities::WorkerThread> m_thread_p;
std::jthread m_thread;

};

Expand Down
47 changes: 12 additions & 35 deletions src/OpMonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> & 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::milliseconds>( std::chrono::steady_clock::now() - last_collection_time);
Expand All @@ -81,6 +56,8 @@ void OpMonManager::run(std::atomic<bool> & running,
// In this way we should garantee the collection of metrics on the system
}
}

TLOG() << "Exiting the monitoring thread";
}


0 comments on commit a1dc09f

Please sign in to comment.