From d651cb7a250f85d691035e93a25d2c832a9c71d3 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 21 Aug 2019 14:55:58 -0700 Subject: [PATCH] Adjust |ulimit -n| automatically, no bash required (#5586) --- core/src/validator.rs | 41 +++++++++++++++++++++++++++++++++++++++++ scripts/tune-system.sh | 3 --- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/src/validator.rs b/core/src/validator.rs index 6ba2dd251..cb2f601de 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -431,6 +431,45 @@ fn get_bank_forks( (bank_forks, bank_forks_info, leader_schedule_cache) } +#[cfg(not(unix))] +fn adjust_ulimit_nofile() {} + +#[cfg(unix)] +fn adjust_ulimit_nofile() { + // Rocks DB likes to have many open files. The default open file descriptor limit is + // usually not enough + let desired_nofile = 65000; + + fn get_nofile() -> libc::rlimit { + let mut nofile = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut nofile) } != 0 { + warn!("getrlimit(RLIMIT_NOFILE) failed"); + } + nofile + } + + let mut nofile = get_nofile(); + if nofile.rlim_cur < desired_nofile { + nofile.rlim_cur = desired_nofile; + if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &nofile) } != 0 { + error!( + "Unable to increase the maximum open file descriptor limit to {}", + desired_nofile + ); + + if cfg!(target_os = "macos") { + error!("On mac OS you may need to run |sudo launchctl limit maxfiles 65536 200000| first"); + } + } + + nofile = get_nofile(); + } + info!("Maximum open file descriptors: {}", nofile.rlim_cur); +} + pub fn new_banks_from_blocktree( blocktree_path: &Path, account_paths: Option, @@ -448,6 +487,8 @@ pub fn new_banks_from_blocktree( ) { let genesis_block = GenesisBlock::load(blocktree_path).expect("Failed to load genesis block"); + adjust_ulimit_nofile(); + let (blocktree, ledger_signal_receiver, completed_slots_receiver) = Blocktree::open_with_signal(blocktree_path).expect("Failed to open ledger database"); diff --git a/scripts/tune-system.sh b/scripts/tune-system.sh index 3c1288353..d36bf72ac 100644 --- a/scripts/tune-system.sh +++ b/scripts/tune-system.sh @@ -3,9 +3,6 @@ # Adjusts system settings for optimal fullnode performance # -# shellcheck source=scripts/ulimit-n.sh -source "$(dirname "${BASH_SOURCE[0]}")"/ulimit-n.sh - sysctl_write() { declare name=$1 declare new_value=$2