mirror of
https://github.com/transmission/transmission
synced 2025-01-03 13:35:36 +00:00
chore: minor housekeeping for bandwidth code (#6080)
This commit is contained in:
parent
27dad53bc5
commit
2c97567370
6 changed files with 48 additions and 47 deletions
|
@ -53,32 +53,32 @@ tr_bytes_per_second_t tr_bandwidth::get_speed_bytes_per_second(RateControl& r, u
|
|||
return r.cache_val_;
|
||||
}
|
||||
|
||||
void tr_bandwidth::notify_bandwidth_consumed_bytes(uint64_t const now, RateControl* r, size_t size)
|
||||
void tr_bandwidth::notify_bandwidth_consumed_bytes(uint64_t const now, RateControl& r, size_t size)
|
||||
{
|
||||
if (r->date_[r->newest_] + GranularityMSec >= now)
|
||||
if (r.date_[r.newest_] + GranularityMSec >= now)
|
||||
{
|
||||
r->size_[r->newest_] += size;
|
||||
r.size_[r.newest_] += size;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++r->newest_ == HistorySize)
|
||||
if (++r.newest_ == HistorySize)
|
||||
{
|
||||
r->newest_ = 0;
|
||||
r.newest_ = 0;
|
||||
}
|
||||
|
||||
r->date_[r->newest_] = now;
|
||||
r->size_[r->newest_] = size;
|
||||
r.date_[r.newest_] = now;
|
||||
r.size_[r.newest_] = size;
|
||||
}
|
||||
|
||||
/* invalidate cache_val*/
|
||||
r->cache_time_ = 0;
|
||||
r.cache_time_ = 0;
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
tr_bandwidth::tr_bandwidth(tr_bandwidth* parent)
|
||||
tr_bandwidth::tr_bandwidth(tr_bandwidth* new_parent)
|
||||
{
|
||||
this->set_parent(parent);
|
||||
this->set_parent(new_parent);
|
||||
}
|
||||
|
||||
// ---
|
||||
|
@ -93,8 +93,8 @@ void remove_child(std::vector<tr_bandwidth*>& v, tr_bandwidth* remove_me) noexce
|
|||
// do the cheaper option of overwriting it with the final item
|
||||
if (auto it = std::find(std::begin(v), std::end(v), remove_me); it != std::end(v))
|
||||
{
|
||||
*it = v.back();
|
||||
v.resize(v.size() - 1);
|
||||
std::swap(*it, v.back());
|
||||
v.pop_back();
|
||||
}
|
||||
}
|
||||
} // namespace deparent_helpers
|
||||
|
@ -136,7 +136,7 @@ void tr_bandwidth::set_parent(tr_bandwidth* new_parent)
|
|||
|
||||
void tr_bandwidth::allocate_bandwidth(
|
||||
tr_priority_t parent_priority,
|
||||
unsigned int period_msec,
|
||||
uint64_t period_msec,
|
||||
std::vector<std::shared_ptr<tr_peerIo>>& peer_pool)
|
||||
{
|
||||
auto const priority = std::max(parent_priority, this->priority_);
|
||||
|
@ -184,7 +184,7 @@ void tr_bandwidth::phase_one(std::vector<tr_peerIo*>& peers, tr_direction dir)
|
|||
// Value of 3000 bytes chosen so that when using µTP 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.
|
||||
static auto constexpr Increment = size_t{ 3000 };
|
||||
static auto constexpr Increment = 3000U;
|
||||
|
||||
auto const bytes_used = peers[i]->flush(dir, Increment);
|
||||
tr_logAddTrace(fmt::format("peer #{} of {} used {} bytes in this pass", i, n_unfinished, bytes_used));
|
||||
|
@ -203,7 +203,7 @@ void tr_bandwidth::phase_one(std::vector<tr_peerIo*>& peers, tr_direction dir)
|
|||
}
|
||||
}
|
||||
|
||||
void tr_bandwidth::allocate(unsigned int period_msec)
|
||||
void tr_bandwidth::allocate(uint64_t period_msec)
|
||||
{
|
||||
// keep these peers alive for the scope of this function
|
||||
auto refs = std::vector<std::shared_ptr<tr_peerIo>>{};
|
||||
|
@ -260,7 +260,7 @@ void tr_bandwidth::allocate(unsigned int period_msec)
|
|||
|
||||
// ---
|
||||
|
||||
size_t tr_bandwidth::clamp(uint64_t now, tr_direction dir, size_t byte_count) const
|
||||
size_t tr_bandwidth::clamp(uint64_t now, tr_direction const dir, size_t byte_count) const
|
||||
{
|
||||
TR_ASSERT(tr_isDirection(dir));
|
||||
|
||||
|
@ -312,7 +312,7 @@ void tr_bandwidth::notify_bandwidth_consumed(tr_direction dir, size_t byte_count
|
|||
|
||||
if (band->is_limited_ && is_piece_data)
|
||||
{
|
||||
band->bytes_left_ -= std::min(size_t{ band->bytes_left_ }, byte_count);
|
||||
band->bytes_left_ -= std::min(band->bytes_left_, byte_count);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_DIRECTION
|
||||
|
@ -331,11 +331,11 @@ void tr_bandwidth::notify_bandwidth_consumed(tr_direction dir, size_t byte_count
|
|||
|
||||
#endif
|
||||
|
||||
notify_bandwidth_consumed_bytes(now, &band->raw_, byte_count);
|
||||
notify_bandwidth_consumed_bytes(now, band->raw_, byte_count);
|
||||
|
||||
if (is_piece_data)
|
||||
{
|
||||
notify_bandwidth_consumed_bytes(now, &band->piece_, byte_count);
|
||||
notify_bandwidth_consumed_bytes(now, band->piece_, byte_count);
|
||||
}
|
||||
|
||||
if (this->parent_ != nullptr)
|
||||
|
@ -356,10 +356,10 @@ tr_bandwidth_limits tr_bandwidth::get_limits() const
|
|||
return limits;
|
||||
}
|
||||
|
||||
void tr_bandwidth::set_limits(tr_bandwidth_limits const* limits)
|
||||
void tr_bandwidth::set_limits(tr_bandwidth_limits const& limits)
|
||||
{
|
||||
this->set_desired_speed_bytes_per_second(TR_UP, tr_toSpeedBytes(limits->up_limit_KBps));
|
||||
this->set_desired_speed_bytes_per_second(TR_DOWN, tr_toSpeedBytes(limits->down_limit_KBps));
|
||||
this->set_limited(TR_UP, limits->up_limited);
|
||||
this->set_limited(TR_DOWN, limits->down_limited);
|
||||
this->set_desired_speed_bytes_per_second(TR_UP, tr_toSpeedBytes(limits.up_limit_KBps));
|
||||
this->set_desired_speed_bytes_per_second(TR_DOWN, tr_toSpeedBytes(limits.down_limit_KBps));
|
||||
this->set_limited(TR_UP, limits.up_limited);
|
||||
this->set_limited(TR_DOWN, limits.down_limited);
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ class tr_peerIo;
|
|||
|
||||
struct tr_bandwidth_limits
|
||||
{
|
||||
tr_kilobytes_per_second_t up_limit_KBps = 0;
|
||||
tr_kilobytes_per_second_t down_limit_KBps = 0;
|
||||
tr_kilobytes_per_second_t up_limit_KBps = 0U;
|
||||
tr_kilobytes_per_second_t down_limit_KBps = 0U;
|
||||
bool up_limited = false;
|
||||
bool down_limited = false;
|
||||
};
|
||||
|
@ -78,12 +78,11 @@ struct tr_bandwidth
|
|||
{
|
||||
private:
|
||||
static constexpr size_t HistoryMSec = 2000U;
|
||||
static constexpr size_t IntervalMSec = HistoryMSec;
|
||||
static constexpr size_t GranularityMSec = 250;
|
||||
static constexpr size_t HistorySize = (IntervalMSec / GranularityMSec);
|
||||
static constexpr size_t GranularityMSec = 250U;
|
||||
static constexpr size_t HistorySize = HistoryMSec / GranularityMSec;
|
||||
|
||||
public:
|
||||
explicit tr_bandwidth(tr_bandwidth* newParent);
|
||||
explicit tr_bandwidth(tr_bandwidth* new_parent);
|
||||
|
||||
tr_bandwidth()
|
||||
: tr_bandwidth(nullptr)
|
||||
|
@ -100,7 +99,9 @@ public:
|
|||
tr_bandwidth(tr_bandwidth&&) = delete;
|
||||
tr_bandwidth(tr_bandwidth&) = delete;
|
||||
|
||||
// @brief Sets the peer. nullptr is allowed.
|
||||
/**
|
||||
* @brief Sets the peer. nullptr is allowed.
|
||||
*/
|
||||
void set_peer(std::weak_ptr<tr_peerIo> peer) noexcept
|
||||
{
|
||||
this->peer_ = std::move(peer);
|
||||
|
@ -115,7 +116,7 @@ public:
|
|||
/**
|
||||
* @brief allocate the next `period_msec`'s worth of bandwidth for the peer-ios to consume
|
||||
*/
|
||||
void allocate(unsigned int period_msec);
|
||||
void allocate(uint64_t period_msec);
|
||||
|
||||
void set_parent(tr_bandwidth* new_parent);
|
||||
|
||||
|
@ -132,7 +133,7 @@ public:
|
|||
/**
|
||||
* @brief clamps `byte_count` down to a number that this bandwidth will allow to be consumed
|
||||
*/
|
||||
[[nodiscard]] size_t clamp(tr_direction dir, size_t byte_count) const noexcept
|
||||
[[nodiscard]] size_t clamp(tr_direction const dir, size_t const byte_count) const noexcept
|
||||
{
|
||||
return this->clamp(0, dir, byte_count);
|
||||
}
|
||||
|
@ -192,9 +193,9 @@ public:
|
|||
*/
|
||||
constexpr bool set_limited(tr_direction dir, bool is_limited)
|
||||
{
|
||||
bool* value = &this->band_[dir].is_limited_;
|
||||
bool const did_change = is_limited != *value;
|
||||
*value = is_limited;
|
||||
bool& value = this->band_[dir].is_limited_;
|
||||
bool const did_change = is_limited != value;
|
||||
value = is_limited;
|
||||
return did_change;
|
||||
}
|
||||
|
||||
|
@ -214,9 +215,9 @@ public:
|
|||
*/
|
||||
constexpr bool honor_parent_limits(tr_direction direction, bool is_enabled)
|
||||
{
|
||||
bool* value = &this->band_[direction].honor_parent_limits_;
|
||||
bool const did_change = is_enabled != *value;
|
||||
*value = is_enabled;
|
||||
bool& value = this->band_[direction].honor_parent_limits_;
|
||||
bool const did_change = is_enabled != value;
|
||||
value = is_enabled;
|
||||
return did_change;
|
||||
}
|
||||
|
||||
|
@ -229,7 +230,7 @@ public:
|
|||
|
||||
[[nodiscard]] tr_bandwidth_limits get_limits() const;
|
||||
|
||||
void set_limits(tr_bandwidth_limits const* limits);
|
||||
void set_limits(tr_bandwidth_limits const& limits);
|
||||
|
||||
private:
|
||||
struct RateControl
|
||||
|
@ -260,15 +261,15 @@ private:
|
|||
|
||||
void deparent() noexcept;
|
||||
|
||||
static void notify_bandwidth_consumed_bytes(uint64_t now, RateControl* r, size_t size);
|
||||
static void notify_bandwidth_consumed_bytes(uint64_t now, RateControl& r, size_t size);
|
||||
|
||||
[[nodiscard]] size_t clamp(uint64_t now, tr_direction dir, size_t byte_count) const;
|
||||
[[nodiscard]] size_t clamp(uint64_t now, tr_direction const dir, size_t byte_count) const;
|
||||
|
||||
static void phase_one(std::vector<tr_peerIo*>& peers, tr_direction dir);
|
||||
|
||||
void allocate_bandwidth(
|
||||
tr_priority_t parent_priority,
|
||||
unsigned int period_msec,
|
||||
uint64_t period_msec,
|
||||
std::vector<std::shared_ptr<tr_peerIo>>& peer_pool);
|
||||
|
||||
mutable std::array<Band, 2> band_ = {};
|
||||
|
|
|
@ -442,7 +442,7 @@ size_t tr_peerIo::try_read(size_t max)
|
|||
|
||||
// Do not write more than the bandwidth allows.
|
||||
// If there is no bandwidth left available, disable writes.
|
||||
max = bandwidth().clamp(TR_DOWN, max);
|
||||
max = bandwidth().clamp(Dir, max);
|
||||
if (max == 0)
|
||||
{
|
||||
set_enabled(Dir, false);
|
||||
|
|
|
@ -1611,7 +1611,7 @@ char const* groupSet(tr_session* session, tr_variant* args_in, tr_variant* /*arg
|
|||
limits.up_limit_KBps = static_cast<tr_kilobytes_per_second_t>(limit);
|
||||
}
|
||||
|
||||
group.set_limits(&limits);
|
||||
group.set_limits(limits);
|
||||
|
||||
if (auto honors = bool{}; tr_variantDictFindBool(args_in, TR_KEY_honorsSessionLimits, &honors))
|
||||
{
|
||||
|
|
|
@ -119,7 +119,7 @@ void bandwidthGroupRead(tr_session* session, std::string_view config_dir)
|
|||
limits.down_limit_KBps = static_cast<tr_kilobytes_per_second_t>(*val);
|
||||
}
|
||||
|
||||
group.set_limits(&limits);
|
||||
group.set_limits(limits);
|
||||
|
||||
if (auto const* val = group_map->find_if<bool>(TR_KEY_honorsSessionLimits); val != nullptr)
|
||||
{
|
||||
|
|
|
@ -1029,7 +1029,7 @@ void tr_torrentSetPeerLimit(tr_torrent* tor, uint16_t max_connected_peers);
|
|||
|
||||
// --- File Priorities
|
||||
|
||||
enum
|
||||
enum : tr_priority_t
|
||||
{
|
||||
TR_PRI_LOW = -1,
|
||||
TR_PRI_NORMAL = 0, /* since Normal is 0, memset initializes nicely */
|
||||
|
|
Loading…
Reference in a new issue