2009-09-16 06:41:35 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2009-2018, Arvid Norberg
|
2009-09-16 06:41:35 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/bandwidth_manager.hpp"
|
2016-09-06 04:25:20 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
#include <climits>
|
|
|
|
#endif
|
2009-09-16 06:41:35 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2009-09-16 06:41:35 +02:00
|
|
|
|
2015-08-04 07:47:15 +02:00
|
|
|
bandwidth_manager::bandwidth_manager(int channel)
|
2009-09-16 06:41:35 +02:00
|
|
|
: m_queued_bytes(0)
|
|
|
|
, m_channel(channel)
|
|
|
|
, m_abort(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void bandwidth_manager::close()
|
|
|
|
{
|
|
|
|
m_abort = true;
|
2014-04-06 03:03:01 +02:00
|
|
|
|
2017-09-17 13:12:54 +02:00
|
|
|
std::vector<bw_request> queue;
|
|
|
|
queue.swap(m_queue);
|
2009-09-16 06:41:35 +02:00
|
|
|
m_queued_bytes = 0;
|
2014-04-06 03:03:01 +02:00
|
|
|
|
2017-09-17 13:12:54 +02:00
|
|
|
while (!queue.empty())
|
2014-04-06 03:03:01 +02:00
|
|
|
{
|
2017-09-17 13:12:54 +02:00
|
|
|
bw_request& bwr = queue.back();
|
2014-04-06 03:03:01 +02:00
|
|
|
bwr.peer->assign_bandwidth(m_channel, bwr.assigned);
|
2017-09-17 13:12:54 +02:00
|
|
|
queue.pop_back();
|
2014-04-06 03:03:01 +02:00
|
|
|
}
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2009-09-16 06:41:35 +02:00
|
|
|
bool bandwidth_manager::is_queued(bandwidth_socket const* peer) const
|
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto const& r : m_queue)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
if (r.peer.get() == peer) return true;
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int bandwidth_manager::queue_size() const
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
return int(m_queue.size());
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t bandwidth_manager::queued_bytes() const
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
return m_queued_bytes;
|
|
|
|
}
|
2016-10-22 17:47:24 +02:00
|
|
|
|
2009-09-16 06:41:35 +02:00
|
|
|
// non prioritized means that, if there's a line for bandwidth,
|
|
|
|
// others will cut in front of the non-prioritized peers.
|
|
|
|
// this is used by web seeds
|
2018-03-21 23:38:06 +01:00
|
|
|
int bandwidth_manager::request_bandwidth(std::shared_ptr<bandwidth_socket> peer
|
|
|
|
, int const blk, int const priority, bandwidth_channel** chan, int const num_channels)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2011-04-21 05:13:53 +02:00
|
|
|
if (m_abort) return 0;
|
2009-09-16 06:41:35 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(blk > 0);
|
|
|
|
TORRENT_ASSERT(priority > 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// if this assert is hit, the peer is requesting more bandwidth before
|
|
|
|
// being assigned bandwidth for an already outstanding request
|
2009-09-16 06:41:35 +02:00
|
|
|
TORRENT_ASSERT(!is_queued(peer.get()));
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (num_channels == 0)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
// the connection is not rate limited by any of its
|
|
|
|
// bandwidth channels, or it doesn't belong to any
|
|
|
|
// channels. There's no point in adding it to
|
|
|
|
// the queue, just satisfy the request immediately
|
2011-04-21 05:13:53 +02:00
|
|
|
return blk;
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
2014-01-17 08:48:38 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int k = 0;
|
2018-03-21 23:38:06 +01:00
|
|
|
bw_request bwr(std::move(peer), blk, priority);
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < num_channels; ++i)
|
|
|
|
{
|
|
|
|
if (chan[i]->need_queueing(blk))
|
|
|
|
bwr.channel[k++] = chan[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k == 0) return blk;
|
|
|
|
|
2009-09-16 06:41:35 +02:00
|
|
|
m_queued_bytes += blk;
|
2018-03-23 23:04:04 +01:00
|
|
|
m_queue.push_back(std::move(bwr));
|
2011-04-21 05:13:53 +02:00
|
|
|
return 0;
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2009-09-16 06:41:35 +02:00
|
|
|
void bandwidth_manager::check_invariant() const
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t queued = 0;
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto const& r : m_queue)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
queued += r.request_size - r.assigned;
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
TORRENT_ASSERT(queued == m_queued_bytes);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void bandwidth_manager::update_quotas(time_duration const& dt)
|
|
|
|
{
|
|
|
|
if (m_abort) return;
|
|
|
|
if (m_queue.empty()) return;
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t dt_milliseconds = total_milliseconds(dt);
|
2009-09-16 06:41:35 +02:00
|
|
|
if (dt_milliseconds > 3000) dt_milliseconds = 3000;
|
|
|
|
|
|
|
|
// for each bandwidth channel, call update_quota(dt)
|
|
|
|
|
|
|
|
std::vector<bandwidth_channel*> channels;
|
|
|
|
|
2017-09-17 13:12:54 +02:00
|
|
|
std::vector<bw_request> queue;
|
2013-11-02 04:26:53 +01:00
|
|
|
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto i = m_queue.begin(); i != m_queue.end();)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
if (i->peer->is_disconnecting())
|
|
|
|
{
|
|
|
|
m_queued_bytes -= i->request_size - i->assigned;
|
|
|
|
|
|
|
|
// return all assigned quota to all the
|
|
|
|
// bandwidth channels this peer belongs to
|
2013-11-02 04:26:53 +01:00
|
|
|
for (int j = 0; j < bw_request::max_bandwidth_channels && i->channel[j]; ++j)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
bandwidth_channel* bwc = i->channel[j];
|
|
|
|
bwc->return_quota(i->assigned);
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
i->assigned = 0;
|
2018-03-23 23:04:04 +01:00
|
|
|
queue.push_back(std::move(*i));
|
2009-09-16 06:41:35 +02:00
|
|
|
i = m_queue.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-02 04:26:53 +01:00
|
|
|
for (int j = 0; j < bw_request::max_bandwidth_channels && i->channel[j]; ++j)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
bandwidth_channel* bwc = i->channel[j];
|
|
|
|
bwc->tmp = 0;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto const& r : m_queue)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
for (int j = 0; j < bw_request::max_bandwidth_channels && r.channel[j]; ++j)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
bandwidth_channel* bwc = r.channel[j];
|
2009-09-16 06:41:35 +02:00
|
|
|
if (bwc->tmp == 0) channels.push_back(bwc);
|
2016-09-06 04:25:20 +02:00
|
|
|
TORRENT_ASSERT(INT_MAX - bwc->tmp > r.priority);
|
|
|
|
bwc->tmp += r.priority;
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto const& ch : channels)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2016-09-06 04:25:20 +02:00
|
|
|
ch->update_quota(int(dt_milliseconds));
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 04:25:20 +02:00
|
|
|
for (auto i = m_queue.begin(); i != m_queue.end();)
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
|
|
|
int a = i->assign_bandwidth();
|
|
|
|
if (i->assigned == i->request_size
|
|
|
|
|| (i->ttl <= 0 && i->assigned > 0))
|
|
|
|
{
|
|
|
|
a += i->request_size - i->assigned;
|
|
|
|
TORRENT_ASSERT(i->assigned <= i->request_size);
|
2018-03-23 23:04:04 +01:00
|
|
|
queue.push_back(std::move(*i));
|
2009-09-16 06:41:35 +02:00
|
|
|
i = m_queue.erase(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
m_queued_bytes -= a;
|
|
|
|
}
|
|
|
|
|
2017-09-17 13:12:54 +02:00
|
|
|
while (!queue.empty())
|
2009-09-16 06:41:35 +02:00
|
|
|
{
|
2017-09-17 13:12:54 +02:00
|
|
|
bw_request& bwr = queue.back();
|
2009-09-16 06:41:35 +02:00
|
|
|
bwr.peer->assign_bandwidth(m_channel, bwr.assigned);
|
2017-09-17 13:12:54 +02:00
|
|
|
queue.pop_back();
|
2009-09-16 06:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|