2008-11-24 04:21:23 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 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"
|
|
|
|
|
2009-01-02 21:50:51 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
2008-11-24 04:21:23 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static float
|
2009-01-05 04:27:54 +00:00
|
|
|
getSpeed( const struct bratecontrol * r, int interval_msec, uint64_t now )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
|
|
|
uint64_t bytes = 0;
|
2009-01-05 04:27:54 +00:00
|
|
|
const uint64_t cutoff = (now?now:tr_date()) - interval_msec;
|
2008-11-24 04:21:23 +00:00
|
|
|
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
|
2009-01-02 23:48:51 +00:00
|
|
|
bytesUsed( const uint64_t now, struct bratecontrol * r, size_t size )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******
|
|
|
|
*******
|
|
|
|
*******
|
|
|
|
******/
|
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE int
|
2008-11-25 21:35:17 +00:00
|
|
|
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-11-24 04:21:23 +00:00
|
|
|
tr_bandwidth*
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidthConstruct( tr_bandwidth * b, tr_session * session, tr_bandwidth * parent )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
|
|
|
b->session = session;
|
2008-12-29 08:54:36 +00:00
|
|
|
b->children = TR_PTR_ARRAY_INIT;
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidth*
|
|
|
|
tr_bandwidthDestruct( tr_bandwidth * b )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
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-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &b->children, NULL );
|
2009-01-02 19:56:06 +00:00
|
|
|
|
2009-01-24 16:21:34 +00:00
|
|
|
memset( b, ~0, sizeof( tr_bandwidth ) );
|
2009-01-02 19:56:06 +00:00
|
|
|
return b;
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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 );
|
|
|
|
|
2009-10-09 20:06:22 +00:00
|
|
|
//fprintf( stderr, "setting parent for %p: %p\n", b, parent );
|
2008-11-25 21:35:17 +00:00
|
|
|
if( b->parent )
|
|
|
|
{
|
2009-10-09 20:06:22 +00:00
|
|
|
void * removed;
|
|
|
|
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b->parent ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2009-10-09 20:06:22 +00:00
|
|
|
removed = tr_ptrArrayRemoveSorted( &b->parent->children, b, comparePointers );
|
|
|
|
assert( removed == b );
|
|
|
|
assert( tr_ptrArrayFindSorted( &b->parent->children, b, comparePointers ) == NULL );
|
|
|
|
//fprintf( stderr, "removed child bandwidth %p from old parent %p\n", b, b->parent );
|
|
|
|
|
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 );
|
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayInsertSorted( &parent->children, b, comparePointers );
|
2009-10-09 20:06:22 +00:00
|
|
|
assert( tr_ptrArrayFindSorted( &parent->children, b, comparePointers ) == b );
|
|
|
|
//fprintf( stderr, "set new parent for %p: %p\n", b, parent );
|
2008-11-25 21:35:17 +00:00
|
|
|
b->parent = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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,
|
2009-04-18 23:17:30 +00:00
|
|
|
tr_priority_t parent_priority,
|
2008-12-09 22:05:45 +00:00
|
|
|
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
|
|
|
{
|
2009-04-18 23:17:30 +00:00
|
|
|
tr_priority_t priority;
|
|
|
|
|
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
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
priority = MAX( parent_priority, b->priority );
|
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
/* add this bandwidth's peer, if any, to the peer pool */
|
2009-04-18 23:17:30 +00:00
|
|
|
if( b->peer != NULL ) {
|
|
|
|
b->peer->priority = priority;
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_ptrArrayAppend( peer_pool, b->peer );
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
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
|
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
/* traverse & repeat for the subtree */
|
2008-11-25 21:35:17 +00:00
|
|
|
if( 1 ) {
|
2008-12-29 08:54:36 +00:00
|
|
|
int i;
|
2009-01-04 16:59:15 +00:00
|
|
|
struct tr_bandwidth ** children = (struct tr_bandwidth**) tr_ptrArrayBase( &b->children );
|
|
|
|
const int n = tr_ptrArraySize( &b->children );
|
2008-11-25 21:35:17 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2009-04-18 23:17:30 +00:00
|
|
|
allocateBandwidth( children[i], priority, dir, period_msec, peer_pool );
|
2008-11-25 21:35:17 +00:00
|
|
|
}
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
static void
|
|
|
|
phaseOne( tr_ptrArray * peerArray, tr_direction dir )
|
2008-12-09 22:05:45 +00:00
|
|
|
{
|
2009-04-18 23:17:30 +00:00
|
|
|
int i, n;
|
|
|
|
int peerCount = tr_ptrArraySize( peerArray );
|
|
|
|
struct tr_peerIo ** peers = (struct tr_peerIo**) tr_ptrArrayBase( peerArray );
|
2009-01-05 04:27:54 +00:00
|
|
|
|
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-31 21:15:22 +00:00
|
|
|
* small chunk of bandwidth. Keep looping until we run out of bandwidth
|
|
|
|
* and/or peers that can use it */
|
2008-12-20 22:19:34 +00:00
|
|
|
n = peerCount;
|
2009-01-02 22:06:11 +00:00
|
|
|
dbgmsg( "%d peers to go round-robin for %s", n, (dir==TR_UP?"upload":"download") );
|
2008-12-20 22:19:34 +00:00
|
|
|
i = n ? tr_cryptoWeakRandInt( n ) : 0; /* pick a random starting point */
|
2009-01-02 04:46:38 +00:00
|
|
|
while( n > 1 )
|
2008-12-15 21:22:08 +00:00
|
|
|
{
|
2009-01-03 04:57:40 +00:00
|
|
|
const size_t increment = 1024;
|
2009-01-19 14:05:43 +00:00
|
|
|
const int bytesUsed = tr_peerIoFlush( peers[i], dir, increment );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2009-01-19 14:05:43 +00:00
|
|
|
dbgmsg( "peer #%d of %d used %d bytes in this pass", i, n, bytesUsed );
|
2009-01-03 02:43:17 +00:00
|
|
|
|
2009-04-25 19:22:27 +00:00
|
|
|
if( bytesUsed == (int)increment )
|
2008-12-20 22:19:34 +00:00
|
|
|
++i;
|
|
|
|
else {
|
|
|
|
/* peer is done writing for now; move it to the end of the list */
|
2009-01-01 18:38:49 +00:00
|
|
|
tr_peerIo * pio = peers[i];
|
2008-12-20 22:19:34 +00:00
|
|
|
peers[i] = peers[n-1];
|
2009-01-01 18:38:49 +00:00
|
|
|
peers[n-1] = pio;
|
2008-12-20 22:19:34 +00:00
|
|
|
--n;
|
2008-12-15 21:22:08 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
if( i == n )
|
|
|
|
i = 0;
|
2008-12-09 22:05:45 +00:00
|
|
|
}
|
2009-04-18 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthAllocate( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
int period_msec )
|
|
|
|
{
|
|
|
|
int i, peerCount;
|
|
|
|
tr_ptrArray tmp = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray low = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray high = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray normal = TR_PTR_ARRAY_INIT;
|
|
|
|
struct tr_peerIo ** peers;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
allocateBandwidth( b, TR_PRI_LOW, dir, period_msec, &tmp );
|
|
|
|
peers = (struct tr_peerIo**) tr_ptrArrayBase( &tmp );
|
|
|
|
peerCount = tr_ptrArraySize( &tmp );
|
|
|
|
|
2009-04-21 16:18:51 +00:00
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
{
|
|
|
|
tr_peerIo * io = peers[i];
|
|
|
|
tr_peerIoRef( io );
|
|
|
|
|
|
|
|
tr_peerIoFlushOutgoingProtocolMsgs( io );
|
|
|
|
|
|
|
|
switch( io->priority ) {
|
|
|
|
case TR_PRI_HIGH: tr_ptrArrayAppend( &high, io ); break;
|
|
|
|
case TR_PRI_LOW: tr_ptrArrayAppend( &low, io ); break;
|
|
|
|
default: tr_ptrArrayAppend( &normal, io ); break;
|
2009-04-18 23:17:30 +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
|
|
|
|
* small chunk of bandwidth. Keep looping until we run out of bandwidth
|
|
|
|
* and/or peers that can use it */
|
|
|
|
phaseOne( &high, dir );
|
|
|
|
phaseOne( &normal, dir );
|
|
|
|
phaseOne( &low, dir );
|
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 )
|
2009-01-24 03:17:59 +00:00
|
|
|
tr_peerIoSetEnabled( peers[i], dir, tr_peerIoHasBandwidthLeft( peers[i], dir ) );
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
tr_peerIoUnref( peers[i] );
|
|
|
|
|
2008-12-15 21:22:08 +00:00
|
|
|
/* cleanup */
|
2009-04-18 23:17:30 +00:00
|
|
|
tr_ptrArrayDestruct( &normal, NULL );
|
|
|
|
tr_ptrArrayDestruct( &high, NULL );
|
|
|
|
tr_ptrArrayDestruct( &low, NULL );
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &tmp, NULL );
|
2008-12-09 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
void
|
2009-01-02 19:56:06 +00:00
|
|
|
tr_bandwidthSetPeer( tr_bandwidth * b, tr_peerIo * peer )
|
2008-11-25 21:35:17 +00:00
|
|
|
{
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isBandwidth( b ) );
|
2009-01-02 19:56:06 +00:00
|
|
|
assert( ( peer == NULL ) || tr_isPeerIo( peer ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
b->peer = peer;
|
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
|
2009-01-05 04:27:54 +00:00
|
|
|
tr_bandwidthGetRawSpeed( const tr_bandwidth * b, const uint64_t now, const 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
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
return getSpeed( &b->band[dir].raw, HISTORY_MSEC, now );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2009-01-05 04:27:54 +00:00
|
|
|
tr_bandwidthGetPieceSpeed( const tr_bandwidth * b, const uint64_t now, const 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
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
return getSpeed( &b->band[dir].piece, HISTORY_MSEC, now );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 23:48:51 +00:00
|
|
|
static void
|
|
|
|
bandwidthUsedImpl( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
size_t byteCount,
|
|
|
|
tr_bool isPieceData,
|
|
|
|
uint64_t now )
|
2008-11-24 04:21:23 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
struct tr_band * band;
|
|
|
|
|
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-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
|
|
|
|
|
2009-01-02 23:48:51 +00:00
|
|
|
bytesUsed( now, &band->raw, byteCount );
|
2008-11-24 04:21:23 +00:00
|
|
|
|
|
|
|
if( isPieceData )
|
2009-01-02 23:48:51 +00:00
|
|
|
bytesUsed( now, &band->piece, byteCount );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
if( b->parent != NULL )
|
2009-01-02 23:48:51 +00:00
|
|
|
bandwidthUsedImpl( b->parent, dir, byteCount, isPieceData, now );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bandwidthUsed( tr_bandwidth * b,
|
|
|
|
tr_direction dir,
|
|
|
|
size_t byteCount,
|
|
|
|
tr_bool isPieceData )
|
|
|
|
{
|
|
|
|
bandwidthUsedImpl( b, dir, byteCount, isPieceData, tr_date( ) );
|
2008-11-24 04:21:23 +00:00
|
|
|
}
|