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