~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
97 lines · 3.4 KB Raw History
//! The file descriptors the process may have (`RLIMIT_NOFILE`), and the
//! share of them a kind of file is given: accounts' logs one share
//! (`Files`), their tables another (`table::BlockCache`).

use rustix::process::{Resource, Rlimit, getrlimit, setrlimit};

/// The least a share is, whatever the limit.
pub const LEAST: usize = 16;

/// The most a share is, whatever the limit.
pub const MOST: usize = 4096;

/// The highest limit looked for where the hard limit itself is refused:
/// past what any share needs.
const SOUGHT: u64 = 1 << 20;

/// The file descriptors the process may hold at once: its soft limit, or
/// none where it has no limit.
pub fn limit() -> Option<u64> {
    getrlimit(Resource::Nofile).current
}

/// Raise the limit of file descriptors to the most the process is allowed,
/// its hard limit, and answer what it then is. Where the system refuses the
/// hard limit, as macOS does one above what it allows a process, the limit
/// is raised to the most the system takes, and is left as it is if it takes
/// nothing higher.
pub fn raise() -> Option<u64> {
    let now = getrlimit(Resource::Nofile);
    let soft = now.current?;
    let set = |current: Option<u64>| {
        let to = Rlimit {
            current,
            maximum: now.maximum,
        };
        setrlimit(Resource::Nofile, to).is_ok()
    };
    if now.maximum == Some(soft) || set(now.maximum) {
        return limit();
    }
    // `low` is taken, and nothing above `high` is.
    let (mut low, mut high) = (soft, now.maximum.map_or(SOUGHT, |m| m.min(SOUGHT)));
    while low < high {
        let middle = low + (high - low).div_ceil(2);
        if set(Some(middle)) {
            low = middle;
        } else {
            high = middle - 1;
        }
    }
    limit()
}

/// The share of `limit` file descriptors (none: no limit) one kind of file
/// is given: a quarter of them, no fewer than `LEAST` and no more than
/// `MOST` (`spec/LIMITS.md`, "Open files and threads").
pub fn share(limit: Option<u64>) -> usize {
    limit.map_or(MOST, |l| ((l / 4).min(MOST as u64) as usize).max(LEAST))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A share is a quarter of the limit, between 16 and 4096.
    #[test]
    fn a_share_is_a_quarter() {
        assert_eq!(share(Some(256)), 64);
        assert_eq!(share(Some(1024)), 256);
        assert_eq!(share(Some(16384)), 4096);
        assert_eq!(share(Some(1_048_576)), 4096);
        assert_eq!(share(None), 4096);
        assert_eq!(share(Some(u64::MAX)), 4096);
        assert_eq!(share(Some(64)), 16);
        assert_eq!(share(Some(20)), 16);
        assert_eq!(share(Some(0)), 16);
    }

    /// Raised, the limit is no lower than it was, the hard limit as it
    /// was, and either the hard limit or the most the system takes.
    #[test]
    fn the_limit_is_raised_as_far_as_it_goes() {
        let before = getrlimit(Resource::Nofile);
        let after = raise();
        let now = getrlimit(Resource::Nofile);
        assert_eq!(now.maximum, before.maximum);
        assert_eq!((after, limit()), (now.current, now.current));
        assert!(after.is_none_or(|a| before.current.is_some_and(|b| a >= b)));
        if let Some(a) = after.filter(|a| Some(*a) != now.maximum && *a < SOUGHT) {
            let more = Rlimit {
                current: Some(a + 1),
                ..now
            };
            assert!(setrlimit(Resource::Nofile, more).is_err(), "{a} taken");
        }
    }
}