summaryrefslogtreecommitdiffstats
path: root/src/app/data_harvester/cpu/sysinfo.rs
blob: 54318e1b0b63d40833c63bdcf87ac1890a8cadcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! CPU stats through sysinfo.
//! Supports FreeBSD.

use std::collections::VecDeque;

use sysinfo::{CpuExt, LoadAvg, System, SystemExt};

use super::{CpuData, CpuDataType, CpuHarvest, PastCpuTotal, PastCpuWork};
use crate::app::data_harvester::cpu::LoadAvgHarvest;

pub async fn get_cpu_data_list(
    sys: &sysinfo::System, show_average_cpu: bool,
    _previous_cpu_times: &mut [(PastCpuWork, PastCpuTotal)],
    _previous_average_cpu_time: &mut Option<(PastCpuWork, PastCpuTotal)>,
) -> crate::error::Result<CpuHarvest> {
    let mut cpu_deque: VecDeque<_> = sys
        .cpus()
        .iter()
        .enumerate()
        .map(|(i, cpu)| CpuData {
            data_type: CpuDataType::Cpu(i),
            cpu_usage: cpu.cpu_usage() as f64,
        })
        .collect();

    if show_average_cpu {
        let cpu = sys.global_cpu_info();

        cpu_deque.push_front(CpuData {
            data_type: CpuDataType::Avg,
            cpu_usage: cpu.cpu_usage() as f64,
        })
    }

    Ok(Vec::from(cpu_deque))
}

pub async fn get_load_avg() -> crate::error::Result<LoadAvgHarvest> {
    let sys = System::new();
    let LoadAvg { one, five, fifteen } = sys.load_average();

    Ok([one as f32, five as f32, fifteen as f32])
}