From 9de7ccf7da43fb73c4d0027ca58f387ee532072e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 26 Nov 2008 15:58:26 +0000 Subject: [PATCH] (libT) slightly more accurate reporting of piece data vs. raw data --- libtransmission/bandwidth.c | 63 ++++++++++++++++++++++--------------- libtransmission/peer-io.c | 19 ++++++----- libtransmission/peer-mgr.c | 2 +- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/libtransmission/bandwidth.c b/libtransmission/bandwidth.c index 286c2b047..e12ea922c 100644 --- a/libtransmission/bandwidth.c +++ b/libtransmission/bandwidth.c @@ -241,7 +241,7 @@ tr_bandwidthIsLimited( const tr_bandwidth * b, } #if 0 -#define DEBUG_DIRECTION TR_DOWN +#define DEBUG_DIRECTION TR_UP #endif void @@ -249,38 +249,47 @@ tr_bandwidthAllocate( tr_bandwidth * b, tr_direction dir, int period_msec ) { - const double currentSpeed = tr_bandwidthGetPieceSpeed( b, dir ); /* KiB/s */ - const double desiredSpeed = b->band[dir].desiredSpeed; /* KiB/s */ - const double seconds_per_pulse = period_msec / 1000.0; - const double current_bytes_per_pulse = currentSpeed * 1024.0 * seconds_per_pulse; - const double desired_bytes_per_pulse = desiredSpeed * 1024.0 * seconds_per_pulse; - const double pulses_per_history = (double)HISTORY_MSEC / period_msec; - const double min = desired_bytes_per_pulse * 0.90; - const double max = desired_bytes_per_pulse * 1.20; - const double next_pulse_bytes = desired_bytes_per_pulse * ( pulses_per_history + 1 ) - - ( current_bytes_per_pulse * pulses_per_history ); - double clamped; + double clamped = 0; - /* clamp the return value to lessen oscillation */ - clamped = next_pulse_bytes; - clamped = MAX( clamped, min ); - clamped = MIN( clamped, max ); - b->band[dir].bytesLeft = clamped; + assert( isBandwidth( b ) ); + assert( isDirection( dir ) ); + + if( b->band[dir].isLimited ) + { + const double currentSpeed = tr_bandwidthGetPieceSpeed( b, dir ); /* KiB/s */ + const double desiredSpeed = b->band[dir].desiredSpeed; /* KiB/s */ + const double seconds_per_pulse = period_msec / 1000.0; + const double current_bytes_per_pulse = currentSpeed * 1024.0 * seconds_per_pulse; + const double desired_bytes_per_pulse = desiredSpeed * 1024.0 * seconds_per_pulse; + const double pulses_per_history = (double)HISTORY_MSEC / period_msec; + const double min = desired_bytes_per_pulse * 0.85; + const double max = desired_bytes_per_pulse * 1.15; + const double next_pulse_bytes = desired_bytes_per_pulse * ( pulses_per_history + 1 ) + - ( current_bytes_per_pulse * pulses_per_history ); + + /* clamp the return value to lessen oscillation */ + clamped = next_pulse_bytes; + clamped = MAX( clamped, min ); + clamped = MIN( clamped, max ); + + b->band[dir].bytesLeft = clamped; #ifdef DEBUG_DIRECTION -if( dir == DEBUG_DIRECTION ) -fprintf( stderr, "bandwidth %p currentSpeed(%5.2f) desiredSpeed(%5.2f), allocating %5.2f (unclamped: %5.2f)\n", - b, currentSpeed, desiredSpeed, - clamped/1024.0, next_pulse_bytes/1024.0 ); + if( dir == DEBUG_DIRECTION ) + fprintf( stderr, "bandwidth %p currentPieceSpeed(%5.2f of %5.2f) desiredSpeed(%5.2f), allocating %5.2f (unclamped: %5.2f)\n", + b, currentSpeed, tr_bandwidthGetRawSpeed( b, dir ), desiredSpeed, + clamped/1024.0, next_pulse_bytes/1024.0 ); #endif + } /* notify the io buffers that there's more bandwidth available */ - if( !b->band[dir].isLimited || ( clamped > 0 ) ) { + if( !b->band[dir].isLimited || ( clamped > 0.001 ) ) + { int i, n=0; short what = dir==TR_UP ? EV_WRITE : EV_READ; struct tr_iobuf ** iobufs = (struct tr_iobuf**) tr_ptrArrayPeek( b->iobufs, &n ); #ifdef DEBUG_DIRECTION -if( dir == DEBUG_DIRECTION ) +if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) ) fprintf( stderr, "bandwidth %p has %d iobufs\n", b, n ); #endif for( i=0; iband[dir]; + oldBytesLeft = band->bytesLeft; + if( band->isLimited && isPieceData ) band->bytesLeft -= MIN( band->bytesLeft, byteCount ); #ifdef DEBUG_DIRECTION -if( ( dir == DEBUG_DIRECTION ) && band->isLimited && isPieceData ) -fprintf( stderr, "%p consumed %zu bytes of piece data... %zu left\n", b, byteCount, band->bytesLeft ); +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 ); #endif bytesUsed( &band->raw, byteCount ); diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 73c030fa0..e74fff980 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -39,7 +39,7 @@ #define IO_TIMEOUT_SECS 8 static size_t -addPacketOverhead( size_t d ) +getPacketOverhead( size_t d ) { /** * http://sd.wareonearth.com/~phil/net/overhead/ @@ -59,7 +59,7 @@ addPacketOverhead( size_t d ) */ static const double assumed_payload_data_rate = 94.0; - return (size_t)( d * ( 100.0 / assumed_payload_data_rate ) ); + return (size_t)( d * ( 100.0 / assumed_payload_data_rate ) - d ); } /** @@ -127,16 +127,19 @@ didWriteWrapper( struct tr_iobuf * iobuf, while( bytes_transferred ) { struct tr_datatype * next = io->output_datatypes->data; - const size_t chunk_length = MIN( next->length, bytes_transferred ); - const size_t n = addPacketOverhead( chunk_length ); + const size_t payload = MIN( next->length, bytes_transferred ); + const size_t overhead = getPacketOverhead( payload ); - tr_bandwidthUsed( io->bandwidth, TR_UP, n, next->isPieceData ); + tr_bandwidthUsed( io->bandwidth, TR_UP, payload, next->isPieceData ); + + if( overhead > 0 ) + tr_bandwidthUsed( io->bandwidth, TR_UP, overhead, FALSE ); if( io->didWrite ) - io->didWrite( io, n, next->isPieceData, io->userData ); + io->didWrite( io, payload, next->isPieceData, io->userData ); - bytes_transferred -= chunk_length; - next->length -= chunk_length; + bytes_transferred -= payload; + next->length -= payload; if( !next->length ) tr_free( tr_list_pop_front( &io->output_datatypes ) ); } diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 7314e58d2..2ca9930ea 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -57,7 +57,7 @@ enum RECONNECT_PERIOD_MSEC = ( 2 * 1000 ), /* how frequently to reallocate bandwidth */ - BANDWIDTH_PERIOD_MSEC = 200, + BANDWIDTH_PERIOD_MSEC = 250, /* max # of peers to ask fer per torrent per reconnect pulse */ MAX_RECONNECTIONS_PER_PULSE = 4,