Line data Source code
1 : /**
2 : * @file OpMonManager.cpp
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include <chrono>
10 :
11 : #include <opmonlib/OpMonManager.hpp>
12 : #include "logging/Logging.hpp"
13 :
14 : using namespace dunedaq::opmonlib;
15 :
16 69 : OpMonManager::OpMonManager( std::string session,
17 : std::string name,
18 69 : facility_ptr_t f_ptr)
19 69 : : MonitorableObject( name, session) {
20 :
21 69 : m_facility.store(f_ptr);
22 :
23 69 : }
24 :
25 14 : void OpMonManager::start_monitoring() {
26 :
27 14 : if ( ! m_cfg )
28 1 : throw MissingConfiguration(ERS_HERE);
29 :
30 26 : TLOG() << "Starting a new monitoring thread with interval " << m_cfg.load()->get_interval().count() << " seconds, at level " << get_opmon_level();
31 :
32 13 : auto running_function = std::bind( & OpMonManager::run, this, std::placeholders::_1);
33 13 : m_thread = std::jthread( running_function );
34 13 : auto handle = m_thread.native_handle();
35 13 : auto thread_name = "opmon";
36 13 : auto rc = pthread_setname_np(handle, thread_name);
37 13 : if (rc != 0) {
38 0 : ers::warning(ThreadNameTooLong(ERS_HERE, thread_name));
39 : }
40 13 : }
41 :
42 :
43 7 : void OpMonManager::stop_monitoring() {
44 :
45 14 : TLOG() << "Gracefully requesting the monitoring thread to stop";
46 :
47 7 : m_thread.request_stop();
48 7 : m_thread.join();
49 7 : }
50 :
51 13 : void OpMonManager::run(std::stop_token stoken ) {
52 :
53 26 : TLOG() << "Monitoring thread started";
54 :
55 13 : auto sleep_interval = std::chrono::milliseconds(100);
56 13 : auto reporting_interval = m_cfg.load()->get_interval();
57 13 : auto last_collection_time = std::chrono::steady_clock::now();
58 :
59 229 : while( ! stoken.stop_requested() ) {
60 :
61 203 : std::this_thread::sleep_for(sleep_interval);
62 203 : auto time_span = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - last_collection_time);
63 :
64 203 : if ( time_span < reporting_interval ) continue;
65 :
66 0 : if ( stoken.stop_requested() ) break;
67 :
68 0 : last_collection_time = std::chrono::steady_clock::now();
69 0 : publish( collect() );
70 : // there is no catch here because collect is supposed to catch all possible exceptions
71 : // In this way we should garantee the collection of metrics on the system
72 :
73 : }
74 :
75 26 : TLOG() << "Exiting the monitoring thread";
76 13 : }
77 :
78 :
|