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 "logging/Logging.hpp"
12 : #include <opmonlib/OpMonManager.hpp>
13 :
14 : using namespace dunedaq::opmonlib;
15 :
16 69 : OpMonManager::OpMonManager(std::string session, std::string name, facility_ptr_t f_ptr)
17 69 : : MonitorableObject(name, session)
18 : {
19 :
20 69 : m_facility.store(f_ptr);
21 69 : }
22 :
23 : void
24 14 : OpMonManager::start_monitoring()
25 : {
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()
31 26 : << " seconds, at level " << get_opmon_level();
32 :
33 13 : auto running_function = std::bind(&OpMonManager::run, this, std::placeholders::_1);
34 13 : m_thread = std::jthread(running_function);
35 13 : auto handle = m_thread.native_handle();
36 13 : auto thread_name = "opmon";
37 13 : auto rc = pthread_setname_np(handle, thread_name);
38 13 : if (rc != 0) {
39 0 : ers::warning(ThreadNameTooLong(ERS_HERE, thread_name));
40 : }
41 13 : }
42 :
43 : void
44 7 : OpMonManager::stop_monitoring()
45 : {
46 :
47 14 : TLOG() << "Gracefully requesting the monitoring thread to stop";
48 :
49 7 : m_thread.request_stop();
50 7 : m_thread.join();
51 7 : }
52 :
53 : void
54 13 : OpMonManager::run(std::stop_token stoken)
55 : {
56 :
57 26 : TLOG() << "Monitoring thread started";
58 :
59 13 : auto sleep_interval = std::chrono::milliseconds(100);
60 13 : auto reporting_interval = m_cfg.load()->get_interval();
61 13 : auto last_collection_time = std::chrono::steady_clock::now();
62 :
63 231 : while (!stoken.stop_requested()) {
64 :
65 205 : std::this_thread::sleep_for(sleep_interval);
66 205 : auto time_span =
67 205 : std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_collection_time);
68 :
69 205 : if (time_span < reporting_interval)
70 205 : continue;
71 :
72 0 : if (stoken.stop_requested())
73 : break;
74 :
75 0 : last_collection_time = std::chrono::steady_clock::now();
76 0 : publish(collect());
77 : // there is no catch here because collect is supposed to catch all possible exceptions
78 : // In this way we should garantee the collection of metrics on the system
79 : }
80 :
81 26 : TLOG() << "Exiting the monitoring thread";
82 13 : }
|