cog_task/util/
system.rs

1use std::fmt::{Debug, Formatter};
2use sysinfo::{CpuExt, SystemExt};
3
4#[derive(Clone)]
5pub struct SystemInfo {
6    system: String,
7    cpu_brand: String,
8    cpu_cores: usize,
9    memory_total: u64,
10    memory_available: u64,
11    pub(crate) renderer: String,
12    pub(crate) hw_acceleration: String,
13}
14
15impl SystemInfo {
16    pub fn new() -> Self {
17        let mut sys = sysinfo::System::new_all();
18        sys.refresh_memory();
19        sys.refresh_cpu();
20
21        Self {
22            system: format!(
23                "{} ({}) ({})",
24                sys.name().unwrap_or_else(|| "".to_owned()).trim(),
25                sys.long_os_version()
26                    .unwrap_or_else(|| "?".to_owned())
27                    .trim(),
28                sys.kernel_version()
29                    .unwrap_or_else(|| "?".to_owned())
30                    .trim(),
31            ),
32            cpu_brand: sys.global_cpu_info().brand().to_string(),
33            cpu_cores: sys.cpus().len(),
34            memory_total: sys.total_memory(),
35            memory_available: sys.available_memory(),
36            renderer: "(?)".to_owned(),
37            hw_acceleration: "(?)".to_owned(),
38        }
39    }
40}
41
42impl Default for SystemInfo {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl Debug for SystemInfo {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        write!(
51            f,
52            "System: {}\nCPU: {} (x{})\nMemory (GiB): {:.1} (available: {:.1})\n\
53            Renderer: {}\nHW acceleration: {}",
54            self.system,
55            self.cpu_brand,
56            self.cpu_cores,
57            self.memory_total as f64 / 2_f64.powi(30),
58            self.memory_available as f64 / 2_f64.powi(30),
59            self.renderer,
60            self.hw_acceleration
61        )
62    }
63}