diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-12-11 19:54:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-11 19:54:02 -0500 |
commit | 8c4ad90e6756a006243f04033ae9bd9bc9e2f095 (patch) | |
tree | 1e189aea1cb5f497ee7207c184528e77a82c2c01 /src/app | |
parent | 86135e466ce437de5fd767b29fad9acae29cef93 (diff) |
refactor: Another small optimization pass (#350)
Making some small changes that would hopefully improve performance a bit.
- Remove redundant string generations for CPU data conversion
- Switch to fnv for PID hashmap and hashsets
- Use buffered reading to avoid having to store too many lines as strings
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/data_harvester.rs | 6 | ||||
-rw-r--r-- | src/app/data_harvester/processes.rs | 79 |
2 files changed, 45 insertions, 40 deletions
diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index be1300f1..d7989c37 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -3,7 +3,7 @@ use std::time::Instant; #[cfg(target_os = "linux")] -use std::collections::HashMap; +use fnv::FnvHashMap; use sysinfo::{System, SystemExt}; @@ -73,7 +73,7 @@ pub struct DataCollector { pub data: Data, sys: System, #[cfg(target_os = "linux")] - pid_mapping: HashMap<crate::Pid, processes::PrevProcDetails>, + pid_mapping: FnvHashMap<crate::Pid, processes::PrevProcDetails>, #[cfg(target_os = "linux")] prev_idle: f64, #[cfg(target_os = "linux")] @@ -99,7 +99,7 @@ impl Default for DataCollector { data: Data::default(), sys: System::new_with_specifics(sysinfo::RefreshKind::new()), #[cfg(target_os = "linux")] - pid_mapping: HashMap::new(), + pid_mapping: FnvHashMap::default(), #[cfg(target_os = "linux")] prev_idle: 0_f64, #[cfg(target_os = "linux")] diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index a2ad6252..94dcf635 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -6,7 +6,7 @@ use sysinfo::ProcessStatus; use crate::utils::error::{self, BottomError}; #[cfg(target_os = "linux")] -use std::collections::{hash_map::RandomState, HashMap}; +use fnv::{FnvHashMap, FnvHashSet}; #[cfg(not(target_os = "linux"))] use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; @@ -88,7 +88,7 @@ pub struct PrevProcDetails { pub cpu_time: f64, pub proc_stat_path: PathBuf, // pub proc_statm_path: PathBuf, - pub proc_exe_path: PathBuf, + // pub proc_exe_path: PathBuf, pub proc_io_path: PathBuf, pub proc_cmdline_path: PathBuf, pub just_read: bool, @@ -98,7 +98,7 @@ impl PrevProcDetails { pub fn new(pid: Pid) -> Self { PrevProcDetails { proc_io_path: PathBuf::from(format!("/proc/{}/io", pid)), - proc_exe_path: PathBuf::from(format!("/proc/{}/exe", pid)), + // proc_exe_path: PathBuf::from(format!("/proc/{}/exe", pid)), proc_stat_path: PathBuf::from(format!("/proc/{}/stat", pid)), // proc_statm_path: PathBuf::from(format!("/proc/{}/statm", pid)), proc_cmdline_path: PathBuf::from(format!("/proc/{}/cmdline", pid)), @@ -111,23 +111,17 @@ impl PrevProcDetails { fn cpu_usage_calculation( prev_idle: &mut f64, prev_non_idle: &mut f64, ) -> error::Result<(f64, f64)> { + use std::io::prelude::*; + use std::io::BufReader; + // From SO answer: https://stackoverflow.com/a/23376195 let mut path = std::path::PathBuf::new(); path.push("/proc"); path.push("stat"); - let stat_results = std::fs::read_to_string(path)?; - let first_line: &str; - - let split_results = stat_results.split('\n').collect::<Vec<&str>>(); - if split_results.is_empty() { - return Err(error::BottomError::InvalidIO(format!( - "Unable to properly split the stat results; saw {} values, expected at least 1 value.", - split_results.len() - ))); - } else { - first_line = split_results[0]; - } + let mut reader = BufReader::new(std::fs::File::open(path)?); + let mut first_line = String::new(); + reader.read_line(&mut first_line)?; let val = first_line.split_whitespace().collect::<Vec<&str>>(); @@ -177,20 +171,6 @@ fn cpu_usage_calculation( } #[cfg(target_os = "linux")] -fn get_process_io(path: &PathBuf) -> std::io::Result<String> { - Ok(std::fs::read_to_string(path)?) -} - -#[cfg(target_os = "linux")] -fn get_linux_process_io_usage(stat: &[&str]) -> (u64, u64) { - // Represents read_bytes and write_bytes - ( - stat[9].parse::<u64>().unwrap_or(0), - stat[11].parse::<u64>().unwrap_or(0), - ) -} - -#[cfg(target_os = "linux")] fn get_linux_process_vsize_rss(stat: &[&str]) -> (u64, u64) { // Represents vsize and rss (bytes and page numbers respectively) ( @@ -200,6 +180,7 @@ fn get_linux_process_vsize_rss(stat: &[&str]) -> (u64, u64) { } #[cfg(target_os = "linux")] +/// Preferably use this only on small files. fn read_path_contents(path: &PathBuf) -> std::io::Result<String> { Ok(std::fs::read_to_string(path)?) } @@ -246,11 +227,14 @@ fn get_linux_cpu_usage( #[allow(clippy::too_many_arguments)] #[cfg(target_os = "linux")] -fn read_proc<S: core::hash::BuildHasher>( +fn read_proc( pid: Pid, cpu_usage: f64, cpu_fraction: f64, - pid_mapping: &mut HashMap<Pid, PrevProcDetails, S>, use_current_cpu_total: bool, + pid_mapping: &mut FnvHashMap<Pid, PrevProcDetails>, use_current_cpu_total: bool, time_difference_in_secs: u64, mem_total_kb: u64, page_file_kb: u64, ) -> error::Result<ProcessHarvest> { + use std::io::prelude::*; + use std::io::BufReader; + let pid_stat = pid_mapping .entry(pid) .or_insert_with(|| PrevProcDetails::new(pid)); @@ -321,11 +305,33 @@ fn read_proc<S: core::hash::BuildHasher>( let mem_usage_bytes = mem_usage_kb * 1024; // This can fail if permission is denied! + let (total_read_bytes, total_write_bytes, read_bytes_per_sec, write_bytes_per_sec) = - if let Ok(io_results) = get_process_io(&pid_stat.proc_io_path) { - let io_stats = io_results.split_whitespace().collect::<Vec<&str>>(); + if let Ok(file) = std::fs::File::open(&pid_stat.proc_io_path) { + let reader = BufReader::new(file); + let mut lines = reader.lines().skip(4); + + // Represents read_bytes and write_bytes, at the 5th and 6th lines (1-index, not 0-index) + let total_read_bytes = if let Some(Ok(read_bytes_line)) = lines.next() { + if let Some(read_bytes) = read_bytes_line.split_whitespace().last() { + read_bytes.parse::<u64>().unwrap_or(0) + } else { + 0 + } + } else { + 0 + }; + + let total_write_bytes = if let Some(Ok(write_bytes_line)) = lines.next() { + if let Some(write_bytes) = write_bytes_line.split_whitespace().last() { + write_bytes.parse::<u64>().unwrap_or(0) + } else { + 0 + } + } else { + 0 + }; - let (total_read_bytes, total_write_bytes) = get_linux_process_io_usage(&io_stats); let read_bytes_per_sec = if time_difference_in_secs == 0 { 0 } else { @@ -371,14 +377,13 @@ fn read_proc<S: core::hash::BuildHasher>( #[cfg(target_os = "linux")] pub fn get_process_data( prev_idle: &mut f64, prev_non_idle: &mut f64, - pid_mapping: &mut HashMap<Pid, PrevProcDetails, RandomState>, use_current_cpu_total: bool, + pid_mapping: &mut FnvHashMap<Pid, PrevProcDetails>, use_current_cpu_total: bool, time_difference_in_secs: u64, mem_total_kb: u64, page_file_kb: u64, ) -> crate::utils::error::Result<Vec<ProcessHarvest>> { // TODO: [PROC THREADS] Add threads - use std::collections::HashSet; if let Ok((cpu_usage, cpu_fraction)) = cpu_usage_calculation(prev_idle, prev_non_idle) { - let mut pids_to_clear: HashSet<Pid> = pid_mapping.keys().cloned().collect(); + let mut pids_to_clear: FnvHashSet<Pid> = pid_mapping.keys().cloned().collect(); let process_vector: Vec<ProcessHarvest> = std::fs::read_dir("/proc")? .filter_map(|dir| { if let Ok(dir) = dir { |