Ignore:
Timestamp:
Nov 26, 2008, 6:00:37 PM (12 years ago)
Author:
charles
Message:

(libT) make the the speed measurements a little more accurate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r7159 r7160  
    3030    HISTORY_MSEC = 2000,
    3131    INTERVAL_MSEC = HISTORY_MSEC,
    32     GRANULARITY_MSEC = 250,
     32    GRANULARITY_MSEC = 40,
    3333    HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ),
    3434    MAGIC_NUMBER = 43143
     
    4242
    4343static float
    44 getSpeed( const struct bratecontrol * r )
    45 {
    46     const uint64_t interval_msec = HISTORY_MSEC;
     44getSpeed( const struct bratecontrol * r, int interval_msec )
     45{
    4746    uint64_t       bytes = 0;
    4847    const uint64_t cutoff = tr_date ( ) - interval_msec;
     
    241240}
    242241
    243 #if 0
     242#if 1
    244243#define DEBUG_DIRECTION TR_UP
    245244#endif
     
    259258        const double currentSpeed = tr_bandwidthGetPieceSpeed( b, dir ); /* KiB/s */
    260259        const double desiredSpeed = b->band[dir].desiredSpeed;           /* KiB/s */
    261         const double seconds_per_pulse = period_msec / 1000.0;
    262         const double current_bytes_per_pulse = currentSpeed * 1024.0 * seconds_per_pulse;
    263         const double desired_bytes_per_pulse = desiredSpeed * 1024.0 * seconds_per_pulse;
    264         const double pulses_per_history = (double)HISTORY_MSEC / period_msec;
    265         const double min = desired_bytes_per_pulse * 0.85;
    266         const double max = desired_bytes_per_pulse * 1.15;
    267         const double next_pulse_bytes = desired_bytes_per_pulse * ( pulses_per_history + 1 )
    268                                       - ( current_bytes_per_pulse * pulses_per_history );
    269 
    270         /* clamp the return value to lessen oscillation */
    271         clamped = next_pulse_bytes;
    272         clamped = MAX( clamped, min );
    273         clamped = MIN( clamped, max );
    274 
    275         b->band[dir].bytesLeft = clamped;
     260        const double pulseCount = HISTORY_MSEC / (double)period_msec;
     261        const double nextPulseSpeed = desiredSpeed * ( pulseCount + 1 ) - ( currentSpeed * pulseCount );
     262        b->band[dir].bytesLeft = nextPulseSpeed * 1024.0 * period_msec / 1000.0;
    276263
    277264#ifdef DEBUG_DIRECTION
    278265        if( dir == DEBUG_DIRECTION )
    279                 fprintf( stderr, "bandwidth %p currentPieceSpeed(%5.2f of %5.2f) desiredSpeed(%5.2f), allocating %5.2f (unclamped: %5.2f)\n",
     266                fprintf( stderr, "bandwidth %p currentPieceSpeed(%5.2f of %5.2f) desiredSpeed(%5.2f), allocating %5.2f\n",
    280267                         b, currentSpeed, tr_bandwidthGetRawSpeed( b, dir ), desiredSpeed,
    281                          clamped/1024.0, next_pulse_bytes/1024.0 );
     268                         b->band[dir].bytesLeft/1024.0 );
    282269#endif
    283270    }
    284271
    285272    /* notify the io buffers that there's more bandwidth available */
    286     if( !b->band[dir].isLimited || ( clamped > 0.001 ) )
     273    if( !b->band[dir].isLimited || ( b->band[dir].bytesLeft > 0 ) )
    287274    {
    288275        int i, n=0;
     
    360347    assert( isDirection( dir ) );
    361348
    362     return getSpeed( &b->band[dir].raw );
     349    return getSpeed( &b->band[dir].raw, HISTORY_MSEC );
    363350}
    364351
     
    369356    assert( isDirection( dir ) );
    370357
    371     return getSpeed( &b->band[dir].piece );
     358    return getSpeed( &b->band[dir].piece, HISTORY_MSEC );
    372359}
    373360
Note: See TracChangeset for help on using the changeset viewer.