Skip to content

Commit

Permalink
Raise file descriptor limit at entry points rather than just within d…
Browse files Browse the repository at this point in the history
…aemon code. (nanocurrency#4677)
  • Loading branch information
clemahieu authored Jul 10, 2024
1 parent bc9bc82 commit 4f450ae
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 15 deletions.
4 changes: 1 addition & 3 deletions nano/core_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <boost/filesystem/path.hpp>

constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384;

namespace nano
{
namespace test
Expand All @@ -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);
Expand Down
14 changes: 12 additions & 2 deletions nano/lib/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -47,12 +47,22 @@ void nano::set_file_descriptor_limit (std::size_t limit)
fd_limit.rlim_cur = std::min (static_cast<rlim_t> (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)
{
Expand Down
5 changes: 5 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 1 addition & 10 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions nano/nano_rpc/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> 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");
Expand Down
1 change: 1 addition & 0 deletions nano/nano_wallet/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions nano/qt_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions nano/rpc_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions nano/slow_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4f450ae

Please sign in to comment.