2008-11-24 04:21:23 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-11-24 04:21:23 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-11-24 04:21:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring> /* memset() */
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bandwidth.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int_weak() */
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2008-12-16 22:08:17 +00:00
|
|
|
#include "peer-io.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed(nullptr, __VA_ARGS__)
|
2009-01-02 21:50:51 +00:00
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
unsigned int tr_bandwidth::getSpeed_Bps(struct bratecontrol const* r, unsigned int interval_msec, uint64_t now)
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (now == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
now = tr_time_msec();
|
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (now != r->cache_time)
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t bytes = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const cutoff = now - interval_msec;
|
2021-10-09 12:52:09 +00:00
|
|
|
auto* rvolatile = (struct bratecontrol*)r;
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = r->newest; r->transfers[i].date > cutoff;)
|
2011-03-15 18:11:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bytes += r->transfers[i].size;
|
2011-03-15 18:11:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (--i == -1)
|
|
|
|
{
|
|
|
|
i = HISTORY_SIZE - 1; /* circular history */
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (i == r->newest)
|
|
|
|
{
|
|
|
|
break; /* we've come all the way around */
|
|
|
|
}
|
2011-03-15 18:11:31 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2019-03-17 04:07:48 +00:00
|
|
|
rvolatile->cache_val = (unsigned int)(bytes * 1000U / interval_msec);
|
2017-04-19 12:04:45 +00:00
|
|
|
rvolatile->cache_time = now;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return r->cache_val;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, struct bratecontrol* r, size_t size)
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (r->transfers[r->newest].date + GRANULARITY_MSEC >= now)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
r->transfers[r->newest].size += size;
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (++r->newest == HISTORY_SIZE)
|
|
|
|
{
|
|
|
|
r->newest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->transfers[r->newest].date = now;
|
|
|
|
r->transfers[r->newest].size = size;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
2011-03-15 18:11:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* invalidate cache_val*/
|
|
|
|
r->cache_time = 0;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
tr_bandwidth::tr_bandwidth(tr_bandwidth* newParent)
|
|
|
|
: band{}
|
|
|
|
, parent{ nullptr }
|
|
|
|
, children{}
|
|
|
|
, peer{ nullptr }
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
this->children = {};
|
|
|
|
this->band[TR_UP].honorParentLimits = true;
|
|
|
|
this->band[TR_DOWN].honorParentLimits = true;
|
|
|
|
this->setParent(newParent);
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::setParent(tr_bandwidth* newParent)
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
TR_ASSERT(this != newParent);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->parent != nullptr)
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
this->parent->children.erase(this);
|
|
|
|
this->parent = nullptr;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (newParent != nullptr)
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
TR_ASSERT(newParent->parent != this);
|
|
|
|
TR_ASSERT(newParent->children.find(this) == newParent->children.end()); // does not exist
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
newParent->children.insert(this);
|
|
|
|
this->parent = newParent;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::allocateBandwidth(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_priority_t parent_priority,
|
|
|
|
tr_direction dir,
|
|
|
|
unsigned int period_msec,
|
2021-10-09 12:52:09 +00:00
|
|
|
std::vector<tr_peerIo*>& peer_pool)
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-11-26 15:58:26 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
tr_priority_t const priority_ = std::max(parent_priority, this->priority);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* set the available bandwidth */
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->band[dir].isLimited)
|
2008-11-26 15:58:26 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
uint64_t const nextPulseSpeed = this->band[dir].desiredSpeed_Bps;
|
|
|
|
this->band[dir].bytesLeft = nextPulseSpeed * period_msec / 1000U;
|
2008-11-26 15:58:26 +00:00
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add this bandwidth's peer, if any, to the peer pool */
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->peer != nullptr)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
this->peer->priority = priority_;
|
|
|
|
peer_pool.push_back(this->peer);
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
2008-12-09 22:05:45 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
// traverse & repeat for the subtree
|
|
|
|
for (auto child : this->children)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
child->allocateBandwidth(priority_, dir, period_msec, peer_pool);
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir)
|
2008-12-09 22:05:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* First phase of IO. Tries to distribute bandwidth fairly to keep faster
|
|
|
|
* peers from starving the others. Loop through the peers, giving each a
|
|
|
|
* small chunk of bandwidth. Keep looping until we run out of bandwidth
|
|
|
|
* and/or peers that can use it */
|
2021-10-09 12:52:09 +00:00
|
|
|
dbgmsg("%lu peers to go round-robin for %s", peerArray.size(), dir == TR_UP ? "upload" : "download");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
size_t n = peerArray.size();
|
2017-04-19 12:04:45 +00:00
|
|
|
while (n > 0)
|
2008-12-15 21:22:08 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const i = tr_rand_int_weak(n); /* pick a peer at random */
|
2011-10-25 16:56:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* value of 3000 bytes chosen so that when using uTP we'll send a full-size
|
|
|
|
* frame right away and leave enough buffered data for the next frame to go
|
|
|
|
* out in a timely manner. */
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const increment = 3000;
|
2011-05-04 21:38:01 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
int const bytesUsed = tr_peerIoFlush(peerArray[i], dir, increment);
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
dbgmsg("peer #%d of %zu used %d bytes in this pass", i, n, bytesUsed);
|
2009-01-03 02:43:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytesUsed != (int)increment)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* peer is done writing for now; move it to the end of the list */
|
2021-10-09 12:52:09 +00:00
|
|
|
std::swap(peerArray[i], peerArray[n - 1]);
|
2017-04-19 12:04:45 +00:00
|
|
|
--n;
|
2008-12-15 21:22:08 +00:00
|
|
|
}
|
2008-12-09 22:05:45 +00:00
|
|
|
}
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec)
|
2009-04-18 23:17:30 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
std::vector<tr_peerIo*> tmp;
|
|
|
|
std::vector<tr_peerIo*> low;
|
|
|
|
std::vector<tr_peerIo*> normal;
|
|
|
|
std::vector<tr_peerIo*> high;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* allocateBandwidth () is a helper function with two purposes:
|
|
|
|
* 1. allocate bandwidth to b and its subtree
|
|
|
|
* 2. accumulate an array of all the peerIos from b and its subtree. */
|
2021-10-09 12:52:09 +00:00
|
|
|
this->allocateBandwidth(TR_PRI_LOW, dir, period_msec, tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
for (auto io : tmp)
|
2009-04-21 16:18:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoRef(io);
|
|
|
|
tr_peerIoFlushOutgoingProtocolMsgs(io);
|
2009-04-21 16:18:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (io->priority)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PRI_HIGH:
|
2021-10-09 12:52:09 +00:00
|
|
|
high.push_back(io);
|
2021-10-06 17:24:02 +00:00
|
|
|
[[fallthrough]];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_PRI_NORMAL:
|
2021-10-09 12:52:09 +00:00
|
|
|
normal.push_back(io);
|
2021-10-06 17:24:02 +00:00
|
|
|
[[fallthrough]];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2021-10-09 12:52:09 +00:00
|
|
|
low.push_back(io);
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* First phase of IO. Tries to distribute bandwidth fairly to keep faster
|
|
|
|
* peers from starving the others. Loop through the peers, giving each a
|
|
|
|
* small chunk of bandwidth. Keep looping until we run out of bandwidth
|
|
|
|
* and/or peers that can use it */
|
2021-10-09 12:52:09 +00:00
|
|
|
phaseOne(high, dir);
|
|
|
|
phaseOne(normal, dir);
|
|
|
|
phaseOne(low, dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* Second phase of IO. To help us scale in high bandwidth situations,
|
|
|
|
* enable on-demand IO for peers with bandwidth left to burn.
|
|
|
|
* This on-demand IO is enabled until (1) the peer runs out of bandwidth,
|
2021-10-09 12:52:09 +00:00
|
|
|
* or (2) the next tr_bandwidth::allocate () call, when we start over again. */
|
|
|
|
for (auto io : tmp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
tr_peerIoSetEnabled(io, dir, tr_peerIoHasBandwidthLeft(io, dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
for (auto io : tmp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
tr_peerIoUnref(io);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byteCount) const
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->band[dir].isLimited)
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
byteCount = std::min(byteCount, this->band[dir].bytesLeft);
|
|
|
|
|
|
|
|
/* if we're getting close to exceeding the speed limit,
|
|
|
|
* clamp down harder on the bytes available */
|
|
|
|
if (byteCount > 0)
|
2011-03-02 07:21:58 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
double current;
|
|
|
|
double desired;
|
|
|
|
double r;
|
2011-03-03 07:20:18 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (now == 0)
|
2011-03-15 18:11:31 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
now = tr_time_msec();
|
2011-03-15 18:11:31 +00:00
|
|
|
}
|
2011-03-02 07:21:58 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
current = this->getRawSpeed_Bps(now, TR_DOWN);
|
|
|
|
desired = this->getDesiredSpeed_Bps(TR_DOWN);
|
|
|
|
r = desired >= 1 ? current / desired : 0;
|
|
|
|
|
|
|
|
if (r > 1.0)
|
|
|
|
{
|
|
|
|
byteCount = 0;
|
|
|
|
}
|
|
|
|
else if (r > 0.9)
|
|
|
|
{
|
|
|
|
byteCount = static_cast<unsigned int>(byteCount * 0.8);
|
|
|
|
}
|
|
|
|
else if (r > 0.8)
|
|
|
|
{
|
|
|
|
byteCount = static_cast<unsigned int>(byteCount * 0.9);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->parent != nullptr && this->band[dir].honorParentLimits && byteCount > 0)
|
|
|
|
{
|
|
|
|
byteCount = this->parent->clamp(now, dir, byteCount);
|
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
return byteCount;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
void tr_bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool isPieceData, uint64_t now)
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
struct tr_band* band_ = &this->band[dir];
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (band_->isLimited && isPieceData)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
band_->bytesLeft -= std::min(size_t{ band_->bytesLeft }, byteCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_DIRECTION
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (dir == DEBUG_DIRECTION && band_->isLimited)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"%p consumed %5zu bytes of %5s data... was %6zu, now %6zu left\n",
|
2021-10-09 12:52:09 +00:00
|
|
|
this,
|
2021-08-15 09:41:48 +00:00
|
|
|
byteCount,
|
|
|
|
isPieceData ? "piece" : "raw",
|
|
|
|
oldBytesLeft,
|
2021-10-09 12:52:09 +00:00
|
|
|
band_->bytesLeft);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
notifyBandwidthConsumedBytes(now, &band_->raw, byteCount);
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isPieceData)
|
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
notifyBandwidthConsumedBytes(now, &band_->piece, byteCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (this->parent != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
this->parent->notifyBandwidthConsumed(dir, byteCount, isPieceData, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|