use crate::{expect, from_str, read_file, read_value, write_value, ProcResult};
use std::time::Duration;
pub mod binfmt_misc;
pub mod epoll;
#[derive(Debug, Clone)]
pub struct DEntryState {
pub nr_dentry: u32,
pub nr_unused: u32,
pub age_limit: Duration,
pub want_pages: bool,
}
impl DEntryState {
fn from_str(s: &str) -> ProcResult<DEntryState> {
let mut s = s.split_whitespace();
let nr_dentry = from_str!(u32, expect!(s.next()));
let nr_unused = from_str!(u32, expect!(s.next()));
let age_limit_sec = from_str!(u32, expect!(s.next()));
let want_pages = from_str!(u32, expect!(s.next()));
Ok(DEntryState {
nr_dentry,
nr_unused,
age_limit: Duration::from_secs(age_limit_sec as u64),
want_pages: want_pages != 0,
})
}
}
pub fn dentry_state() -> ProcResult<DEntryState> {
let s: String = read_file("/proc/sys/fs/dentry-state")?;
DEntryState::from_str(&s)
}
pub fn file_max() -> ProcResult<usize> {
read_value("/proc/sys/fs/file-max")
}
pub fn set_file_max(max: usize) -> ProcResult<()> {
write_value("/proc/sys/fs/file-max", max)
}
#[derive(Debug, Clone)]
pub struct FileState {
pub allocated: u64,
pub free: u64,
pub max: u64,
}
pub fn file_nr() -> ProcResult<FileState> {
let s = read_file("/proc/sys/fs/file-nr")?;
let mut s = s.split_whitespace();
let allocated = from_str!(u64, expect!(s.next()));
let free = from_str!(u64, expect!(s.next()));
let max = from_str!(u64, expect!(s.next()));
Ok(FileState { allocated, free, max })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dentry() {
let d = dentry_state().unwrap();
println!("{:?}", d);
}
#[test]
fn filenr() {
let f = file_nr().unwrap();
println!("{:?}", f);
}
}