diff --git a/libtransmission/bandwidth.cc b/libtransmission/bandwidth.cc index 10bf1f36a..aa752cc0f 100644 --- a/libtransmission/bandwidth.cc +++ b/libtransmission/bandwidth.cc @@ -23,75 +23,75 @@ **** ***/ -unsigned int tr_bandwidth::getSpeed_Bps(struct bratecontrol const* r, unsigned int interval_msec, uint64_t now) +unsigned int Bandwidth::getSpeed_Bps(RateControl const* 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 = (struct bratecontrol*)r; + 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 */ } - 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; + rvolatile->cache_val_ = (unsigned int)(bytes * 1000U / interval_msec); + rvolatile->cache_time_ = now; } - return r->cache_val; + return r->cache_val_; } -void tr_bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, struct bratecontrol* r, size_t size) +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_ + GRANULARITY_MSEC >= now) { - r->transfers[r->newest].size += size; + r->transfers_[r->newest_].size_ += size; } else { - if (++r->newest == HISTORY_SIZE) + if (++r->newest_ == HISTORY_SIZE) { - r->newest = 0; + r->newest_ = 0; } - r->transfers[r->newest].date = now; - r->transfers[r->newest].size = size; + r->transfers_[r->newest_].date_ = now; + r->transfers_[r->newest_].size_ = size; } /* invalidate cache_val*/ - r->cache_time = 0; + r->cache_time_ = 0; } /*** **** ***/ -tr_bandwidth::tr_bandwidth(tr_bandwidth* newParent) - : band{} - , parent{ nullptr } - , children{} - , peer{ nullptr } +Bandwidth::Bandwidth(Bandwidth* newParent) + : band_{} + , parent_{ nullptr } + , children_{} + , peer_{ nullptr } { - this->children = {}; - this->band[TR_UP].honorParentLimits = true; - this->band[TR_DOWN].honorParentLimits = true; + this->children_ = {}; + this->band_[TR_UP].honor_parent_limits_ = true; + this->band_[TR_DOWN].honor_parent_limits_ = true; this->setParent(newParent); } @@ -99,23 +99,23 @@ tr_bandwidth::tr_bandwidth(tr_bandwidth* newParent) **** ***/ -void tr_bandwidth::setParent(tr_bandwidth* newParent) +void Bandwidth::setParent(Bandwidth* newParent) { TR_ASSERT(this != newParent); - if (this->parent != nullptr) + if (this->parent_ != nullptr) { - this->parent->children.erase(this); - this->parent = nullptr; + this->parent_->children_.erase(this); + this->parent_ = nullptr; } if (newParent != nullptr) { - TR_ASSERT(newParent->parent != this); - TR_ASSERT(newParent->children.find(this) == newParent->children.end()); // does not exist + TR_ASSERT(newParent->parent_ != this); + TR_ASSERT(newParent->children_.find(this) == newParent->children_.end()); // does not exist - newParent->children.insert(this); - this->parent = newParent; + newParent->children_.insert(this); + this->parent_ = newParent; } } @@ -123,7 +123,7 @@ void tr_bandwidth::setParent(tr_bandwidth* newParent) **** ***/ -void tr_bandwidth::allocateBandwidth( +void Bandwidth::allocateBandwidth( tr_priority_t parent_priority, tr_direction dir, unsigned int period_msec, @@ -134,27 +134,27 @@ void tr_bandwidth::allocateBandwidth( tr_priority_t const priority_ = std::max(parent_priority, this->priority); /* set the available bandwidth */ - if (this->band[dir].isLimited) + if (this->band_[dir].is_limited_) { - uint64_t const nextPulseSpeed = this->band[dir].desiredSpeed_Bps; - this->band[dir].bytesLeft = nextPulseSpeed * period_msec / 1000U; + uint64_t const nextPulseSpeed = this->band_[dir].desired_speed_bps_; + this->band_[dir].bytes_left_ = nextPulseSpeed * period_msec / 1000U; } /* add this bandwidth's peer, if any, to the peer pool */ - if (this->peer != nullptr) + if (this->peer_ != nullptr) { - this->peer->priority = priority_; - peer_pool.push_back(this->peer); + 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); } } -void tr_bandwidth::phaseOne(std::vector& peerArray, tr_direction dir) +void Bandwidth::phaseOne(std::vector& peerArray, tr_direction dir) { /* First phase of IO. Tries to distribute bandwidth fairly to keep faster * peers from starving the others. Loop through the peers, giving each a @@ -185,7 +185,7 @@ void tr_bandwidth::phaseOne(std::vector& peerArray, tr_direction dir } } -void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec) +void Bandwidth::allocate(tr_direction dir, unsigned int period_msec) { std::vector tmp; std::vector low; @@ -228,7 +228,7 @@ void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec) /* 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, - * or (2) the next tr_bandwidth::allocate () call, when we start over again. */ + * or (2) the next Bandwidth::allocate () call, when we start over again. */ for (auto io : tmp) { tr_peerIoSetEnabled(io, dir, tr_peerIoHasBandwidthLeft(io, dir)); @@ -244,13 +244,13 @@ void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec) **** ***/ -unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byteCount) const +unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byteCount) const { TR_ASSERT(tr_isDirection(dir)); - if (this->band[dir].isLimited) + if (this->band_[dir].is_limited_) { - byteCount = std::min(byteCount, this->band[dir].bytesLeft); + byteCount = std::min(byteCount, this->band_[dir].bytes_left_); /* if we're getting close to exceeding the speed limit, * clamp down harder on the bytes available */ @@ -284,23 +284,23 @@ unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int by } } - if (this->parent != nullptr && this->band[dir].honorParentLimits && byteCount > 0) + if (this->parent_ != nullptr && this->band_[dir].honor_parent_limits_ && byteCount > 0) { - byteCount = this->parent->clamp(now, dir, byteCount); + byteCount = this->parent_->clamp(now, dir, byteCount); } return byteCount; } -void tr_bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool isPieceData, uint64_t now) +void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, bool isPieceData, uint64_t now) { TR_ASSERT(tr_isDirection(dir)); - struct tr_band* band_ = &this->band[dir]; + Band* band = &this->band_[dir]; - if (band_->isLimited && isPieceData) + if (band->is_limited_ && isPieceData) { - band_->bytesLeft -= std::min(size_t{ band_->bytesLeft }, byteCount); + band->bytes_left_ -= std::min(size_t{ band->bytes_left_ }, byteCount); } #ifdef DEBUG_DIRECTION @@ -319,15 +319,15 @@ void tr_bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byteCount, b #endif - notifyBandwidthConsumedBytes(now, &band_->raw, byteCount); + notifyBandwidthConsumedBytes(now, &band->raw_, byteCount); if (isPieceData) { - notifyBandwidthConsumedBytes(now, &band_->piece, byteCount); + notifyBandwidthConsumedBytes(now, &band->piece_, byteCount); } - if (this->parent != nullptr) + if (this->parent_ != nullptr) { - this->parent->notifyBandwidthConsumed(dir, byteCount, isPieceData, now); + this->parent_->notifyBandwidthConsumed(dir, byteCount, isPieceData, now); } } diff --git a/libtransmission/bandwidth.h b/libtransmission/bandwidth.h index 58c4f6ac5..0f6ccdc34 100644 --- a/libtransmission/bandwidth.h +++ b/libtransmission/bandwidth.h @@ -26,39 +26,6 @@ struct tr_peerIo; * @{ */ -/* these are PRIVATE IMPLEMENTATION details that should not be touched. - * it's included in the header for inlining and composition. */ -constexpr size_t HISTORY_MSEC = 2000U; -constexpr size_t INTERVAL_MSEC = HISTORY_MSEC; -constexpr size_t GRANULARITY_MSEC = 200; -constexpr size_t HISTORY_SIZE = (INTERVAL_MSEC / GRANULARITY_MSEC); - -/* these are PRIVATE IMPLEMENTATION details that should not be touched. - * it's included in the header for inlining and composition. */ -struct bratecontrol -{ - int newest; - struct - { - uint64_t date; - uint64_t size; - } transfers[HISTORY_SIZE]; - uint64_t cache_time; - unsigned int cache_val; -}; - -/* these are PRIVATE IMPLEMENTATION details that should not be touched. - * it's included in the header for inlining and composition. */ -struct tr_band -{ - bool isLimited; - bool honorParentLimits; - unsigned int bytesLeft; - unsigned int desiredSpeed_Bps; - struct bratecontrol raw; - struct bratecontrol piece; -}; - /** * Bandwidth is an object for measuring and constraining bandwidth speeds. * @@ -86,29 +53,29 @@ struct tr_band * * CONSTRAINING * - * Call tr_bandwidth::allocate() periodically. tr_bandwidth knows its current + * Call Bandwidth::allocate() periodically. tr_bandwidth knows its current * speed and will decide how many bytes to make available over the * user-specified period to reach the user-specified desired speed. * If appropriate, it notifies its peer-ios that new bandwidth is available. * - * tr_bandwidth::allocate() operates on the tr_bandwidth subtree, so usually + * Bandwidth::allocate() operates on the tr_bandwidth subtree, so usually * you'll only need to invoke it for the top-level tr_session bandwidth. * * The peer-ios all have a pointer to their associated tr_bandwidth object, - * and call tr_bandwidth::clamp() before performing I/O to see how much + * and call Bandwidth::clamp() before performing I/O to see how much * bandwidth they can safely use. */ -struct tr_bandwidth +struct Bandwidth { public: - explicit tr_bandwidth(tr_bandwidth* newParent); + explicit Bandwidth(Bandwidth* newParent); - tr_bandwidth() - : tr_bandwidth(nullptr) + Bandwidth() + : Bandwidth(nullptr) { } - ~tr_bandwidth() + ~Bandwidth() { this->setParent(nullptr); } @@ -118,7 +85,7 @@ public: */ void setPeer(tr_peerIo* newPeer) { - this->peer = newPeer; + this->peer_ = newPeer; } /** @@ -132,7 +99,7 @@ public: */ void allocate(tr_direction dir, unsigned int period_msec); - void setParent(tr_bandwidth* newParent); + void setParent(Bandwidth* newParent); [[nodiscard]] tr_priority_t getPriority() const { @@ -157,7 +124,7 @@ public: { TR_ASSERT(tr_isDirection(dir)); - return getSpeed_Bps(&this->band[dir].raw, HISTORY_MSEC, now); + return getSpeed_Bps(&this->band_[dir].raw_, HISTORY_MSEC, now); } /** @brief Get the number of piece data bytes read or sent by this bandwidth subtree. */ @@ -165,17 +132,17 @@ public: { TR_ASSERT(tr_isDirection(dir)); - return getSpeed_Bps(&this->band[dir].piece, HISTORY_MSEC, now); + return getSpeed_Bps(&this->band_[dir].piece_, HISTORY_MSEC, now); } /** * @brief Set the desired speed for this bandwidth subtree. - * @see tr_bandwidth::allocate - * @see tr_bandwidth::getDesiredSpeed + * @see Bandwidth::allocate + * @see Bandwidth::getDesiredSpeed */ constexpr bool setDesiredSpeed_Bps(tr_direction dir, unsigned int desiredSpeed) { - unsigned int* value = &this->band[dir].desiredSpeed_Bps; + unsigned int* value = &this->band_[dir].desired_speed_bps_; bool const didChange = desiredSpeed != *value; *value = desiredSpeed; return didChange; @@ -183,11 +150,11 @@ public: /** * @brief Get the desired speed for the bandwidth subtree. - * @see tr_bandwidth::setDesiredSpeed + * @see Bandwidth::setDesiredSpeed */ [[nodiscard]] constexpr double getDesiredSpeed_Bps(tr_direction dir) const { - return this->band[dir].desiredSpeed_Bps; + return this->band_[dir].desired_speed_bps_; } /** @@ -195,7 +162,7 @@ public: */ constexpr bool setLimited(tr_direction dir, bool isLimited) { - bool* value = &this->band[dir].isLimited; + bool* value = &this->band_[dir].is_limited_; bool const didChange = isLimited != *value; *value = isLimited; return didChange; @@ -206,7 +173,7 @@ public: */ [[nodiscard]] constexpr bool isLimited(tr_direction dir) const { - return this->band[dir].isLimited; + return this->band_[dir].is_limited_; } /** @@ -217,7 +184,7 @@ public: */ constexpr bool honorParentLimits(tr_direction direction, bool isEnabled) { - bool* value = &this->band[direction].honorParentLimits; + bool* value = &this->band_[direction].honor_parent_limits_; bool const didChange = isEnabled != *value; *value = isEnabled; return didChange; @@ -227,13 +194,41 @@ public: { TR_ASSERT(tr_isDirection(direction)); - return this->band[direction].honorParentLimits; + return this->band_[direction].honor_parent_limits_; } -private: - static unsigned int getSpeed_Bps(struct bratecontrol const* r, unsigned int interval_msec, uint64_t now); + 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 void notifyBandwidthConsumedBytes(uint64_t now, struct bratecontrol* r, size_t size); + struct RateControl + { + int newest_; + struct Transfer + { + uint64_t date_; + uint64_t size_; + }; + std::array transfers_; + uint64_t cache_time_; + unsigned int cache_val_; + }; + + struct Band + { + bool is_limited_; + bool honor_parent_limits_; + unsigned int bytes_left_; + unsigned int desired_speed_bps_; + RateControl raw_; + RateControl piece_; + }; + +private: + static unsigned int getSpeed_Bps(RateControl const* 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; @@ -246,10 +241,10 @@ private: std::vector& peer_pool); tr_priority_t priority = 0; - std::array band; - struct tr_bandwidth* parent; - std::unordered_set children; - struct tr_peerIo* peer; + std::array band_; + Bandwidth* parent_; + std::unordered_set children_; + tr_peerIo* peer_; }; /* @} */ diff --git a/libtransmission/peer-io.cc b/libtransmission/peer-io.cc index 049002465..fabbe2b5e 100644 --- a/libtransmission/peer-io.cc +++ b/libtransmission/peer-io.cc @@ -643,7 +643,7 @@ static auto dummy_utp_function_table = UTPFunctionTable{ static tr_peerIo* tr_peerIoNew( tr_session* session, - tr_bandwidth* parent, + Bandwidth* parent, tr_address const* addr, tr_port port, uint8_t const* torrentHash, @@ -680,7 +680,7 @@ static tr_peerIo* tr_peerIoNew( io->timeCreated = tr_time(); io->inbuf = evbuffer_new(); io->outbuf = evbuffer_new(); - io->bandwidth = new tr_bandwidth(parent); + io->bandwidth = new Bandwidth(parent); io->bandwidth->setPeer(io); dbgmsg(io, "bandwidth is %p; its parent is %p", (void*)&io->bandwidth, (void*)parent); @@ -719,7 +719,7 @@ static tr_peerIo* tr_peerIoNew( tr_peerIo* tr_peerIoNewIncoming( tr_session* session, - tr_bandwidth* parent, + Bandwidth* parent, tr_address const* addr, tr_port port, struct tr_peer_socket const socket) @@ -732,7 +732,7 @@ tr_peerIo* tr_peerIoNewIncoming( tr_peerIo* tr_peerIoNewOutgoing( tr_session* session, - tr_bandwidth* parent, + Bandwidth* parent, tr_address const* addr, tr_port port, uint8_t const* torrentHash, diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index 96df3b82f..297ad78bd 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -26,7 +26,7 @@ #include "utils.h" /* tr_time() */ struct evbuffer; -struct tr_bandwidth; +struct Bandwidth; struct tr_datatype; struct tr_peerIo; @@ -93,7 +93,7 @@ struct tr_peerIo // Changed to non-owning pointer temporarily till tr_peerIo becomes C++-constructible and destructible // TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value - struct tr_bandwidth* bandwidth; + Bandwidth* bandwidth; tr_crypto crypto; struct evbuffer* inbuf; @@ -110,7 +110,7 @@ struct tr_peerIo tr_peerIo* tr_peerIoNewOutgoing( tr_session* session, - struct tr_bandwidth* parent, + Bandwidth* parent, struct tr_address const* addr, tr_port port, uint8_t const* torrentHash, @@ -119,7 +119,7 @@ tr_peerIo* tr_peerIoNewOutgoing( tr_peerIo* tr_peerIoNewIncoming( tr_session* session, - struct tr_bandwidth* parent, + Bandwidth* parent, struct tr_address const* addr, tr_port port, struct tr_peer_socket const socket); @@ -299,7 +299,7 @@ void tr_peerIoDrain(tr_peerIo* io, struct evbuffer* inbuf, size_t byteCount); size_t tr_peerIoGetWriteBufferSpace(tr_peerIo const* io, uint64_t now); -static inline void tr_peerIoSetParent(tr_peerIo* io, struct tr_bandwidth* parent) +static inline void tr_peerIoSetParent(tr_peerIo* io, Bandwidth* parent) { TR_ASSERT(tr_isPeerIo(io)); diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 59b587959..10842b5f4 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -3184,7 +3184,7 @@ static int getRate(tr_torrent const* tor, struct peer_atom* atom, uint64_t now) return Bps; } -static inline bool isBandwidthMaxedOut(tr_bandwidth const* b, uint64_t const now_msec, tr_direction dir) +static inline bool isBandwidthMaxedOut(Bandwidth const* b, uint64_t const now_msec, tr_direction dir) { if (!b->isLimited(dir)) { diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 6b6a790d6..522e1cda1 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -636,7 +636,7 @@ tr_session* tr_sessionInit(char const* configDir, bool messageQueuingEnabled, tr session->cache = tr_cacheNew(1024 * 1024 * 2); session->magicNumber = SESSION_MAGIC_NUMBER; session->session_id = tr_session_id_new(); - session->bandwidth = new tr_bandwidth(nullptr); + session->bandwidth = new Bandwidth(nullptr); tr_variantInitList(&session->removedTorrents, 0); /* nice to start logging at the very beginning */ diff --git a/libtransmission/session.h b/libtransmission/session.h index db5a562aa..297265216 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -242,7 +242,7 @@ struct tr_session /* monitors the "global pool" speeds */ // Changed to non-owning pointer temporarily till tr_session becomes C++-constructible and destructible // TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value - struct tr_bandwidth* bandwidth; + Bandwidth* bandwidth; float desiredRatio; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 578a9e23b..3f2c7819b 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -881,7 +881,7 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) tor->incompleteDir = tr_strdup(dir); } - tor->bandwidth = new tr_bandwidth(session->bandwidth); + tor->bandwidth = new Bandwidth(session->bandwidth); tor->bandwidth->setPriority(tr_ctorGetBandwidthPriority(ctor)); tor->error = TR_STAT_OK; diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 09d8ce3b3..dc1beb345 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -260,7 +260,7 @@ struct tr_torrent // Changed to non-owning pointer temporarily till tr_torrent becomes C++-constructible and destructible // TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value - struct tr_bandwidth* bandwidth; + Bandwidth* bandwidth; tr_swarm* swarm; diff --git a/libtransmission/webseed.cc b/libtransmission/webseed.cc index 0f7a84ae5..6c9edc900 100644 --- a/libtransmission/webseed.cc +++ b/libtransmission/webseed.cc @@ -109,7 +109,7 @@ public: tr_peer_callback const callback; void* const callback_data; - tr_bandwidth bandwidth; + Bandwidth bandwidth; std::set tasks; struct event* timer = nullptr; int consecutive_failures = 0;