Added memory data checking.

This commit is contained in:
ClementTsang 2019-09-07 19:03:18 -04:00
parent ace6a4bc68
commit 521698a2bd
3 changed files with 59 additions and 13 deletions

View File

@ -9,19 +9,20 @@ 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_cpu_packages : 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();
let mut list_of_timed_memory : Vec<mem::MemData> = Vec::new();
let mut list_of_timed_swap : Vec<mem::MemData> = Vec::new();
loop {
println!("Start data loop...");
sys.refresh_system();
// TODO: Get data, potentially store? Use a result to check!
let list_of_processes = processes::get_sorted_processes_list(processes::ProcessSorting::NAME, true).await;
let list_of_processes = processes::get_sorted_processes_list(processes::ProcessSorting::CPU, true).await;
for process in list_of_processes {
println!("Process: {} with PID {}, CPU: {}, MEM: {}", process.command, process.pid, process.cpu_usage, process.mem_usage,);
}
@ -56,6 +57,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
list_of_timed_memory.push(mem::get_mem_data_list().await?);
list_of_timed_swap.push(mem::get_swap_data_list().await?);
if !list_of_timed_memory.is_empty() {
let current_mem = list_of_timed_memory.last().unwrap();
println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time);
}
if !list_of_timed_swap.is_empty() {
let current_mem = list_of_timed_swap.last().unwrap();
println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time);
}
// Send to drawing module
println!("End data loop...");
window::draw_terminal();

View File

@ -1,3 +1,31 @@
fn get_timestamped_ram_data() {}
use heim_common::units::information;
pub fn get_ram_data_list() {}
pub struct MemData {
pub mem_total : u64,
pub mem_used : u64,
pub time : std::time::SystemTime,
}
pub async fn get_mem_data_list() -> Result<MemData, heim::Error> {
let memory = heim::memory::memory().await?;
Ok(MemData {
mem_total : memory.total().get::<information::megabyte>(),
mem_used : memory.total().get::<information::megabyte>() - memory.available().get::<information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
pub async fn get_swap_data_list() -> Result<MemData, heim::Error> {
let memory = heim::memory::swap().await?;
Ok(MemData {
mem_total : memory.total().get::<information::megabyte>(),
mem_used : memory.used().get::<information::megabyte>(),
time : std::time::SystemTime::now(),
})
}
pub fn is_mem_data_old() -> bool {
true
}

View File

@ -53,18 +53,22 @@ pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_
let mut process_stream = heim::process::processes().map_ok(cpu_usage).try_buffer_unordered(std::usize::MAX);
// TODO: Evaluate whether this is too slow!
// TODO: Should I filter out blank command names?
// TODO: Group together processes
let mut process_vector : Vec<ProcessInfo> = Vec::new();
while let Some(process) = process_stream.next().await {
let (process, cpu_usage) = process.unwrap();
let mem_measurement = process.memory().await.unwrap();
process_vector.push(ProcessInfo {
command : process.name().await.unwrap_or_else(|_| "".to_string()),
pid : process.pid() as u32,
cpu_usage : cpu_usage.get::<units::ratio::percent>(),
mem_usage : mem_measurement.rss().get::<units::information::megabyte>(),
});
if let Ok(process) = process {
let (process, cpu_usage) = process;
let mem_measurement = process.memory().await;
if let Ok(mem_measurement) = mem_measurement {
process_vector.push(ProcessInfo {
command : process.name().await.unwrap_or_else(|_| "".to_string()),
pid : process.pid() as u32,
cpu_usage : cpu_usage.get::<units::ratio::percent>(),
mem_usage : mem_measurement.rss().get::<units::information::megabyte>(),
});
}
}
}
match sorting_method {
ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage, b.cpu_usage, reverse_order)),