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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-02 17:46:22 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "ptrarray.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "utils.h" /* tr_new(), tr_free() */
|
2009-01-02 17:46:22 +00:00
|
|
|
|
|
|
|
struct tr_peerIo;
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup networked_io Networked IO
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-01-02 17:46:22 +00:00
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* it's included in the header for inlining and composition. */
|
|
|
|
enum
|
|
|
|
{
|
2019-03-17 04:07:48 +00:00
|
|
|
HISTORY_MSEC = 2000U,
|
2017-04-19 12:04:45 +00:00
|
|
|
INTERVAL_MSEC = HISTORY_MSEC,
|
|
|
|
GRANULARITY_MSEC = 200,
|
|
|
|
HISTORY_SIZE = (INTERVAL_MSEC / GRANULARITY_MSEC),
|
|
|
|
BANDWIDTH_MAGIC_NUMBER = 43143
|
2009-01-02 17:46:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* it's included in the header for inlining and composition. */
|
|
|
|
struct bratecontrol
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int newest;
|
|
|
|
struct
|
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
uint64_t date;
|
|
|
|
uint64_t size;
|
2021-08-15 09:41:48 +00:00
|
|
|
} transfers[HISTORY_SIZE];
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t cache_time;
|
|
|
|
unsigned int cache_val;
|
2009-01-02 17:46:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* it's included in the header for inlining and composition. */
|
|
|
|
struct tr_band
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isLimited;
|
|
|
|
bool honorParentLimits;
|
|
|
|
unsigned int bytesLeft;
|
|
|
|
unsigned int desiredSpeed_Bps;
|
|
|
|
struct bratecontrol raw;
|
|
|
|
struct bratecontrol piece;
|
2009-01-02 17:46:22 +00:00
|
|
|
};
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/**
|
|
|
|
* Bandwidth is an object for measuring and constraining bandwidth speeds.
|
|
|
|
*
|
|
|
|
* Bandwidth objects can be "stacked" so that a peer can be made to obey
|
|
|
|
* multiple constraints (for example, obeying the global speed limit and a
|
|
|
|
* per-torrent speed limit).
|
|
|
|
*
|
|
|
|
* HIERARCHY
|
|
|
|
*
|
|
|
|
* Transmission's bandwidth hierarchy is a tree.
|
|
|
|
* At the top is the global bandwidth object owned by tr_session.
|
|
|
|
* Its children are per-torrent bandwidth objects owned by tr_torrent.
|
|
|
|
* Underneath those are per-peer bandwidth objects owned by tr_peer.
|
|
|
|
*
|
|
|
|
* tr_session also owns a tr_handshake's bandwidths, so that the handshake
|
2010-12-27 19:18:17 +00:00
|
|
|
* I/O can be counted in the global raw totals. When the handshake is done,
|
2008-11-25 21:35:17 +00:00
|
|
|
* the bandwidth's ownership passes to a tr_peer.
|
2009-08-10 20:04:08 +00:00
|
|
|
*
|
2008-11-25 21:35:17 +00:00
|
|
|
* MEASURING
|
|
|
|
*
|
|
|
|
* When you ask a bandwidth object for its speed, it gives the speed of the
|
2010-12-27 19:18:17 +00:00
|
|
|
* subtree underneath it as well. So you can get Transmission's overall
|
2008-11-25 21:35:17 +00:00
|
|
|
* speed by quering tr_session's bandwidth, per-torrent speeds by asking
|
|
|
|
* tr_torrent's bandwidth, and per-peer speeds by asking tr_peer's bandwidth.
|
|
|
|
*
|
|
|
|
* CONSTRAINING
|
2009-08-10 20:04:08 +00:00
|
|
|
*
|
2017-04-21 07:40:57 +00:00
|
|
|
* Call tr_bandwidthAllocate() periodically. tr_bandwidth knows its current
|
2008-11-25 21:35:17 +00:00
|
|
|
* speed and will decide how many bytes to make available over the
|
|
|
|
* user-specified period to reach the user-specified desired speed.
|
2008-12-29 21:19:31 +00:00
|
|
|
* If appropriate, it notifies its peer-ios that new bandwidth is available.
|
2009-08-10 20:04:08 +00:00
|
|
|
*
|
2017-04-21 07:40:57 +00:00
|
|
|
* tr_bandwidthAllocate() operates on the tr_bandwidth subtree, so usually
|
2008-11-25 21:35:17 +00:00
|
|
|
* you'll only need to invoke it for the top-level tr_session bandwidth.
|
|
|
|
*
|
2008-12-29 21:19:31 +00:00
|
|
|
* The peer-ios all have a pointer to their associated tr_bandwidth object,
|
2017-04-21 07:40:57 +00:00
|
|
|
* and call tr_bandwidthClamp() before performing I/O to see how much
|
2008-11-25 21:35:17 +00:00
|
|
|
* bandwidth they can safely use.
|
|
|
|
*/
|
2009-01-02 17:46:22 +00:00
|
|
|
typedef struct tr_bandwidth
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* it's included in the header for inlining and composition. */
|
|
|
|
|
|
|
|
struct tr_band band[2];
|
|
|
|
struct tr_bandwidth* parent;
|
|
|
|
tr_priority_t priority;
|
|
|
|
int magicNumber;
|
|
|
|
unsigned int uniqueKey;
|
|
|
|
tr_session* session;
|
|
|
|
tr_ptrArray children; /* struct tr_bandwidth */
|
|
|
|
struct tr_peerIo* peer;
|
2021-08-15 09:41:48 +00:00
|
|
|
} tr_bandwidth;
|
2008-11-24 04:21:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthConstruct(tr_bandwidth* bandwidth, tr_session* session, tr_bandwidth* parent);
|
2009-01-02 19:56:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthDestruct(tr_bandwidth* bandwidth);
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
/** @brief test to see if the pointer refers to a live bandwidth object */
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_isBandwidth(tr_bandwidth const* b)
|
2009-01-02 17:46:22 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return b != nullptr && b->magicNumber == BANDWIDTH_MAGIC_NUMBER;
|
2009-01-02 17:46:22 +00:00
|
|
|
}
|
2008-12-21 18:15:00 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/******
|
|
|
|
*******
|
|
|
|
******/
|
2008-11-24 04:21:23 +00:00
|
|
|
|
|
|
|
/**
|
2010-07-03 00:25:22 +00:00
|
|
|
* @brief Set the desired speed for this bandwidth subtree.
|
2008-11-25 21:35:17 +00:00
|
|
|
* @see tr_bandwidthAllocate
|
|
|
|
* @see tr_bandwidthGetDesiredSpeed
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline bool tr_bandwidthSetDesiredSpeed_Bps(tr_bandwidth* bandwidth, tr_direction dir, unsigned int desiredSpeed)
|
2009-01-02 17:46:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
unsigned int* value = &bandwidth->band[dir].desiredSpeed_Bps;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const didChange = desiredSpeed != *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
*value = desiredSpeed;
|
|
|
|
return didChange;
|
2009-01-02 17:46:22 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/**
|
2010-07-03 00:25:22 +00:00
|
|
|
* @brief Get the desired speed for the bandwidth subtree.
|
2008-11-25 21:35:17 +00:00
|
|
|
* @see tr_bandwidthSetDesiredSpeed
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline double tr_bandwidthGetDesiredSpeed_Bps(tr_bandwidth const* bandwidth, tr_direction dir)
|
2009-01-02 17:46:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return bandwidth->band[dir].desiredSpeed_Bps;
|
2009-01-02 17:46:22 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/**
|
2008-12-29 21:19:31 +00:00
|
|
|
* @brief Set whether or not this bandwidth should throttle its peer-io's speeds
|
2008-11-25 21:35:17 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline bool tr_bandwidthSetLimited(tr_bandwidth* bandwidth, tr_direction dir, bool isLimited)
|
2009-01-02 17:46:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool* value = &bandwidth->band[dir].isLimited;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const didChange = isLimited != *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
*value = isLimited;
|
|
|
|
return didChange;
|
2009-01-02 17:46:22 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/**
|
2008-12-29 21:19:31 +00:00
|
|
|
* @return nonzero if this bandwidth throttles its peer-ios speeds
|
2008-11-25 21:35:17 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_bandwidthIsLimited(tr_bandwidth const* bandwidth, tr_direction dir)
|
2009-01-02 17:46:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return bandwidth->band[dir].isLimited;
|
2009-01-02 17:46:22 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
|
|
|
/**
|
2008-12-29 21:19:31 +00:00
|
|
|
* @brief allocate the next period_msec's worth of bandwidth for the peer-ios to consume
|
2008-11-25 21:35:17 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthAllocate(tr_bandwidth* bandwidth, tr_direction direction, unsigned int period_msec);
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/**
|
|
|
|
* @brief clamps byteCount down to a number that this bandwidth will allow to be consumed
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_bandwidthClamp(tr_bandwidth const* bandwidth, tr_direction direction, unsigned int byteCount);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
/******
|
|
|
|
*******
|
|
|
|
******/
|
|
|
|
|
2009-01-02 17:46:22 +00:00
|
|
|
/** @brief Get the raw total of bytes read or sent by this bandwidth subtree. */
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_bandwidthGetRawSpeed_Bps(tr_bandwidth const* bandwidth, uint64_t const now, tr_direction const direction);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2009-01-02 17:46:22 +00:00
|
|
|
/** @brief Get the number of piece data bytes read or sent by this bandwidth subtree. */
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_bandwidthGetPieceSpeed_Bps(tr_bandwidth const* bandwidth, uint64_t const now, tr_direction const direction);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Notify the bandwidth object that some of its allocated bandwidth has been consumed.
|
2008-12-29 21:19:31 +00:00
|
|
|
* This is is usually invoked by the peer-io after a read or write.
|
2008-11-25 21:35:17 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthUsed(tr_bandwidth* bandwidth, tr_direction direction, size_t byteCount, bool isPieceData, uint64_t now);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
/******
|
|
|
|
*******
|
|
|
|
******/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthSetParent(tr_bandwidth* bandwidth, tr_bandwidth* parent);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Almost all the time we do want to honor a parents' bandwidth cap, so that
|
|
|
|
* (for example) a peer is constrained by a per-torrent cap and the global cap.
|
|
|
|
* 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...
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline bool tr_bandwidthHonorParentLimits(tr_bandwidth* bandwidth, tr_direction direction, bool isEnabled)
|
2009-01-02 19:56:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool* value = &bandwidth->band[direction].honorParentLimits;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const didChange = isEnabled != *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
*value = isEnabled;
|
|
|
|
return didChange;
|
2009-01-02 19:56:06 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_bandwidthAreParentLimitsHonored(tr_bandwidth const* bandwidth, tr_direction direction)
|
2009-03-04 19:52:57 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isBandwidth(bandwidth));
|
|
|
|
TR_ASSERT(tr_isDirection(direction));
|
2009-03-04 19:52:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return bandwidth->band[direction].honorParentLimits;
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/******
|
|
|
|
*******
|
|
|
|
******/
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bandwidthSetPeer(tr_bandwidth* bandwidth, struct tr_peerIo* peerIo);
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|