refactor: run clang-tidy on bandwidth.h, .cc (#1930)

* chore: allow vararg calls in qt/.clang-tidy

An awkward PR since this isn't intended for the qt/ codebase at all, but
rather for use with libtransmission instead. But libtransmission is *so*
noncompilant at this point that this config can't be promoted higher up
the tree yet. Instead, it needs to be callable on-request by devs.

* refactor: make clang-tidy happy with bandwidth.h, .cc

* refactor: use getFooSpeedBytesPerSecond()
This commit is contained in:
Charles Kerr 2021-10-12 01:04:22 -05:00 committed by GitHub
parent 148de2da94
commit 62372602b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 112 additions and 113 deletions

View File

@ -23,50 +23,49 @@
****
***/
unsigned int Bandwidth::getSpeed_Bps(RateControl const* r, unsigned int interval_msec, uint64_t now)
unsigned int Bandwidth::getSpeedBytesPerSecond(RateControl& r, unsigned int interval_msec, uint64_t now)
{
if (now == 0)
{
now = tr_time_msec();
}
if (now != r->cache_time_)
if (now != r.cache_time_)
{
uint64_t bytes = 0;
uint64_t const cutoff = now - interval_msec;
auto* rvolatile = (RateControl*)r;
for (int i = r->newest_; r->transfers_[i].date_ > cutoff;)
for (int i = r.newest_; r.transfers_[i].date_ > cutoff;)
{
bytes += r->transfers_[i].size_;
bytes += r.transfers_[i].size_;
if (--i == -1)
{
i = HISTORY_SIZE - 1; /* circular history */
i = HistorySize - 1; /* circular history */
}
if (i == r->newest_)
if (i == r.newest_)
{
break; /* we've come all the way around */
}
}
rvolatile->cache_val_ = (unsigned int)(bytes * 1000U / interval_msec);
rvolatile->cache_time_ = now;
r.cache_val_ = unsigned(bytes * 1000U / interval_msec);
r.cache_time_ = now;
}
return r->cache_val_;
return r.cache_val_;
}
void Bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, RateControl* r, size_t size)
{
if (r->transfers_[r->newest_].date_ + GRANULARITY_MSEC >= now)
if (r->transfers_[r->newest_].date_ + GranularityMSec >= now)
{
r->transfers_[r->newest_].size_ += size;
}
else
{
if (++r->newest_ == HISTORY_SIZE)
if (++r->newest_ == HistorySize)
{
r->newest_ = 0;
}
@ -83,25 +82,20 @@ void Bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, RateControl* r,
****
***/
Bandwidth::Bandwidth(Bandwidth* newParent)
: band_{}
, parent_{ nullptr }
, children_{}
, peer_{ nullptr }
Bandwidth::Bandwidth(Bandwidth* new_parent)
{
this->children_ = {};
this->band_[TR_UP].honor_parent_limits_ = true;
this->band_[TR_DOWN].honor_parent_limits_ = true;
this->setParent(newParent);
this->setParent(new_parent);
}
/***
****
***/
void Bandwidth::setParent(Bandwidth* newParent)
void Bandwidth::setParent(Bandwidth* new_parent)
{
TR_ASSERT(this != newParent);
TR_ASSERT(this != new_parent);
if (this->parent_ != nullptr)
{
@ -109,13 +103,13 @@ void Bandwidth::setParent(Bandwidth* newParent)
this->parent_ = nullptr;
}
if (newParent != nullptr)
if (new_parent != nullptr)
{
TR_ASSERT(newParent->parent_ != this);
TR_ASSERT(newParent->children_.find(this) == newParent->children_.end()); // does not exist
TR_ASSERT(new_parent->parent_ != this);
TR_ASSERT(new_parent->children_.find(this) == new_parent->children_.end()); // does not exist
newParent->children_.insert(this);
this->parent_ = newParent;
new_parent->children_.insert(this);
this->parent_ = new_parent;
}
}
@ -131,26 +125,26 @@ void Bandwidth::allocateBandwidth(
{
TR_ASSERT(tr_isDirection(dir));
tr_priority_t const priority_ = std::max(parent_priority, this->priority);
tr_priority_t const priority = std::max(parent_priority, this->priority_);
/* set the available bandwidth */
if (this->band_[dir].is_limited_)
{
uint64_t const nextPulseSpeed = this->band_[dir].desired_speed_bps_;
this->band_[dir].bytes_left_ = nextPulseSpeed * period_msec / 1000U;
uint64_t const next_pulse_speed = this->band_[dir].desired_speed_bps_;
this->band_[dir].bytes_left_ = next_pulse_speed * period_msec / 1000U;
}
/* add this bandwidth's peer, if any, to the peer pool */
if (this->peer_ != nullptr)
{
this->peer_->priority = priority_;
this->peer_->priority = priority;
peer_pool.push_back(this->peer_);
}
// traverse & repeat for the subtree
for (auto child : this->children_)
for (auto* child : this->children_)
{
child->allocateBandwidth(priority_, dir, period_msec, peer_pool);
child->allocateBandwidth(priority, dir, period_msec, peer_pool);
}
}
@ -172,11 +166,11 @@ void Bandwidth::phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir)
* out in a timely manner. */
size_t const increment = 3000;
int const bytesUsed = tr_peerIoFlush(peerArray[i], dir, increment);
int const bytes_used = tr_peerIoFlush(peerArray[i], dir, increment);
dbgmsg("peer #%d of %zu used %d bytes in this pass", i, n, bytesUsed);
dbgmsg("peer #%d of %zu used %d bytes in this pass", i, n, bytes_used);
if (bytesUsed != (int)increment)
if (bytes_used != int(increment))
{
/* peer is done writing for now; move it to the end of the list */
std::swap(peerArray[i], peerArray[n - 1]);
@ -197,7 +191,7 @@ void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
* 2. accumulate an array of all the peerIos from b and its subtree. */
this->allocateBandwidth(TR_PRI_LOW, dir, period_msec, tmp);
for (auto io : tmp)
for (auto* io : tmp)
{
tr_peerIoRef(io);
tr_peerIoFlushOutgoingProtocolMsgs(io);
@ -229,12 +223,12 @@ void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
* 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,
* or (2) the next Bandwidth::allocate () call, when we start over again. */
for (auto io : tmp)
for (auto* io : tmp)
{
tr_peerIoSetEnabled(io, dir, tr_peerIoHasBandwidthLeft(io, dir));
}
for (auto io : tmp)
for (auto* io : tmp)
{
tr_peerIoUnref(io);
}
@ -244,17 +238,17 @@ void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
****
***/
unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byteCount) const
unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const
{
TR_ASSERT(tr_isDirection(dir));
if (this->band_[dir].is_limited_)
{
byteCount = std::min(byteCount, this->band_[dir].bytes_left_);
byte_count = std::min(byte_count, this->band_[dir].bytes_left_);
/* if we're getting close to exceeding the speed limit,
* clamp down harder on the bytes available */
if (byteCount > 0)
if (byte_count > 0)
{
double current;
double desired;
@ -265,42 +259,42 @@ unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byteC
now = tr_time_msec();
}
current = this->getRawSpeed_Bps(now, TR_DOWN);
desired = this->getDesiredSpeed_Bps(TR_DOWN);
current = this->getRawSpeedBytesPerSecond(now, TR_DOWN);
desired = this->getDesiredSpeedBytesPerSecond(TR_DOWN);
r = desired >= 1 ? current / desired : 0;
if (r > 1.0)
{
byteCount = 0;
byte_count = 0;
}
else if (r > 0.9)
{
byteCount = static_cast<unsigned int>(byteCount * 0.8);
byte_count = static_cast<unsigned int>(byte_count * 0.8);
}
else if (r > 0.8)
{
byteCount = static_cast<unsigned int>(byteCount * 0.9);
byte_count = static_cast<unsigned int>(byte_count * 0.9);
}
}
}
if (this->parent_ != nullptr && this->band_[dir].honor_parent_limits_ && byteCount > 0)
if (this->parent_ != nullptr && this->band_[dir].honor_parent_limits_ && byte_count > 0)
{
byteCount = this->parent_->clamp(now, dir, byteCount);
byte_count = this->parent_->clamp(now, dir, byte_count);
}
return byteCount;
return byte_count;
}
void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool isPieceData, uint64_t now)
void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byte_count, bool is_piece_data, uint64_t now)
{
TR_ASSERT(tr_isDirection(dir));
Band* band = &this->band_[dir];
if (band->is_limited_ && isPieceData)
if (band->is_limited_ && is_piece_data)
{
band->bytes_left_ -= std::min(size_t{ band->bytes_left_ }, byteCount);
band->bytes_left_ -= std::min(size_t{ band->bytes_left_ }, byte_count);
}
#ifdef DEBUG_DIRECTION
@ -311,23 +305,23 @@ void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool
stderr,
"%p consumed %5zu bytes of %5s data... was %6zu, now %6zu left\n",
this,
byteCount,
isPieceData ? "piece" : "raw",
byte_count,
is_piece_data ? "piece" : "raw",
oldBytesLeft,
band_->bytesLeft);
}
#endif
notifyBandwidthConsumedBytes(now, &band->raw_, byteCount);
notifyBandwidthConsumedBytes(now, &band->raw_, byte_count);
if (isPieceData)
if (is_piece_data)
{
notifyBandwidthConsumedBytes(now, &band->piece_, byteCount);
notifyBandwidthConsumedBytes(now, &band->piece_, byte_count);
}
if (this->parent_ != nullptr)
{
this->parent_->notifyBandwidthConsumed(dir, byteCount, isPieceData, now);
this->parent_->notifyBandwidthConsumed(dir, byte_count, is_piece_data, now);
}
}

View File

@ -80,19 +80,24 @@ public:
this->setParent(nullptr);
}
Bandwidth& operator=(Bandwidth&&) = delete;
Bandwidth& operator=(Bandwidth) = delete;
Bandwidth(Bandwidth&&) = delete;
Bandwidth(Bandwidth&) = delete;
/**
* @brief Sets new peer, nullptr is allowed.
*/
void setPeer(tr_peerIo* newPeer)
constexpr void setPeer(tr_peerIo* peer)
{
this->peer_ = newPeer;
this->peer_ = peer;
}
/**
* @brief Notify the bandwidth object that some of its allocated bandwidth has been consumed.
* This is is usually invoked by the peer-io after a read or write.
*/
void notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool isPieceData, uint64_t now);
void notifyBandwidthConsumed(tr_direction dir, size_t byte_count, bool is_piece_data, uint64_t now);
/**
* @brief allocate the next period_msec's worth of bandwidth for the peer-ios to consume
@ -101,38 +106,38 @@ public:
void setParent(Bandwidth* newParent);
[[nodiscard]] tr_priority_t getPriority() const
[[nodiscard]] constexpr tr_priority_t getPriority() const
{
return this->priority;
return this->priority_;
}
void setPriority(tr_priority_t prio)
constexpr void setPriority(tr_priority_t prio)
{
this->priority = prio;
this->priority_ = prio;
}
/**
* @brief clamps byteCount down to a number that this bandwidth will allow to be consumed
*/
[[nodiscard]] unsigned int clamp(tr_direction dir, unsigned int byteCount) const
* @brief clamps byte_count down to a number that this bandwidth will allow to be consumed
*/
[[nodiscard]] unsigned int clamp(tr_direction dir, unsigned int byte_count) const
{
return this->clamp(0, dir, byteCount);
return this->clamp(0, dir, byte_count);
}
/** @brief Get the raw total of bytes read or sent by this bandwidth subtree. */
[[nodiscard]] unsigned int getRawSpeed_Bps(uint64_t const now, tr_direction const dir) const
[[nodiscard]] unsigned int getRawSpeedBytesPerSecond(uint64_t const now, tr_direction const dir) const
{
TR_ASSERT(tr_isDirection(dir));
return getSpeed_Bps(&this->band_[dir].raw_, HISTORY_MSEC, now);
return getSpeedBytesPerSecond(this->band_[dir].raw_, HistoryMSec, now);
}
/** @brief Get the number of piece data bytes read or sent by this bandwidth subtree. */
[[nodiscard]] unsigned int getPieceSpeed_Bps(uint64_t const now, tr_direction const dir) const
[[nodiscard]] unsigned int getPieceSpeedBytesPerSecond(uint64_t const now, tr_direction const dir) const
{
TR_ASSERT(tr_isDirection(dir));
return getSpeed_Bps(&this->band_[dir].piece_, HISTORY_MSEC, now);
return getSpeedBytesPerSecond(this->band_[dir].piece_, HistoryMSec, now);
}
/**
@ -140,19 +145,19 @@ public:
* @see Bandwidth::allocate
* @see Bandwidth::getDesiredSpeed
*/
constexpr bool setDesiredSpeed_Bps(tr_direction dir, unsigned int desiredSpeed)
constexpr bool setDesiredSpeedBytesPerSecond(tr_direction dir, unsigned int desired_speed)
{
unsigned int* value = &this->band_[dir].desired_speed_bps_;
bool const didChange = desiredSpeed != *value;
*value = desiredSpeed;
return didChange;
auto& value = this->band_[dir].desired_speed_bps_;
bool const did_change = desired_speed != value;
value = desired_speed;
return did_change;
}
/**
* @brief Get the desired speed for the bandwidth subtree.
* @see Bandwidth::setDesiredSpeed
*/
[[nodiscard]] constexpr double getDesiredSpeed_Bps(tr_direction dir) const
[[nodiscard]] constexpr double getDesiredSpeedBytesPerSecond(tr_direction dir) const
{
return this->band_[dir].desired_speed_bps_;
}
@ -160,12 +165,12 @@ public:
/**
* @brief Set whether or not this bandwidth should throttle its peer-io's speeds
*/
constexpr bool setLimited(tr_direction dir, bool isLimited)
constexpr bool setLimited(tr_direction dir, bool is_limited)
{
bool* value = &this->band_[dir].is_limited_;
bool const didChange = isLimited != *value;
*value = isLimited;
return didChange;
bool const did_change = is_limited != *value;
*value = is_limited;
return did_change;
}
/**
@ -182,12 +187,12 @@ public:
* But when we set a torrent's speed mode to TR_SPEEDLIMIT_UNLIMITED, then
* in that particular case we want to ignore the global speed limit...
*/
constexpr bool honorParentLimits(tr_direction direction, bool isEnabled)
constexpr bool honorParentLimits(tr_direction direction, bool is_enabled)
{
bool* value = &this->band_[direction].honor_parent_limits_;
bool const didChange = isEnabled != *value;
*value = isEnabled;
return didChange;
bool const did_change = is_enabled != *value;
*value = is_enabled;
return did_change;
}
[[nodiscard]] constexpr bool areParentLimitsHonored(tr_direction direction) const
@ -197,10 +202,10 @@ public:
return this->band_[direction].honor_parent_limits_;
}
static constexpr size_t HISTORY_MSEC = 2000U;
static constexpr size_t INTERVAL_MSEC = HISTORY_MSEC;
static constexpr size_t GRANULARITY_MSEC = 200;
static constexpr size_t HISTORY_SIZE = (INTERVAL_MSEC / GRANULARITY_MSEC);
static constexpr size_t HistoryMSec = 2000U;
static constexpr size_t IntervalMSec = HistoryMSec;
static constexpr size_t GranularityMSec = 200;
static constexpr size_t HistorySize = (IntervalMSec / GranularityMSec);
struct RateControl
{
@ -209,7 +214,7 @@ public:
uint64_t date_;
uint64_t size_;
};
std::array<Transfer, HISTORY_SIZE> transfers_;
std::array<Transfer, HistorySize> transfers_;
uint64_t cache_time_;
unsigned int cache_val_;
int newest_;
@ -226,13 +231,13 @@ public:
};
private:
static unsigned int getSpeed_Bps(RateControl const* r, unsigned int interval_msec, uint64_t now);
static unsigned int getSpeedBytesPerSecond(RateControl& r, unsigned int interval_msec, uint64_t now);
static void notifyBandwidthConsumedBytes(uint64_t now, RateControl* r, size_t size);
[[nodiscard]] unsigned int clamp(uint64_t now, tr_direction dir, unsigned int byteCount) const;
[[nodiscard]] unsigned int clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const;
static void phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir);
static void phaseOne(std::vector<tr_peerIo*>& peer_array, tr_direction dir);
void allocateBandwidth(
tr_priority_t parent_priority,
@ -240,11 +245,11 @@ private:
unsigned int period_msec,
std::vector<tr_peerIo*>& peer_pool);
std::array<Band, 2> band_;
Bandwidth* parent_;
mutable std::array<Band, 2> band_ = {};
Bandwidth* parent_ = nullptr;
std::unordered_set<Bandwidth*> children_;
tr_peerIo* peer_;
tr_priority_t priority = 0;
tr_peerIo* peer_ = nullptr;
tr_priority_t priority_ = 0;
};
/* @} */

View File

@ -1067,7 +1067,7 @@ static unsigned int getDesiredOutputBufferSize(tr_peerIo const* io, uint64_t now
* being large enough to hold the next 20 seconds' worth of input,
* or a few blocks, whichever is bigger.
* It's okay to tweak this as needed */
unsigned int const currentSpeed_Bps = io->bandwidth->getPieceSpeed_Bps(now, TR_UP);
unsigned int const currentSpeed_Bps = io->bandwidth->getPieceSpeedBytesPerSecond(now, TR_UP);
unsigned int const period = 15U; /* arbitrary */
/* the 3 is arbitrary; the .5 is to leave room for messages */
static auto const ceiling = (unsigned int)(MAX_BLOCK_SIZE * 3.5);

View File

@ -345,7 +345,7 @@ static inline bool tr_peerIoHasBandwidthLeft(tr_peerIo const* io, tr_direction d
static inline unsigned int tr_peerIoGetPieceSpeed_Bps(tr_peerIo const* io, uint64_t now, tr_direction dir)
{
return io->bandwidth->getPieceSpeed_Bps(now, dir);
return io->bandwidth->getPieceSpeedBytesPerSecond(now, dir);
}
/**

View File

@ -3181,8 +3181,8 @@ static inline bool isBandwidthMaxedOut(Bandwidth const* b, uint64_t const now_ms
}
else
{
unsigned int const got = b->getPieceSpeed_Bps(now_msec, dir);
unsigned int const want = b->getDesiredSpeed_Bps(dir);
unsigned int const got = b->getPieceSpeedBytesPerSecond(now_msec, dir);
unsigned int const want = b->getDesiredSpeedBytesPerSecond(dir);
return got >= want;
}
}

View File

@ -1488,7 +1488,7 @@ static void updateBandwidth(tr_session* session, tr_direction dir)
session->bandwidth->setLimited(dir, isLimited && !zeroCase);
session->bandwidth->setDesiredSpeed_Bps(dir, limit_Bps);
session->bandwidth->setDesiredSpeedBytesPerSecond(dir, limit_Bps);
}
enum
@ -1881,12 +1881,12 @@ bool tr_sessionGetDeleteSource(tr_session const* session)
unsigned int tr_sessionGetPieceSpeed_Bps(tr_session const* session, tr_direction dir)
{
return tr_isSession(session) ? session->bandwidth->getPieceSpeed_Bps(0, dir) : 0;
return tr_isSession(session) ? session->bandwidth->getPieceSpeedBytesPerSecond(0, dir) : 0;
}
static unsigned int tr_sessionGetRawSpeed_Bps(tr_session const* session, tr_direction dir)
{
return tr_isSession(session) ? session->bandwidth->getRawSpeed_Bps(0, dir) : 0;
return tr_isSession(session) ? session->bandwidth->getRawSpeedBytesPerSecond(0, dir) : 0;
}
double tr_sessionGetRawSpeed_KBps(tr_session const* session, tr_direction dir)

View File

@ -196,7 +196,7 @@ void tr_torrentSetSpeedLimit_Bps(tr_torrent* tor, tr_direction dir, unsigned int
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(tr_isDirection(dir));
if (tor->bandwidth->setDesiredSpeed_Bps(dir, Bps))
if (tor->bandwidth->setDesiredSpeedBytesPerSecond(dir, Bps))
{
tr_torrentSetDirty(tor);
}
@ -212,7 +212,7 @@ unsigned int tr_torrentGetSpeedLimit_Bps(tr_torrent const* tor, tr_direction dir
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(tr_isDirection(dir));
return tor->bandwidth->getDesiredSpeed_Bps(dir);
return tor->bandwidth->getDesiredSpeedBytesPerSecond(dir);
}
unsigned int tr_torrentGetSpeedLimit_KBps(tr_torrent const* tor, tr_direction dir)
@ -1294,10 +1294,10 @@ tr_stat const* tr_torrentStat(tr_torrent* tor)
s->peersFrom[i] = swarm_stats.peerFromCount[i];
}
s->rawUploadSpeed_KBps = toSpeedKBps(tor->bandwidth->getRawSpeed_Bps(now, TR_UP));
s->rawDownloadSpeed_KBps = toSpeedKBps(tor->bandwidth->getRawSpeed_Bps(now, TR_DOWN));
pieceUploadSpeed_Bps = tor->bandwidth->getPieceSpeed_Bps(now, TR_UP);
pieceDownloadSpeed_Bps = tor->bandwidth->getPieceSpeed_Bps(now, TR_DOWN);
s->rawUploadSpeed_KBps = toSpeedKBps(tor->bandwidth->getRawSpeedBytesPerSecond(now, TR_UP));
s->rawDownloadSpeed_KBps = toSpeedKBps(tor->bandwidth->getRawSpeedBytesPerSecond(now, TR_DOWN));
pieceUploadSpeed_Bps = tor->bandwidth->getPieceSpeedBytesPerSecond(now, TR_UP);
pieceDownloadSpeed_Bps = tor->bandwidth->getPieceSpeedBytesPerSecond(now, TR_DOWN);
s->pieceUploadSpeed_KBps = toSpeedKBps(pieceUploadSpeed_Bps);
s->pieceDownloadSpeed_KBps = toSpeedKBps(pieceDownloadSpeed_Bps);

View File

@ -93,7 +93,7 @@ public:
if (direction == TR_DOWN)
{
is_active = !std::empty(tasks);
Bps = bandwidth.getPieceSpeed_Bps(now, direction);
Bps = bandwidth.getPieceSpeedBytesPerSecond(now, direction);
}
if (setme_Bps != nullptr)