Changeset 7336


Ignore:
Timestamp:
Dec 9, 2008, 10:05:45 PM (12 years ago)
Author:
charles
Message:

(trunk libT) experimental code to try & fix the "one torrent / one peer eats all my bandwidth" issue reported in the forums & irc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r7265 r7336  
    1818#include "transmission.h"
    1919#include "bandwidth.h"
     20#include "crypto.h"
    2021#include "iobuf.h"
    2122#include "ptrarray.h"
     
    245246#endif
    246247
    247 void
    248 tr_bandwidthAllocate( tr_bandwidth  * b,
    249                       tr_direction    dir,
    250                       int             period_msec )
     248static void
     249allocateBandwidth( tr_bandwidth  * b,
     250                   tr_direction    dir,
     251                   int             period_msec,
     252                   tr_ptrArray   * addme_buffers )
    251253{
    252254    assert( isBandwidth( b ) );
     
    256258    {
    257259        const double desiredSpeed = b->band[dir].desiredSpeed;
    258 #if 0
    259         const double currentSpeed = getSpeed( &b->band[dir].piece, HISTORY_MSEC - period_msec );
    260         const double pulseCount = ( HISTORY_MSEC - period_msec ) / (double)period_msec;
    261         const double nextPulseSpeed = desiredSpeed * ( pulseCount + 1 ) - ( currentSpeed * pulseCount );
    262 #else
    263260        const double nextPulseSpeed = desiredSpeed;
    264 #endif
    265261        b->band[dir].bytesLeft = MAX( 0.0, nextPulseSpeed * 1024.0 * period_msec / 1000.0 );
    266262
     
    276272    if( !b->band[dir].isLimited || ( b->band[dir].bytesLeft > 0 ) )
    277273    {
    278         int i, n=0;
    279         short what = dir==TR_UP ? EV_WRITE : EV_READ;
    280         struct tr_iobuf ** iobufs = (struct tr_iobuf**) tr_ptrArrayPeek( b->iobufs, &n );
     274        int i;
     275        const int n = tr_ptrArraySize( b->iobufs );
     276        for( i=0; i<n; ++i )
     277            tr_ptrArrayAppend( addme_buffers, tr_ptrArrayNth( b->iobufs, i ) );
     278    }
     279
    281280#ifdef DEBUG_DIRECTION
    282281if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) )
    283282fprintf( stderr, "bandwidth %p has %d iobufs\n", b, n );
    284283#endif
    285         for( i=0; i<n; ++i )
    286             tr_iobuf_enable( iobufs[i], what );
    287     }
    288284
    289285    /* all children should reallocate too */
     
    296292}
    297293
     294void
     295tr_bandwidthAllocate( tr_bandwidth  * b,
     296                      tr_direction    dir,
     297                      int             period_msec )
     298{
     299    int n;
     300    tr_ptrArray * tmp;
     301    struct tr_iobuf ** buffers;
     302    const short what = dir==TR_UP ? EV_WRITE : EV_READ;
     303
     304    tmp = tr_ptrArrayNew( );
     305    allocateBandwidth( b, dir, period_msec, tmp );
     306    buffers = (struct tr_iobuf**) tr_ptrArrayPeek( b->children, &n );
     307
     308    /* notify the io buffers in a random order s.t. no
     309       particular peer gets to hog all the bandwidth */
     310    while( n > 0 ) {
     311        const int i = tr_cryptoRandInt( n );
     312        tr_iobuf_enable( buffers[i], what );
     313        buffers[i] = buffers[n-1];
     314        --n;
     315    }
     316
     317    tr_ptrArrayFree( tmp, NULL );
     318}
     319
    298320/***
    299321****
Note: See TracChangeset for help on using the changeset viewer.