liftof_cc/threads/
monitoring.rs

1/// TOF CPU related monitoring tasks 
2///
3/// Monitors environmental data from the 
4/// MTB as well as the TOF CPU
5
6use std::time::{
7    Instant,
8    Duration
9};
10use std::thread::sleep;
11use std::sync::{
12    Arc,
13    Mutex
14};
15
16use crossbeam_channel::Sender;
17
18use gondola_core::prelude::*;
19//use tof_dataclasses::monitoring::{
20//    CPUMoniData,
21//};
22//use tof_dataclasses::packets::TofPacket;
23//use tof_dataclasses::threading::ThreadControl;
24
25#[cfg(feature="tof-ctrl")]
26use tof_control::helper::cpu_type::{
27    CPUTempDebug,
28    CPUInfoDebug,
29};
30
31//use liftof_lib::thread_control::ThreadControl;
32
33
34/// Monitor the main tof computer (sysinfo)
35///
36/// Get cpu usage, disk usage and temperature
37/// information for the main TOF CPU
38///
39/// Thread to be used with liftof-cc and friends
40///
41/// # Arguments
42///
43/// * thread control - start/stop/halt/revive thread
44///                    externally
45/// * verbose        - print monitoring information 
46///                    to the terminal
47#[cfg(feature="tof-ctrl")]
48pub fn monitor_cpu(tp_sender      : Sender<TofPacket>,
49                   moni_interval  : u64,
50                   thread_control : Arc<Mutex<ThreadControl>>,
51                   verbose        : bool) {
52  let mut moni_data = CPUMoniData::new();
53  let mut timer     = Instant::now();
54  let sleep_time    = Duration::from_secs(moni_interval);
55  'main: loop {
56    let cpu_info    = CPUInfoDebug::new();
57    let cpu_temp    = CPUTempDebug::new();
58    if timer.elapsed().as_secs() >= moni_interval {
59      moni_data.add_temps(&cpu_temp);
60      moni_data.add_info(&cpu_info);
61      //let tp = TofPacket::from(&moni_data);
62      let tp = moni_data.pack();
63      match tp_sender.send(tp) {
64        Err(err) => error!("Can't send CPUMoniData over channel1 {err}"),
65        Ok(_)    => ()
66      }
67      timer = Instant::now();
68      if verbose {
69        println!("{}", moni_data);
70      }
71    }
72    sleep(sleep_time);
73    // FIXME - technically we should look for the 
74    // stop signal on a shorter timescale.
75    // But this saves CPU cycles
76    match thread_control.try_lock() {
77      Err(err) => error!("Unable to lock shared memory! {err}"),
78      Ok(tc)   => {
79        //println!("== ==> [monitoring] tc lock ackquired!");
80        if tc.stop_flag {
81          println!("==> Stopping monitoring thread, stop signal received!");
82          break 'main;
83        }
84      }
85    }
86  }
87}