Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bottlecap/src/metrics/enhanced/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl Lambda {

// Set fd_max and initial value for fd_use
let fd_max = proc::get_fd_max_data(&pids);
let mut fd_use = proc::get_fd_use_data(&pids).unwrap_or_else(|_| -1_f64);
let mut fd_use = proc::get_fd_use_data(&pids);

// Set threads_max and initial value for threads_use
let threads_max = proc::get_threads_max_data(&pids);
Expand All @@ -586,9 +586,8 @@ impl Lambda {
// Otherwise keep monitoring file descriptor and thread usage periodically
_ = interval.tick() => {
pids = proc::get_pid_list();
if let Ok(fd_use_curr) = proc::get_fd_use_data(&pids) {
fd_use = fd_use.max(fd_use_curr);
}
let fd_use_curr = proc::get_fd_use_data(&pids);
fd_use = fd_use.max(fd_use_curr);
if let Ok(threads_use_curr) = proc::get_threads_use_data(&pids) {
threads_use = threads_use.max(threads_use_curr);
}
Expand Down
29 changes: 13 additions & 16 deletions bottlecap/src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use constants::{
LAMDBA_NETWORK_INTERFACE, PROC_NET_DEV_PATH, PROC_PATH, PROC_STAT_PATH, PROC_UPTIME_PATH,
};
use regex::Regex;
use tracing::debug;
use tracing::{debug, trace};

#[must_use]
pub fn get_pid_list() -> Vec<i64> {
Expand Down Expand Up @@ -244,26 +244,29 @@ fn get_fd_max_data_from_path(path: &str, pids: &[i64]) -> f64 {
fd_max
}

pub fn get_fd_use_data(pids: &[i64]) -> Result<f64, io::Error> {
#[must_use]
pub fn get_fd_use_data(pids: &[i64]) -> f64 {
get_fd_use_data_from_path(PROC_PATH, pids)
}

fn get_fd_use_data_from_path(path: &str, pids: &[i64]) -> Result<f64, io::Error> {
fn get_fd_use_data_from_path(path: &str, pids: &[i64]) -> f64 {
let mut fd_use = 0;

for &pid in pids {
let fd_path = format!("{path}/{pid}/fd");
let Ok(files) = fs::read_dir(fd_path) else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"File descriptor use data not found",
));
let Ok(files) = fs::read_dir(&fd_path) else {
trace!(
"File descriptor use data not found in path {} with pid {}",
fd_path,
pid
);
continue;
};
let count = files.count();
fd_use += count;
}

Ok(fd_use as f64)
fd_use as f64
}

#[must_use]
Expand Down Expand Up @@ -473,14 +476,8 @@ mod tests {
fn test_get_fd_use_data() {
let path = "./tests/proc/process/valid";
let pids = get_pid_list_from_path(path_from_root(path).as_str());
let fd_use_result = get_fd_use_data_from_path(path, &pids);
assert!(fd_use_result.is_ok());
let fd_use = fd_use_result.unwrap();
let fd_use = get_fd_use_data_from_path(path, &pids);
assert!((fd_use - 5.0).abs() < f64::EPSILON);

let path = "./tests/proc/process/invalid_missing";
let fd_use_result = get_fd_use_data_from_path(path, &pids);
assert!(fd_use_result.is_err());
}

#[test]
Expand Down
Loading