LCOV - code coverage report
Current view: top level - opmonlib/src - OpMonManager.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 86.2 % 29 25
Test Date: 2025-12-21 13:07:08 Functions: 100.0 % 5 5

            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           67 : OpMonManager::OpMonManager( std::string session,
      17              :                             std::string name,
      18           67 :                             facility_ptr_t f_ptr)
      19           67 :   : MonitorableObject( name, session) {
      20              : 
      21           67 :   m_facility.store(f_ptr);
      22              :   
      23           67 : }
      24              : 
      25            1 : void OpMonManager::start_monitoring() {
      26              : 
      27            1 :   if ( ! m_cfg ) 
      28            0 :     throw MissingConfiguration(ERS_HERE);
      29              : 
      30            2 :   TLOG() << "Starting a new monitoring thread with interval " << m_cfg.load()->get_interval().count() << " seconds, at level " << get_opmon_level();
      31              : 
      32            1 :   auto running_function = std::bind( & OpMonManager::run, this, std::placeholders::_1);
      33            1 :   m_thread = std::jthread( running_function );
      34            1 :   auto handle = m_thread.native_handle();
      35            1 :   auto thread_name = "opmon";
      36            1 :   auto rc =  pthread_setname_np(handle, thread_name);    
      37            1 :    if (rc != 0) {
      38            0 :     ers::warning(ThreadNameTooLong(ERS_HERE, thread_name));
      39              :   }
      40            1 : }
      41              :   
      42            1 : void OpMonManager::run(std::stop_token stoken ) {
      43              : 
      44            1 :   auto sleep_interval = std::chrono::milliseconds(100);
      45            1 :   auto reporting_interval = m_cfg.load()->get_interval();  
      46            1 :   auto last_collection_time = std::chrono::steady_clock::now();
      47              :   
      48            3 :   while( ! stoken.stop_requested() ) {
      49              :     
      50            1 :     std::this_thread::sleep_for(sleep_interval);
      51            1 :     auto time_span = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - last_collection_time);
      52              :     
      53            1 :     if ( time_span >= reporting_interval ) {
      54            0 :       last_collection_time = std::chrono::steady_clock::now();
      55            0 :       publish( collect() );
      56              :       // there is no catch here because collect is supposed to catch all possible exceptions
      57              :       // In this way we should garantee the collection of metrics on the system
      58              :     }
      59              :   }
      60              : 
      61            2 :   TLOG() << "Exiting the monitoring thread";
      62            1 : }
      63              : 
      64              : 
        

Generated by: LCOV version 2.0-1