From e2ca3e15452bf2d6cd2c75c7cd30bf5e4a1b3a1b Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 27 Aug 2010 14:52:42 +0000 Subject: [PATCH] rate limiter fix --- ChangeLog | 1 + examples/client_test.cpp | 2 ++ include/libtorrent/bandwidth_limit.hpp | 5 +++-- src/bandwidth_limit.cpp | 12 ++++++++---- src/peer_connection.cpp | 8 ++++++-- src/session_impl.cpp | 6 ++++-- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 86f56b2ec..4ae196210 100644 --- a/ChangeLog +++ b/ChangeLog @@ -41,6 +41,7 @@ incoming connection * added more detailed instrumentation of the disk I/O thread + * fixed limitation in rate limiter * fixed build error with boost 1.44 0.15.2 release diff --git a/examples/client_test.cpp b/examples/client_test.cpp index deccf353b..bade7c6ef 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -793,6 +793,7 @@ int main(int argc, char* argv[]) " -H Don't start DHT\n" " -W Set the max number of peers to keep in the peer list\n" " -N Do not attempt to use UPnP and NAT-PMP to forward ports\n" + " -Y Rate limit local peers\n" " " "\n\n" "TORRENT is a path to a .torrent file\n" @@ -989,6 +990,7 @@ int main(int argc, char* argv[]) break; case 'I': outgoing_interface = arg; break; case 'N': start_upnp = false; --i; break; + case 'Y': settings.ignore_limits_on_local_network = false; --i; break; } ++i; // skip the argument } diff --git a/include/libtorrent/bandwidth_limit.hpp b/include/libtorrent/bandwidth_limit.hpp index e10e63ed1..1d9ca5608 100644 --- a/include/libtorrent/bandwidth_limit.hpp +++ b/include/libtorrent/bandwidth_limit.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_BANDWIDTH_CHANNEL_HPP_INCLUDED #include +#include #include "libtorrent/assert.hpp" @@ -70,11 +71,11 @@ private: // this is the amount of bandwidth we have // been assigned without using yet. - int m_quota_left; + boost::int64_t m_quota_left; // the limit is the number of bytes // per second we are allowed to use. - int m_limit; + boost::int64_t m_limit; }; } diff --git a/src/bandwidth_limit.cpp b/src/bandwidth_limit.cpp index 016c517ce..1013198df 100644 --- a/src/bandwidth_limit.cpp +++ b/src/bandwidth_limit.cpp @@ -52,7 +52,7 @@ namespace libtorrent int bandwidth_channel::quota_left() const { if (m_limit == 0) return inf; - return (std::max)(m_quota_left, 0); + return (std::max)(m_quota_left, boost::int64_t(0)); } void bandwidth_channel::update_quota(int dt_milliseconds) @@ -60,9 +60,10 @@ namespace libtorrent if (m_limit == 0) return; m_quota_left += (m_limit * dt_milliseconds + 500) / 1000; if (m_quota_left > m_limit * 3) m_quota_left = m_limit * 3; - distribute_quota = (std::max)(m_quota_left, 0); -// fprintf(stderr, "%p: [%d]: + %d limit: %d\n", this -// , dt_milliseconds, (m_limit * dt_milliseconds + 500) / 1000, m_limit); + distribute_quota = (std::max)(m_quota_left, boost::int64_t(0)); +// fprintf(stderr, "%p: [%d]: + %"PRId64" limit: %"PRId64" quota_left: %"PRId64"\n", this +// , dt_milliseconds, (m_limit * dt_milliseconds + 500) / 1000, m_limit +// , m_quota_left); } // this is used when connections disconnect with @@ -81,6 +82,9 @@ namespace libtorrent TORRENT_ASSERT(amount >= 0); TORRENT_ASSERT(m_limit >= 0); if (m_limit == 0) return; + +// fprintf(stderr, "%p: - %"PRId64" limit: %"PRId64" quota_left: %"PRId64"\n", this +// , amount, m_limit, m_quota_left); m_quota_left -= amount; } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index ffdcd2cef..5803baf23 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -4179,7 +4179,9 @@ namespace libtorrent // peers that we are not interested in are non-prioritized m_channel_state[upload_channel] = peer_info::bw_limit; m_ses.m_upload_rate.request_bandwidth(self() - , m_send_buffer.size(), priority + , (std::max)(m_send_buffer.size(), m_statistics.upload_rate() * 2 + / (1000 / m_ses.m_settings.tick_interval)) + , priority , bwc1, bwc2, bwc3, bwc4); #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << time_now_string() << " *** REQUEST_BANDWIDTH [ " @@ -4212,7 +4214,9 @@ namespace libtorrent TORRENT_ASSERT(m_outstanding_bytes >= 0); m_channel_state[download_channel] = peer_info::bw_limit; m_ses.m_download_rate.request_bandwidth(self() - , (std::max)(m_outstanding_bytes, m_packet_size - m_recv_pos) + 30 + , (std::max)((std::max)(m_outstanding_bytes, m_packet_size - m_recv_pos) + 30 + , m_statistics.download_rate() * 2 + / (1000 / m_ses.m_settings.tick_interval)) , priority , bwc1, bwc2, bwc3, bwc4); } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index eb461e7ae..bdaa4f89a 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1326,7 +1326,7 @@ namespace aux { if (update_disk_io_thread) update_disk_thread_settings(); - if (m_allowed_upload_slots <= m_settings.num_optimistic_unchoke_slots / 2) + if (m_settings.num_optimistic_unchoke_slots >= m_allowed_upload_slots / 2) { if (m_alerts.should_post()) m_alerts.post_alert(performance_alert(torrent_handle() @@ -3697,7 +3697,7 @@ namespace aux { if (m_max_uploads == limit) return; m_max_uploads = limit; m_allowed_upload_slots = limit; - if (m_allowed_upload_slots <= m_settings.num_optimistic_unchoke_slots / 2) + if (m_settings.num_optimistic_unchoke_slots >= m_allowed_upload_slots / 2) { if (m_alerts.should_post()) m_alerts.post_alert(performance_alert(torrent_handle() @@ -4063,7 +4063,9 @@ namespace aux { } if (m_settings.num_optimistic_unchoke_slots) + { TORRENT_ASSERT(num_optimistic <= m_settings.num_optimistic_unchoke_slots); + } if (m_num_unchoked != unchokes) {