Set up disk to use heim

This commit is contained in:
ClementTsang 2019-09-07 16:39:17 -04:00
parent 153a2590b0
commit f9b98c71ec
3 changed files with 64 additions and 29 deletions

View File

@ -9,11 +9,15 @@ mod window;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize
let refresh_interval = 1; // TODO: Make changing this possible!
let get_physical_io = false;
let mut sys = System::new();
let mut list_of_timed_processes : Vec<cpu::TimedCPUPackagesStruct> = Vec::new();
let mut list_of_timed_processes : Vec<cpu::TimedCPUPackages> = Vec::new();
let mut list_of_timed_io : Vec<Vec<disks::TimedIOInfo>> = Vec::new();
let mut list_of_timed_physical_io : Vec<Vec<disks::TimedIOInfo>> = Vec::new();
loop {
dbg!("Start data loop...");
sys.refresh_system();
// Get data, potentially store?
@ -22,19 +26,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let list_of_disks = disks::get_disk_usage_list().await?;
for disk in list_of_disks {
println!("{} is mounted on {}: {}/{} free.", disk.name, disk.mount_point, disk.avail_space as f64, disk.total_space as f64);
dbg!("{} is mounted on {}: {}/{} free.", disk.name, disk.mount_point, disk.avail_space as f64, disk.total_space as f64);
// TODO: Check if this is valid
}
list_of_timed_io.push(disks::get_io_usage_list(false).await?);
list_of_timed_physical_io.push(disks::get_io_usage_list(true).await?);
if !list_of_timed_io.is_empty() {
for io in list_of_timed_io.last().unwrap() {
dbg!("IO counter for {} at {:?}: {} writes, {} reads.", &io.mount_point, io.time, io.write_bytes, io.read_bytes);
}
}
if !list_of_timed_physical_io.is_empty() {
for io in list_of_timed_physical_io.last().unwrap() {
dbg!("Physical IO counter for {} at {:?}: {} writes, {} reads.", &io.mount_point, io.time, io.write_bytes, io.read_bytes);
}
}
list_of_timed_processes.push(cpu::get_cpu_data_list(&sys));
if !list_of_timed_processes.is_empty() {
let current_cpu_time = list_of_timed_processes.last().unwrap().time;
for cpu in &list_of_timed_processes.last().unwrap().processor_list {
println!("CPU {} has {}% usage at timestamp {:?}!", cpu.cpu_name, cpu.cpu_usage, current_cpu_time);
dbg!("CPU {} has {}% usage at timestamp {:?}!", &cpu.cpu_name, cpu.cpu_usage, current_cpu_time);
}
}
// Send to drawing module
dbg!("End data loop...");
window::draw_terminal();
// Repeat on interval

View File

@ -5,12 +5,12 @@ pub struct CPUData {
pub cpu_usage : u32,
}
pub struct TimedCPUPackagesStruct {
pub struct TimedCPUPackages {
pub processor_list : Vec<CPUData>,
pub time : std::time::SystemTime,
}
pub fn get_cpu_data_list(sys : &System) -> TimedCPUPackagesStruct {
pub fn get_cpu_data_list(sys : &System) -> TimedCPUPackages {
let cpu_data = sys.get_processor_list();
let mut cpu_vec = Vec::new();
@ -21,12 +21,12 @@ pub fn get_cpu_data_list(sys : &System) -> TimedCPUPackagesStruct {
})
}
TimedCPUPackagesStruct {
TimedCPUPackages {
processor_list : cpu_vec,
time : std::time::SystemTime::now(),
}
}
pub fn clear_old_cpu_data() -> bool {
pub fn is_cpu_data_old() -> bool {
true
}

View File

@ -7,29 +7,47 @@ pub struct DiskInfo {
pub total_space : u64,
}
pub struct IOInfo {
pub name : Box<str>,
pub struct TimedIOInfo {
pub mount_point : Box<str>,
pub read_bytes : u64,
pub write_bytes : u64,
pub time : std::time::SystemTime,
}
pub async fn get_io_usage_list() -> Result<Vec<IOInfo>, heim::Error> {
let mut io_list : Vec<IOInfo> = Vec::new();
let mut counters = heim::disk::io_counters();
while let Some(counter) = counters.next().await {
dbg!(counter?);
pub async fn get_io_usage_list(get_physical : bool) -> Result<Vec<TimedIOInfo>, heim::Error> {
let mut io_list : Vec<TimedIOInfo> = Vec::new();
if get_physical {
let mut physical_counter_stream = heim::disk::io_counters_physical();
while let Some(io) = physical_counter_stream.next().await {
let io = io?;
io_list.push(TimedIOInfo {
mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")),
read_bytes : io.read_bytes().get::<heim_common::units::information::megabyte>(),
write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
}
else {
let mut counter_stream = heim::disk::io_counters();
while let Some(io) = counter_stream.next().await {
let io = io?;
io_list.push(TimedIOInfo {
mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")),
read_bytes : io.read_bytes().get::<heim_common::units::information::megabyte>(),
write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
println!("\n\n--- Per physical disk ---\n");
let mut counters = heim::disk::io_counters_physical();
while let Some(counter) = counters.next().await {
dbg!(counter?);
}
Ok(io_list)
}
pub fn is_io_data_old() -> bool {
true
}
pub async fn get_disk_usage_list() -> Result<Vec<DiskInfo>, heim::Error> {
let mut vec_disks : Vec<DiskInfo> = Vec::new();
let mut partitions_stream = heim::disk::partitions_physical();
@ -38,15 +56,12 @@ pub async fn get_disk_usage_list() -> Result<Vec<DiskInfo>, heim::Error> {
let part = part?;
let usage = heim::disk::usage(part.mount_point().to_path_buf()).await?;
println!(
"{:<17} {:<10} {:<10} {:<10} {:<10} {}",
part.device().unwrap().to_str().unwrap(),
usage.total().get::<heim_common::units::information::megabyte>(),
usage.used().get::<heim_common::units::information::megabyte>(),
usage.free().get::<heim_common::units::information::megabyte>(),
part.file_system().as_str(),
part.mount_point().to_string_lossy(),
);
vec_disks.push(DiskInfo {
avail_space : usage.free().get::<heim_common::units::information::megabyte>(),
total_space : usage.total().get::<heim_common::units::information::megabyte>(),
mount_point : Box::from(part.mount_point().to_str().unwrap_or("Name Unavailable")),
name : Box::from(part.device().unwrap_or_else(|| std::ffi::OsStr::new("Name Unavailable")).to_str().unwrap_or("Name Unavailable")),
});
}
Ok(vec_disks)