2008-11-24 04:21:23 +00:00
|
|
|
/*
|
2008-12-16 00:20:44 +00:00
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@transmissionbt.com>
|
2008-11-24 04:21:23 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-12-24 17:45:27 +00:00
|
|
|
* $Id$
|
2008-11-24 04:21:23 +00:00
|
|
|
*/
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
#include <assert.h>
|
2008-11-24 04:21:23 +00:00
|
|
|
#include <limits.h>
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
#include "event.h"
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bandwidth.h"
|
2008-12-09 22:05:45 +00:00
|
|
|
#include "crypto.h"
|
2008-12-16 22:08:17 +00:00
|
|
|
#include "peer-io.h"
|
2008-11-25 21:35:17 +00:00
|
|
|
#include "ptrarray.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
HISTORY_MSEC = 2000,
|
|
|
|
INTERVAL_MSEC = HISTORY_MSEC,
|
2008-12-04 03:31:14 +00:00
|
|
|
GRANULARITY_MSEC = 50,
|
2008-11-25 21:35:17 +00:00
|
|
|
HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ),
|
|
|
|
MAGIC_NUMBER = 43143
|
2008-11-24 04:21:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bratecontrol
|
|
|
|
{
|
|
|
|
int newest;
|
|
|
|
struct { uint64_t date, size; } transfers[HISTORY_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
static float
|
2008-11-26 18:00:37 +00:00
|
|
|
getSpeed( const struct bratecontrol * r, int interval_msec )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
|
|
|
uint64_t bytes = 0;
|
|
|
|
const uint64_t cutoff = tr_date ( ) - interval_msec;
|
|
|
|
int i = r->newest;
|
|
|
|
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
if( r->transfers[i].date <= cutoff )
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytes += r->transfers[i].size;
|
|
|
|
|
|
|
|
if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
|
|
|
|
if( i == r->newest ) break; /* we've come all the way around */
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( bytes / 1024.0 ) * ( 1000.0 / interval_msec );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bytesUsed( struct bratecontrol * r, size_t size )
|
|
|
|
{
|
|
|
|
const uint64_t now = tr_date ( );
|
|
|
|
|
|
|
|
if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
|
|
|
|
r->transfers[r->newest].size += size;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
|
|
|
|
r->transfers[r->newest].date = now;
|
|
|
|
r->transfers[r->newest].size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******
|
|
|
|
*******
|
|
|
|
*******
|
|
|
|
******/
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
struct tr_band
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isLimited;
|
|
|
|
tr_bool honorParentLimits;
|
2008-11-24 04:21:23 +00:00
|
|
|
size_t bytesLeft;
|
2008-11-25 21:35:17 +00:00
|
|
|
double desiredSpeed;
|
2008-11-24 04:21:23 +00:00
|
|
|
struct bratecontrol raw;
|
|
|
|
struct bratecontrol piece;
|
2008-11-25 21:35:17 +00:00
|
|
|
};
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
struct tr_bandwidth
|
|
|
|
{
|
|
|
|
struct tr_band band[2];
|
|
|
|
struct tr_bandwidth * parent;
|
|
|
|
int magicNumber;
|
2008-11-24 04:21:23 +00:00
|
|
|
tr_session * session;
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_ptrArray * children; /* struct tr_bandwidth */
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArray * peers; /* tr_peerIo */
|
2008-11-24 04:21:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
static int
|
|
|
|
comparePointers( const void * a, const void * b )
|
|
|
|
{
|
2008-12-22 05:19:52 +00:00
|
|
|
if( a != b )
|
|
|
|
return a < b ? -1 : 1;
|
|
|
|
|
|
|
|
return 0;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool
|
|
|
|
tr_isBandwidth( const tr_bandwidth * b )
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
|
|
|
return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
tr_bandwidth*
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
|
|
|
tr_bandwidth * b = tr_new0( tr_bandwidth, 1 );
|
|
|
|
b->session = session;
|
2008-11-25 21:35:17 +00:00
|
|
|
b->children = tr_ptrArrayNew( );
|
2008-12-16 22:08:17 +00:00
|
|
|
b->peers = tr_ptrArrayNew( );
|
2008-11-25 21:35:17 +00:00
|
|
|
b->magicNumber = MAGIC_NUMBER;
|
2008-12-21 18:15:00 +00:00
|
|
|
b->band[TR_UP].honorParentLimits = TRUE;
|
|
|
|
b->band[TR_DOWN].honorParentLimits = TRUE;
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthSetParent( b, parent );
|
2008-11-24 04:21:23 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthFree( tr_bandwidth * b )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
tr_bandwidthSetParent( b, NULL );
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArrayFree( b->peers, NULL );
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_ptrArrayFree( b->children, NULL );
|
|
|
|
b->magicNumber = 0xDEAD;
|
2008-11-24 04:21:23 +00:00
|
|
|
tr_free( b );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
void
|
|
|
|
tr_bandwidthSetParent( tr_bandwidth * b,
|
|
|
|
tr_bandwidth * parent )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
assert( b != parent );
|
|
|
|
|
|
|
|
if( b->parent )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b->parent ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
tr_ptrArrayRemoveSorted( b->parent->children, b, comparePointers );
|
2008-12-21 18:15:00 +00:00
|
|
|
b->parent = NULL;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( parent )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( parent ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
assert( parent->parent != b );
|
|
|
|
|
|
|
|
tr_ptrArrayInsertSorted( parent->children, b, comparePointers );
|
|
|
|
b->parent = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthHonorParentLimits( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool honorParentLimits )
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
b->band[dir].honorParentLimits = honorParentLimits;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthSetDesiredSpeed( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
double desiredSpeed )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
b->band[dir].desiredSpeed = desiredSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b,
|
|
|
|
tr_direction dir )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
return b->band[dir].desiredSpeed;
|
|
|
|
}
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
void
|
|
|
|
tr_bandwidthSetLimited( tr_bandwidth * b,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir,
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool isLimited )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
b->band[dir].isLimited = isLimited;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthIsLimited( const tr_bandwidth * b,
|
|
|
|
tr_direction dir )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
return b->band[dir].isLimited;
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
2008-11-26 18:54:35 +00:00
|
|
|
#if 0
|
|
|
|
#warning do not check the code in with this enabled
|
2008-11-26 15:58:26 +00:00
|
|
|
#define DEBUG_DIRECTION TR_UP
|
2008-11-25 21:35:17 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-09 22:05:45 +00:00
|
|
|
static void
|
|
|
|
allocateBandwidth( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
int period_msec,
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArray * peer_pool )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-26 15:58:26 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
/* set the available bandwidth */
|
2008-11-26 15:58:26 +00:00
|
|
|
if( b->band[dir].isLimited )
|
|
|
|
{
|
2008-11-27 18:56:58 +00:00
|
|
|
const double desiredSpeed = b->band[dir].desiredSpeed;
|
2008-12-04 03:31:14 +00:00
|
|
|
const double nextPulseSpeed = desiredSpeed;
|
2008-11-28 21:35:01 +00:00
|
|
|
b->band[dir].bytesLeft = MAX( 0.0, nextPulseSpeed * 1024.0 * period_msec / 1000.0 );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_DIRECTION
|
2008-11-26 15:58:26 +00:00
|
|
|
if( dir == DEBUG_DIRECTION )
|
2008-11-26 18:00:37 +00:00
|
|
|
fprintf( stderr, "bandwidth %p currentPieceSpeed(%5.2f of %5.2f) desiredSpeed(%5.2f), allocating %5.2f\n",
|
2008-11-26 15:58:26 +00:00
|
|
|
b, currentSpeed, tr_bandwidthGetRawSpeed( b, dir ), desiredSpeed,
|
2008-11-26 18:00:37 +00:00
|
|
|
b->band[dir].bytesLeft/1024.0 );
|
2008-11-25 21:35:17 +00:00
|
|
|
#endif
|
2008-11-26 15:58:26 +00:00
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
/* traverse & repeat for the subtree */
|
2008-11-26 15:58:26 +00:00
|
|
|
{
|
2008-12-09 22:05:45 +00:00
|
|
|
int i;
|
2008-12-16 22:08:17 +00:00
|
|
|
const int n = tr_ptrArraySize( b->peers );
|
2008-12-09 22:05:45 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArrayAppend( peer_pool, tr_ptrArrayNth( b->peers, i ) );
|
2008-12-09 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
#ifdef DEBUG_DIRECTION
|
2008-11-26 15:58:26 +00:00
|
|
|
if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) )
|
2008-12-16 22:08:17 +00:00
|
|
|
fprintf( stderr, "bandwidth %p has %d peers\n", b, n );
|
2008-11-25 21:35:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* all children should reallocate too */
|
|
|
|
if( 1 ) {
|
|
|
|
int i, n=0;
|
|
|
|
struct tr_bandwidth ** children = (struct tr_bandwidth**) tr_ptrArrayPeek( b->children, &n );
|
|
|
|
for( i=0; i<n; ++i )
|
2008-12-16 22:08:17 +00:00
|
|
|
allocateBandwidth( children[i], dir, period_msec, peer_pool );
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2008-12-09 22:05:45 +00:00
|
|
|
void
|
|
|
|
tr_bandwidthAllocate( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
int period_msec )
|
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
int i, n, peerCount;
|
2008-12-09 22:05:45 +00:00
|
|
|
tr_ptrArray * tmp;
|
2008-12-16 22:08:17 +00:00
|
|
|
struct tr_peerIo ** peers;
|
2008-12-24 17:45:27 +00:00
|
|
|
const uint64_t now = tr_date( );
|
|
|
|
const uint64_t cutoff = now + 100; /* 1/10th of a second */
|
|
|
|
|
2008-12-09 22:05:45 +00:00
|
|
|
|
2008-12-20 22:19:34 +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. */
|
2008-12-09 22:05:45 +00:00
|
|
|
tmp = tr_ptrArrayNew( );
|
|
|
|
allocateBandwidth( b, dir, period_msec, tmp );
|
2008-12-20 22:19:34 +00:00
|
|
|
peers = (struct tr_peerIo**) tr_ptrArrayPeek( tmp, &peerCount );
|
|
|
|
|
|
|
|
/* Stop all peers from listening for the socket to be ready for IO.
|
|
|
|
* See "Second phase of IO" lower in this function for more info. */
|
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
tr_peerIoSetEnabled( peers[i], dir, FALSE );
|
|
|
|
|
2008-12-21 18:15:00 +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
|
2008-12-24 17:45:27 +00:00
|
|
|
* small chunk of bandwidth. Keep looping until we reach the cutoff or
|
|
|
|
* run out of bandwidth and/or peers that can use it */
|
2008-12-20 22:19:34 +00:00
|
|
|
n = peerCount;
|
|
|
|
i = n ? tr_cryptoWeakRandInt( n ) : 0; /* pick a random starting point */
|
2008-12-24 17:45:27 +00:00
|
|
|
for( ; n>0 && tr_date()<=cutoff; )
|
2008-12-15 21:22:08 +00:00
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
const int increment = n==1 ? 4096 : 1024;
|
|
|
|
const int byteCount = tr_peerIoFlush( peers[i], dir, increment);
|
|
|
|
|
|
|
|
if( byteCount == increment )
|
|
|
|
++i;
|
|
|
|
else {
|
|
|
|
/* peer is done writing for now; move it to the end of the list */
|
|
|
|
tr_peerIo * tmp = peers[i];
|
|
|
|
peers[i] = peers[n-1];
|
|
|
|
peers[n-1] = tmp;
|
|
|
|
--n;
|
2008-12-15 21:22:08 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
assert( i <= n );
|
|
|
|
if( i == n )
|
|
|
|
i = 0;
|
2008-12-09 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 18:15:00 +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,
|
|
|
|
* or (2) the next tr_bandwidthAllocate() call, when we start over again. */
|
2008-12-20 22:19:34 +00:00
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
if( tr_peerIoHasBandwidthLeft( peers[i], dir ) )
|
|
|
|
tr_peerIoSetEnabled( peers[i], dir, TRUE );
|
|
|
|
|
2008-12-15 21:22:08 +00:00
|
|
|
/* cleanup */
|
2008-12-09 22:05:45 +00:00
|
|
|
tr_ptrArrayFree( tmp, NULL );
|
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bandwidthAddPeer( tr_bandwidth * b,
|
|
|
|
tr_peerIo * peerIo )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isPeerIo( peerIo ) );
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArrayInsertSorted( b->peers, peerIo, comparePointers );
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
void
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_bandwidthRemovePeer( tr_bandwidth * b,
|
|
|
|
tr_peerIo * peerIo )
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isPeerIo( peerIo ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
tr_ptrArrayRemoveSorted( b->peers, peerIo, comparePointers );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
size_t
|
|
|
|
tr_bandwidthClamp( const tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
size_t byteCount )
|
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
if( b )
|
|
|
|
{
|
|
|
|
if( b->band[dir].isLimited )
|
|
|
|
byteCount = MIN( byteCount, b->band[dir].bytesLeft );
|
|
|
|
|
|
|
|
if( b->parent && b->band[dir].honorParentLimits )
|
|
|
|
byteCount = tr_bandwidthClamp( b->parent, dir, byteCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
return byteCount;
|
|
|
|
}
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
double
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthGetRawSpeed( const tr_bandwidth * b, tr_direction dir )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-11-26 18:00:37 +00:00
|
|
|
return getSpeed( &b->band[dir].raw, HISTORY_MSEC );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthGetPieceSpeed( const tr_bandwidth * b, tr_direction dir )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-11-26 18:00:37 +00:00
|
|
|
return getSpeed( &b->band[dir].piece, HISTORY_MSEC );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthUsed( tr_bandwidth * b,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir,
|
2008-11-24 04:21:23 +00:00
|
|
|
size_t byteCount,
|
2008-12-21 18:15:00 +00:00
|
|
|
tr_bool isPieceData )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
struct tr_band * band;
|
2008-11-26 15:58:26 +00:00
|
|
|
size_t oldBytesLeft;
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
band = &b->band[dir];
|
2008-11-24 04:21:23 +00:00
|
|
|
|
2008-11-26 15:58:26 +00:00
|
|
|
oldBytesLeft = band->bytesLeft;
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
if( band->isLimited && isPieceData )
|
|
|
|
band->bytesLeft -= MIN( band->bytesLeft, byteCount );
|
|
|
|
|
|
|
|
#ifdef DEBUG_DIRECTION
|
2008-11-26 15:58:26 +00:00
|
|
|
if( ( dir == DEBUG_DIRECTION ) && ( band->isLimited ) )
|
|
|
|
fprintf( stderr, "%p consumed %5zu bytes of %5s data... was %6zu, now %6zu left\n",
|
|
|
|
b, byteCount, (isPieceData?"piece":"raw"), oldBytesLeft, band->bytesLeft );
|
2008-11-25 21:35:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bytesUsed( &band->raw, byteCount );
|
2008-11-24 04:21:23 +00:00
|
|
|
|
|
|
|
if( isPieceData )
|
2008-11-25 21:35:17 +00:00
|
|
|
bytesUsed( &band->piece, byteCount );
|
|
|
|
|
|
|
|
if( b->parent != NULL )
|
|
|
|
tr_bandwidthUsed( b->parent, dir, byteCount, isPieceData );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|