clean up use of getrlimit() by wrapping it and move it to platform_util.cpp. Also take the opportunity to make it simulator friendly (consistent in simulation)
This commit is contained in:
parent
f7b84bf4da
commit
2acb3dcb24
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
|
int max_open_files();
|
||||||
|
|
||||||
boost::uint64_t total_physical_ram();
|
boost::uint64_t total_physical_ram();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,25 +56,6 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if TORRENT_USE_RLIMIT
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wlong-long"
|
|
||||||
#endif // __GNUC__
|
|
||||||
|
|
||||||
#include <sys/resource.h>
|
|
||||||
|
|
||||||
// capture this here where warnings are disabled (the macro generates warnings)
|
|
||||||
const rlim_t rlimit_as = RLIMIT_AS;
|
|
||||||
const rlim_t rlim_infinity = RLIM_INFINITY;
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
#endif // __GNUC__
|
|
||||||
|
|
||||||
#endif // TORRENT_USE_RLIMIT
|
|
||||||
|
|
||||||
#ifdef TORRENT_LINUX
|
#ifdef TORRENT_LINUX
|
||||||
#include <linux/unistd.h>
|
#include <linux/unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/error.hpp"
|
#include "libtorrent/error.hpp"
|
||||||
#include "libtorrent/file_pool.hpp"
|
#include "libtorrent/file_pool.hpp"
|
||||||
#include "libtorrent/torrent_info.hpp"
|
#include "libtorrent/torrent_info.hpp"
|
||||||
|
#include "libtorrent/platform_util.hpp"
|
||||||
#include <boost/scoped_array.hpp>
|
#include <boost/scoped_array.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
#include <boost/tuple/tuple.hpp>
|
#include <boost/tuple/tuple.hpp>
|
||||||
|
@ -174,21 +175,14 @@ namespace libtorrent
|
||||||
m_disk_cache.set_settings(m_settings, ec);
|
m_disk_cache.set_settings(m_settings, ec);
|
||||||
TORRENT_ASSERT(!ec);
|
TORRENT_ASSERT(!ec);
|
||||||
|
|
||||||
#if TORRENT_USE_RLIMIT
|
// deduct some margin for epoll/kqueue, log files,
|
||||||
// ---- auto-cap open files ----
|
// futexes, shared objects etc.
|
||||||
|
// 80% of the available file descriptors should go to connections
|
||||||
struct rlimit rl;
|
// 20% goes towards regular files
|
||||||
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
const int max_files = (std::min)((std::max)(5
|
||||||
{
|
, (max_open_files() - 20) * 2 / 10)
|
||||||
// deduct some margin for epoll/kqueue, log files,
|
, m_file_pool.size_limit());
|
||||||
// futexes, shared objects etc.
|
m_file_pool.resize(max_files);
|
||||||
rl.rlim_cur -= 20;
|
|
||||||
|
|
||||||
// 80% of the available file descriptors should go to connections
|
|
||||||
// 20% goes towards regular files
|
|
||||||
m_file_pool.resize((std::min)(m_file_pool.size_limit(), int(rl.rlim_cur * 2 / 10)));
|
|
||||||
}
|
|
||||||
#endif // TORRENT_USE_RLIMIT
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disk_io_thread::~disk_io_thread()
|
disk_io_thread::~disk_io_thread()
|
||||||
|
|
|
@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
||||||
|
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
#if TORRENT_USE_RLIMIT
|
#if TORRENT_USE_RLIMIT
|
||||||
|
|
||||||
|
@ -48,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
// capture this here where warnings are disabled (the macro generates warnings)
|
// capture this here where warnings are disabled (the macro generates warnings)
|
||||||
const rlim_t rlimit_as = RLIMIT_AS;
|
const rlim_t rlimit_as = RLIMIT_AS;
|
||||||
|
const rlim_t rlimit_nofile = RLIMIT_NOFILE;
|
||||||
const rlim_t rlim_infinity = RLIM_INFINITY;
|
const rlim_t rlim_infinity = RLIM_INFINITY;
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
|
@ -70,8 +72,33 @@ const rlim_t rlim_infinity = RLIM_INFINITY;
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
|
|
||||||
|
int max_open_files()
|
||||||
|
{
|
||||||
|
#if defined TORRENT_BUILD_SIMULATOR
|
||||||
|
return 256;
|
||||||
|
#elif TORRENT_USE_RLIMIT
|
||||||
|
|
||||||
|
struct rlimit rl;
|
||||||
|
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
||||||
|
{
|
||||||
|
if (rl.rlim_cur == rlim_infinity)
|
||||||
|
return (std::numeric_limits<int>::max)();
|
||||||
|
|
||||||
|
return rl.rlim_cur;
|
||||||
|
}
|
||||||
|
return 1024;
|
||||||
|
#else
|
||||||
|
// this seems like a reasonable limit for windows.
|
||||||
|
// http://blogs.msdn.com/b/oldnewthing/archive/2007/07/18/3926581.aspx
|
||||||
|
return 10000;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
boost::uint64_t total_physical_ram()
|
boost::uint64_t total_physical_ram()
|
||||||
{
|
{
|
||||||
|
#if defined TORRENT_BUILD_SIMULATOR
|
||||||
|
return boost::uint64_t(4) * 1024 * 1024 * 1024;
|
||||||
|
#else
|
||||||
// figure out how much physical RAM there is in
|
// figure out how much physical RAM there is in
|
||||||
// this machine. This is used for automatically
|
// this machine. This is used for automatically
|
||||||
// sizing the disk cache size when it's set to
|
// sizing the disk cache size when it's set to
|
||||||
|
@ -116,6 +143,7 @@ namespace libtorrent
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
|
#endif // TORRENT_BUILD_SIMULATOR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -114,6 +114,7 @@ const rlim_t rlim_infinity = RLIM_INFINITY;
|
||||||
#include "libtorrent/torrent_handle.hpp"
|
#include "libtorrent/torrent_handle.hpp"
|
||||||
#include "libtorrent/choker.hpp"
|
#include "libtorrent/choker.hpp"
|
||||||
#include "libtorrent/error.hpp"
|
#include "libtorrent/error.hpp"
|
||||||
|
#include "libtorrent/platform_util.hpp"
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
|
|
||||||
|
@ -523,33 +524,19 @@ namespace aux {
|
||||||
|
|
||||||
#endif // TORRENT_DISABLE_LOGGING
|
#endif // TORRENT_DISABLE_LOGGING
|
||||||
|
|
||||||
#if TORRENT_USE_RLIMIT
|
|
||||||
// ---- auto-cap max connections ----
|
// ---- auto-cap max connections ----
|
||||||
|
int max_files = max_open_files();
|
||||||
struct rlimit rl;
|
// deduct some margin for epoll/kqueue, log files,
|
||||||
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
// futexes, shared objects etc.
|
||||||
{
|
// 80% of the available file descriptors should go to connections
|
||||||
|
m_settings.set_int(settings_pack::connections_limit, (std::min)(
|
||||||
|
m_settings.get_int(settings_pack::connections_limit)
|
||||||
|
, (std::max)(5, (max_files - 20) * 8 / 10)));
|
||||||
|
// 20% goes towards regular files (see disk_io_thread)
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
session_log(" max number of open files: %d", int(rl.rlim_cur));
|
session_log(" max connections: %d", m_settings.get_int(settings_pack::connections_limit));
|
||||||
#endif
|
session_log(" max files: %d", max_files);
|
||||||
// deduct some margin for epoll/kqueue, log files,
|
|
||||||
// futexes, shared objects etc.
|
|
||||||
rl.rlim_cur -= 20;
|
|
||||||
|
|
||||||
// 80% of the available file descriptors should go to connections
|
|
||||||
m_settings.set_int(settings_pack::connections_limit, (std::min)(
|
|
||||||
m_settings.get_int(settings_pack::connections_limit)
|
|
||||||
, int(rl.rlim_cur * 8 / 10)));
|
|
||||||
// 20% goes towards regular files (see disk_io_thread)
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
|
||||||
session_log(" max connections: %d", m_settings.get_int(settings_pack::connections_limit));
|
|
||||||
session_log(" max files: %d", int(rl.rlim_cur * 2 / 10));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif // TORRENT_USE_RLIMIT
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
|
||||||
session_log(" generated peer ID: %s", m_peer_id.to_string().c_str());
|
session_log(" generated peer ID: %s", m_peer_id.to_string().c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -6292,21 +6279,15 @@ retry:
|
||||||
|
|
||||||
void session_impl::update_connections_limit()
|
void session_impl::update_connections_limit()
|
||||||
{
|
{
|
||||||
if (m_settings.get_int(settings_pack::connections_limit) <= 0)
|
int limit = m_settings.get_int(settings_pack::connections_limit);
|
||||||
{
|
|
||||||
m_settings.set_int(settings_pack::connections_limit, (std::numeric_limits<int>::max)());
|
if (limit <= 0)
|
||||||
#if TORRENT_USE_RLIMIT
|
limit = (std::numeric_limits<int>::max)();
|
||||||
rlimit l;
|
|
||||||
if (getrlimit(RLIMIT_NOFILE, &l) == 0
|
limit = (std::max)(5, (std::min)(limit
|
||||||
&& l.rlim_cur != rlim_infinity)
|
, max_open_files() - 20 - m_settings.get_int(settings_pack::file_pool_size)));
|
||||||
{
|
|
||||||
m_settings.set_int(settings_pack::connections_limit
|
m_settings.set_int(settings_pack::connections_limit, limit);
|
||||||
, l.rlim_cur - m_settings.get_int(settings_pack::file_pool_size));
|
|
||||||
if (m_settings.get_int(settings_pack::connections_limit) < 5)
|
|
||||||
m_settings.set_int(settings_pack::connections_limit, 5);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (num_connections() > m_settings.get_int(settings_pack::connections_limit)
|
if (num_connections() > m_settings.get_int(settings_pack::connections_limit)
|
||||||
&& !m_torrents.empty())
|
&& !m_torrents.empty())
|
||||||
|
|
Loading…
Reference in New Issue