From 7874629919c5c54816f098e0353e5f64496cea91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= <33373714+Levminer@users.noreply.github.com> Date: Fri, 6 Sep 2024 14:28:10 +0200 Subject: [PATCH] Linux disk read/write --- platforms/unix/hardwareinfo/src/lib.rs | 6 +++ platforms/unix/hardwareinfo/src/linux/mod.rs | 51 ++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/platforms/unix/hardwareinfo/src/lib.rs b/platforms/unix/hardwareinfo/src/lib.rs index 969db87..543a220 100644 --- a/platforms/unix/hardwareinfo/src/lib.rs +++ b/platforms/unix/hardwareinfo/src/lib.rs @@ -4,6 +4,7 @@ use netdev::{get_default_interface, ip::Ipv4Net, mac::MacAddr, NetworkDevice}; use nvml_wrapper::enum_wrappers::device::{Clock, TemperatureSensor}; use nvml_wrapper::struct_wrappers::device::{MemoryInfo, Utilization}; use serde::{Deserialize, Serialize}; +use std::time::SystemTime; use std::{ env, net::{IpAddr, Ipv4Addr}, @@ -117,8 +118,13 @@ pub struct CoresDisk { pub free_space: u64, pub throughput_read: f64, pub throughput_write: f64, + pub data_read: f64, + pub data_written: f64, pub temperature: CoresSensor, pub health: String, + pub read_sectors: usize, + pub write_sectors: usize, + pub last_timestamp: SystemTime, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/platforms/unix/hardwareinfo/src/linux/mod.rs b/platforms/unix/hardwareinfo/src/linux/mod.rs index 5561294..ac564d0 100644 --- a/platforms/unix/hardwareinfo/src/linux/mod.rs +++ b/platforms/unix/hardwareinfo/src/linux/mod.rs @@ -1,8 +1,9 @@ -use std::sync::LazyLock; +use std::{sync::LazyLock, time::SystemTime}; +use drive::DriveData; use log::{debug, info}; -use crate::{compare_sensor, CoresDisk, CoresRAMInfo, CoresSensor, Data}; +use crate::{compare_sensor, CoresDisk, CoresRAMInfo, CoresSensor, Data, Round}; pub mod cpu; pub mod drive; @@ -57,7 +58,6 @@ pub fn linux_hardware_info(data: &mut Data) { }); } - let mut drive_data = Vec::with_capacity(drive_paths.len()); for path in &drive_paths { let d = drive::DriveData::new(path); @@ -70,10 +70,14 @@ pub fn linux_hardware_info(data: &mut Data) { free_space: 0, throughput_read: 0.0, throughput_write: 0.0, + data_read: 0.0, + data_written: 0.0, temperature: CoresSensor::default(), health: "N/A".to_string(), + read_sectors: d.disk_stats.get("read_sectors").unwrap_or(&0).clone(), + write_sectors: d.disk_stats.get("write_sectors").unwrap_or(&0).clone(), + last_timestamp: SystemTime::now(), }); - drive_data.push(drive::DriveData::new(path)); } } @@ -107,6 +111,45 @@ pub fn linux_hardware_info(data: &mut Data) { if let Ok(temp) = cpu_data.temperature { data.hw_info.cpu.temperature[0] = compare_sensor(&prev_temp, temp as f64); } + + // Drives + let mut i = 0; + + for path in &drive_paths { + let d = drive::DriveData::new(path); + + if !d.is_virtual { + let inner = &d.inner; + let prev = &data.hw_info.system.storage.disks[i].clone(); + + let time_passed = SystemTime::now() + .duration_since(prev.last_timestamp) + .map_or(1.0f64, |timestamp| timestamp.as_secs_f64()); + + let old_inner = inner.clone().sys_stats().unwrap(); + let write_sectors = old_inner.get("write_sectors").unwrap_or(&0); + let read_sectors = old_inner.get("read_sectors").unwrap_or(&0); + let delta_write_sectors = write_sectors.saturating_sub(prev.write_sectors); + let delta_read_sectors = read_sectors.saturating_sub(prev.read_sectors); + let write_speed = (delta_write_sectors * SECTOR_SIZE) as f64 / time_passed; + let read_speed = (delta_read_sectors * SECTOR_SIZE) as f64 / time_passed; + let total_written = (write_sectors * SECTOR_SIZE) as f64 / 1_000_000_000.0; + let total_read = (read_sectors * SECTOR_SIZE) as f64 / 1_000_000_000.0; + + data.hw_info.system.storage.disks[i].throughput_write = write_speed; + data.hw_info.system.storage.disks[i].throughput_read = read_speed; + data.hw_info.system.storage.disks[i].data_read = total_read.fmt_num(); + data.hw_info.system.storage.disks[i].data_written = total_written.fmt_num(); + + data.hw_info.system.storage.disks[i].write_sectors = + d.disk_stats.get("write_sectors").unwrap_or(&0).clone(); + data.hw_info.system.storage.disks[i].read_sectors = + d.disk_stats.get("read_sectors").unwrap_or(&0).clone(); + data.hw_info.system.storage.disks[i].last_timestamp = SystemTime::now(); + + i += 1; + } + } } // let disks = &data.hw_info.system.storage.disks;