From 599543e889d0701fd6094a7574b2a9123fb8ff2b Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 3 Oct 2009 19:02:31 +0000 Subject: [PATCH] automatically caps files and connections by default to rlimit --- ChangeLog | 1 + include/libtorrent/file_pool.hpp | 1 + src/session_impl.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/ChangeLog b/ChangeLog index 83ab7a51a..67bef06ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -72,6 +72,7 @@ * improved support for sparse files on windows * added ability to give seeding torrents preference to active slots * added torrent_status::finished_time + * automatically caps files and connections by default to rlimit release 0.14.7 diff --git a/include/libtorrent/file_pool.hpp b/include/libtorrent/file_pool.hpp index 8576e4c2c..df25300b3 100644 --- a/include/libtorrent/file_pool.hpp +++ b/include/libtorrent/file_pool.hpp @@ -61,6 +61,7 @@ namespace libtorrent void release(void* st); void release(fs::path const& p); void resize(int size); + int size_limit() const { return m_size; } private: diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 3a3d6c062..8e259d62d 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -307,6 +307,32 @@ namespace aux { m_buffer_allocations = 0; #endif +#if defined TORRENT_BSD || defined TORRENT_LINUX + // ---- auto-cap open files ---- + + struct rlimit rl; + if (getrlimit(RLIMIT_NOFILE, &rl) == 0) + { +#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING + (*m_logger) << time_now_string() << "max number of open files: " << rl.rlim_cur << "\n"; +#endif + + // deduct some margin for epoll/kqueue, log files, + // futexes, shared objects etc. + rl.rlim_cur -= 20; + + // 80% of the available file descriptors should go + m_max_connections = (std::min)(m_max_connections, int(rl.rlim_cur * 8 / 10)); + // 20% goes towards regular files + m_files.resize((std::min)(m_files.size_limit(), int(rl.rlim_cur * 2 / 10))); +#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING + (*m_logger) << time_now_string() << " max connections: " << m_max_connections << "\n"; + (*m_logger) << time_now_string() << " max files: " << m_files.size_limit() << "\n"; +#endif + } +#endif // TORRENT_BSD || TORRENT_LINUX + + // ---- generate a peer id ---- static seed_random_generator seeder;