| 1 |
//! This modules contains functions for kernel variables related to filesystems |
| 2 |
|
| 3 |
use crate::{expect, from_str, read_file, read_value, write_value, ProcResult}; |
| 4 |
use std::time::Duration; |
| 5 |
|
| 6 |
pub mod binfmt_misc; |
| 7 |
pub mod epoll; |
| 8 |
|
| 9 |
/// Information about the status of the directory cache (dcache) |
| 10 |
#[derive(Debug, Clone)] |
| 11 |
pub struct DEntryState { |
| 12 |
/// The number of allocated dentries (dcache entries) |
| 13 |
/// |
| 14 |
/// Unused in Linux 2.2 |
| 15 |
pub nr_dentry: u32, |
| 16 |
|
| 17 |
/// The number of unused dentries. |
| 18 |
pub nr_unused: u32, |
| 19 |
|
| 20 |
/// The age after which dcache entries can be reclaimied when memory is short |
| 21 |
pub age_limit: Duration, |
| 22 |
|
| 23 |
/// Is true when the kernel has called `shrink_dcache_pages()` and the dcache isn't pruned yet. |
| 24 |
pub want_pages: bool, |
| 25 |
} |
| 26 |
|
| 27 |
impl DEntryState { |
| 28 |
fn from_str(s: &str) -> ProcResult<DEntryState> { |
| 29 |
let mut s = s.split_whitespace(); |
| 30 |
let nr_dentry = from_str!(u32, expect!(s.next())); |
| 31 |
let nr_unused = from_str!(u32, expect!(s.next())); |
| 32 |
let age_limit_sec = from_str!(u32, expect!(s.next())); |
| 33 |
let want_pages = from_str!(u32, expect!(s.next())); |
| 34 |
|
| 35 |
Ok(DEntryState { |
| 36 |
nr_dentry, |
| 37 |
nr_unused, |
| 38 |
age_limit: Duration::from_secs(age_limit_sec as u64), |
| 39 |
want_pages: want_pages != 0, |
| 40 |
}) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/// Get information about the status of the directory cache (dcache) |
| 45 |
/// |
| 46 |
/// Linux Linux 2.2 |
| 47 |
pub fn dentry_state() -> ProcResult<DEntryState> { |
| 48 |
let s: String = read_file("/proc/sys/fs/dentry-state")?; |
| 49 |
|
| 50 |
DEntryState::from_str(&s) |
| 51 |
} |
| 52 |
|
| 53 |
/// Get the system-wide limit on the number of open files for all processes. |
| 54 |
/// |
| 55 |
/// System calls that fail when encounā tering this limit fail with the error `ENFILE`. |
| 56 |
pub fn file_max() -> ProcResult<usize> { |
| 57 |
read_value("/proc/sys/fs/file-max") |
| 58 |
} |
| 59 |
|
| 60 |
/// Set the system-wide limit on the number of open files for all processes. |
| 61 |
pub fn set_file_max(max: usize) -> ProcResult<()> { |
| 62 |
write_value("/proc/sys/fs/file-max", max) |
| 63 |
} |
| 64 |
#[derive(Debug, Clone)] |
| 65 |
pub struct FileState { |
| 66 |
/// The number of allocated file handles. |
| 67 |
/// |
| 68 |
/// (i.e. the number of files presently opened) |
| 69 |
pub allocated: u64, |
| 70 |
|
| 71 |
/// The number of free file handles. |
| 72 |
pub free: u64, |
| 73 |
|
| 74 |
/// The maximum number of file handles. |
| 75 |
/// |
| 76 |
/// This may be u64::MAX |
| 77 |
pub max: u64, |
| 78 |
} |
| 79 |
|
| 80 |
pub fn file_nr() -> ProcResult<FileState> { |
| 81 |
let s = read_file("/proc/sys/fs/file-nr")?; |
| 82 |
let mut s = s.split_whitespace(); |
| 83 |
let allocated = from_str!(u64, expect!(s.next())); |
| 84 |
let free = from_str!(u64, expect!(s.next())); |
| 85 |
let max = from_str!(u64, expect!(s.next())); |
| 86 |
|
| 87 |
Ok(FileState { allocated, free, max }) |
| 88 |
} |
| 89 |
|
| 90 |
#[cfg(test)] |
| 91 |
mod tests { |
| 92 |
use super::*; |
| 93 |
#[test] |
| 94 |
fn dentry() { |
| 95 |
let d = dentry_state().unwrap(); |
| 96 |
println!("{:?}", d); |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn filenr() { |
| 101 |
let f = file_nr().unwrap(); |
| 102 |
println!("{:?}", f); |
| 103 |
} |
| 104 |
} |