From 7b352cc0cf74fbbdedd9da183612897c5e2ea1eb Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Tue, 7 May 2024 15:04:03 +0200 Subject: [PATCH] client: tools: add helper to raise nofile rlimit The default soft limit for open file handles is rather low, as some apis (e.g. the POSIX `select(2)` syscall) do not work [0]. The lookahead cache use during the backup clients metadata comparison to reuse unchanged files however requires much higher limits to work effectively. This helper function allows to raise the soft limit to the hard limit, as provided by the `getrlimit(2)` syscall. [0] https://0pointer.net/blog/file-descriptor-limits.html Suggested-by: Thomas Lamprecht Signed-off-by: Christian Ebner --- pbs-client/src/tools/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pbs-client/src/tools/mod.rs b/pbs-client/src/tools/mod.rs index bf507850..29cd33df 100644 --- a/pbs-client/src/tools/mod.rs +++ b/pbs-client/src/tools/mod.rs @@ -612,3 +612,26 @@ pub fn handle_root_with_optional_format_version_prelude bail!("unexpected entry kind {:?}", first.kind()), } } + +/// Raise the soft limit for open file handles to the hard limit +/// +/// Returns the values set before raising the limit as libc::rlimit64 +pub fn raise_nofile_limit() -> Result { + let mut old = libc::rlimit64 { + rlim_cur: 0, + rlim_max: 0, + }; + if 0 != unsafe { libc::getrlimit64(libc::RLIMIT_NOFILE, &mut old as *mut libc::rlimit64) } { + bail!("Failed to get nofile rlimit"); + } + + let mut new = libc::rlimit64 { + rlim_cur: old.rlim_max, + rlim_max: old.rlim_max, + }; + if 0 != unsafe { libc::setrlimit64(libc::RLIMIT_NOFILE, &mut new as *mut libc::rlimit64) } { + bail!("Failed to set nofile rlimit"); + } + + Ok(old) +}