Skip to content

Commit

Permalink
Daemon memory and system fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Aug 24, 2024
1 parent 2e01053 commit 9c427d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
43 changes: 30 additions & 13 deletions platforms/unix/hardwareinfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ pub mod settings;

trait Round {
fn fmt_num(self) -> f64;
fn fmt_num2(self) -> f64;
}

impl Round for f64 {
fn fmt_num(self) -> f64 {
(self * 10.0).round() / 10.0
}

fn fmt_num2(self) -> f64 {
(self * 100.0).round() / 100.0
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -315,14 +320,14 @@ pub fn refresh_hardware_info(data: &mut Data) {
}

// RAM Info
let total_memory = (data.sys.total_memory() as f64 / gb).fmt_num();
let used_memory = (data.sys.used_memory() as f64 / gb).fmt_num();
let total_swap = (data.sys.total_swap() as f64 / gb).fmt_num();
let used_swap = (data.sys.used_swap() as f64 / gb).fmt_num();
let ram_used = ((used_memory / total_memory) * 100.0).fmt_num();
let swap_used = ((used_swap / total_swap) * 100.0).fmt_num();
let memory_available = (total_memory - used_memory).fmt_num();
let virtual_memory_available = (total_swap - used_swap).fmt_num();
let total_memory = (data.sys.total_memory() as f64 / gb).fmt_num2();
let used_memory = (data.sys.used_memory() as f64 / gb).fmt_num2();
let total_swap = (data.sys.total_swap() as f64 / gb).fmt_num2();
let used_swap = (data.sys.used_swap() as f64 / gb).fmt_num2();
let ram_used = ((used_memory / total_memory) * 100.0).fmt_num2();
let swap_used = ((used_swap / total_swap) * 100.0).fmt_num2();
let memory_available = (total_memory - used_memory).fmt_num2();
let virtual_memory_available = (total_swap - used_swap).fmt_num2();

let mut mem_map = IndexMap::<String, f64>::new();
mem_map.insert("Memory Used".to_string(), used_memory);
Expand Down Expand Up @@ -421,11 +426,23 @@ pub fn refresh_hardware_info(data: &mut Data) {

let mut gpu_mem_map = IndexMap::<String, f64>::new();

gpu_mem_map.insert("GPU Memory Used".to_string(), memory.used as f64);
gpu_mem_map.insert(
"GPU Memory Used".to_string(),
(memory.used as f64 / gb).fmt_num(),
);
gpu_mem_map.insert("N/A".to_string(), 0.0);
gpu_mem_map.insert("GPU Memory Total".to_string(), memory.total as f64);
gpu_mem_map.insert("GPU Memory Free".to_string(), memory.free as f64);
gpu_mem_map.insert("GPU Memory Used".to_string(), memory.used as f64);
gpu_mem_map.insert(
"GPU Memory Total".to_string(),
(memory.total as f64 / gb).fmt_num(),
);
gpu_mem_map.insert(
"GPU Memory Free".to_string(),
(memory.free as f64 / gb).fmt_num(),
);
gpu_mem_map.insert(
"GPU Memory Used".to_string(),
(memory.used as f64 / gb).fmt_num(),
);

if data.first_run {
data.hw_info.gpu.name = device.name().unwrap();
Expand Down Expand Up @@ -611,7 +628,7 @@ pub fn refresh_hardware_info(data: &mut Data) {

if let Ok(model) = model_file {
if model.contains("Raspberry Pi") {
data.hw_info.system.motherboard.name = model;
data.hw_info.system.motherboard.name = model.trim().to_string();

// Components temperature:
let components = Components::new_with_refreshed_list();
Expand Down
24 changes: 22 additions & 2 deletions platforms/unix/hardwareinfo/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub fn linux_hardware_info(data: &mut Data) {

data.hw_info.cpu.info[0].max_speed = cpu_info.max_speed.unwrap_or(1.0) / 1000.0 / 1000.0;

info!("{cpu_info:?}");

let cpu_data = cpu::CpuData::new(logical_cpus);
if let Ok(temp) = cpu_data.temperature {
data.hw_info.cpu.temperature.push(CoresSensor {
Expand Down Expand Up @@ -78,6 +76,28 @@ pub fn linux_hardware_info(data: &mut Data) {
drive_data.push(drive::DriveData::new(path));
}
}

// System
let board_name = std::fs::read_to_string("/sys/devices/virtual/dmi/id/board_name");
let bios_date = std::fs::read_to_string("/sys/devices/virtual/dmi/id/bios_date");
let bios_version = std::fs::read_to_string("/sys/devices/virtual/dmi/id/bios_version");
let bios_vendor = std::fs::read_to_string("/sys/devices/virtual/dmi/id/bios_vendor");

if let Ok(model) = board_name {
data.hw_info.system.motherboard.name = model.trim().to_string();
}

if let Ok(date) = bios_date {
data.hw_info.system.bios.date = date.trim().to_string();
}

if let Ok(version) = bios_version {
data.hw_info.system.bios.version = version.trim().to_string();
}

if let Ok(vendor) = bios_vendor {
data.hw_info.system.bios.vendor = vendor.trim().to_string();
}
} else {
// CPU
let logical_cpus = data.sys.cpus().len();
Expand Down

0 comments on commit 9c427d5

Please sign in to comment.