Skip to content

Commit

Permalink
Linux disk read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Sep 6, 2024
1 parent bfad5e5 commit 7874629
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
6 changes: 6 additions & 0 deletions platforms/unix/hardwareinfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)]
Expand Down
51 changes: 47 additions & 4 deletions platforms/unix/hardwareinfo/src/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7874629

Please sign in to comment.