From 4f450ae5119094800e58b44b109584cacf6f2f05 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Wed, 10 Jul 2024 12:13:41 +0100 Subject: [PATCH] Raise file descriptor limit at entry points rather than just within daemon code. (#4677) --- nano/core_test/entry.cpp | 4 +--- nano/lib/utility.cpp | 14 ++++++++++++-- nano/lib/utility.hpp | 5 +++++ nano/nano_node/daemon.cpp | 11 +---------- nano/nano_node/entry.cpp | 2 ++ nano/nano_rpc/entry.cpp | 1 + nano/nano_wallet/entry.cpp | 1 + nano/qt_test/entry.cpp | 1 + nano/rpc_test/entry.cpp | 1 + nano/slow_test/entry.cpp | 1 + 10 files changed, 26 insertions(+), 15 deletions(-) diff --git a/nano/core_test/entry.cpp b/nano/core_test/entry.cpp index 423f3731b8..9d08284dd0 100644 --- a/nano/core_test/entry.cpp +++ b/nano/core_test/entry.cpp @@ -6,8 +6,6 @@ #include -constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; - namespace nano { namespace test @@ -19,8 +17,8 @@ void force_nano_dev_network (); GTEST_API_ int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); - nano::set_file_descriptor_limit (OPEN_FILE_DESCRIPTORS_LIMIT); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; testing::InitGoogleTest (&argc, argv); diff --git a/nano/lib/utility.cpp b/nano/lib/utility.cpp index 869828bd04..64d51efaca 100644 --- a/nano/lib/utility.cpp +++ b/nano/lib/utility.cpp @@ -35,7 +35,7 @@ void nano::set_file_descriptor_limit (std::size_t limit) rlimit fd_limit{}; if (-1 == getrlimit (RLIMIT_NOFILE, &fd_limit)) { - std::cerr << "Unable to get current limits for the number of open file descriptors: " << std::strerror (errno); + std::cerr << "WARNING: Unable to get current limits for the number of open file descriptors: " << std::strerror (errno); return; } @@ -47,12 +47,22 @@ void nano::set_file_descriptor_limit (std::size_t limit) fd_limit.rlim_cur = std::min (static_cast (limit), fd_limit.rlim_max); if (-1 == setrlimit (RLIMIT_NOFILE, &fd_limit)) { - std::cerr << "Unable to set limits for the number of open file descriptors: " << std::strerror (errno); + std::cerr << "WARNING: Unable to set limits for the number of open file descriptors: " << std::strerror (errno); return; } #endif } +void nano::initialize_file_descriptor_limit () +{ + nano::set_file_descriptor_limit (DEFAULT_FILE_DESCRIPTOR_LIMIT); + auto limit = nano::get_file_descriptor_limit (); + if (limit < DEFAULT_FILE_DESCRIPTOR_LIMIT) + { + std::cerr << "WARNING: Current file descriptor limit of " << limit << " is lower than the " << DEFAULT_FILE_DESCRIPTOR_LIMIT << " recommended. Node was unable to change it." << std::endl; + } +} + nano::container_info_composite::container_info_composite (std::string const & name) : name (name) { diff --git a/nano/lib/utility.hpp b/nano/lib/utility.hpp index 35d19892dd..ecbc35ee13 100644 --- a/nano/lib/utility.hpp +++ b/nano/lib/utility.hpp @@ -134,6 +134,11 @@ void create_load_memory_address_files (); */ std::size_t get_file_descriptor_limit (); void set_file_descriptor_limit (std::size_t limit); +/** + * This should be called from entry points. It sets the file descriptor limit to the maximum allowed and logs any errors. + */ +constexpr std::size_t DEFAULT_FILE_DESCRIPTOR_LIMIT = 16384; +void initialize_file_descriptor_limit (); void remove_all_files_in_dir (std::filesystem::path const & dir); void move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to); diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 6752bde692..45eaea5509 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -54,8 +54,6 @@ void install_abort_signal_handler () sigaction (SIGABRT, &sa, NULL); #endif } - -constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; } void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flags const & flags) @@ -105,14 +103,7 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag // Print info about number of logical cores detected, those are used to decide how many IO, worker and signature checker threads to spawn logger.info (nano::log::type::daemon, "Hardware concurrency: {} ( configured: {} )", std::thread::hardware_concurrency (), nano::hardware_concurrency ()); - - nano::set_file_descriptor_limit (OPEN_FILE_DESCRIPTORS_LIMIT); - auto const file_descriptor_limit = nano::get_file_descriptor_limit (); - logger.info (nano::log::type::daemon, "File descriptors limit: {}", file_descriptor_limit); - if (file_descriptor_limit < OPEN_FILE_DESCRIPTORS_LIMIT) - { - logger.warn (nano::log::type::daemon, "File descriptors limit is lower than the {} recommended. Node was unable to change it.", OPEN_FILE_DESCRIPTORS_LIMIT); - } + logger.info (nano::log::type::daemon, "File descriptors limit: {}", nano::get_file_descriptor_limit ()); // for the daemon start up, if the user hasn't specified a port in // the config, we must use the default peering port for the network diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 205a8d2af9..c86228a04b 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -55,11 +55,13 @@ class address_library_pair bool operator< (const address_library_pair & other) const; bool operator== (const address_library_pair & other) const; }; + } int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 11cea70432..c54b335869 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -84,6 +84,7 @@ void run (std::filesystem::path const & data_path, std::vector cons int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); boost::program_options::options_description description ("Command line options"); diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 65be63a226..25b29947fb 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -237,6 +237,7 @@ int run_wallet (QApplication & application, int argc, char * const * argv, std:: int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/qt_test/entry.cpp b/nano/qt_test/entry.cpp index e8976178c4..b3ce57fd8f 100644 --- a/nano/qt_test/entry.cpp +++ b/nano/qt_test/entry.cpp @@ -17,6 +17,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/rpc_test/entry.cpp b/nano/rpc_test/entry.cpp index 2a94b7d9e7..806f07645b 100644 --- a/nano/rpc_test/entry.cpp +++ b/nano/rpc_test/entry.cpp @@ -15,6 +15,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::set_use_memory_pools (false); diff --git a/nano/slow_test/entry.cpp b/nano/slow_test/entry.cpp index 0b9a0ca758..096c7dc6a9 100644 --- a/nano/slow_test/entry.cpp +++ b/nano/slow_test/entry.cpp @@ -14,6 +14,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard;