Changeset 6784


Ignore:
Timestamp:
Sep 18, 2008, 3:55:31 AM (13 years ago)
Author:
charles
Message:

add new compile-time throttle options suggested by persept

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r6782 r6784  
    129129    tr_ptrArray * incomingHandshakes; /* tr_handshake */
    130130    tr_timer * bandwidthTimer;
    131     double rateHistory[2][BANDWIDTH_PULSES_PER_SECOND];
    132     double globalPoolHistory[2][BANDWIDTH_PULSES_PER_SECOND];
     131    double rateHistory[2][BANDWIDTH_PULSE_HISTORY];
     132    double globalPoolHistory[2][BANDWIDTH_PULSE_HISTORY];
    133133};
    134134
     
    14771477{
    14781478    int i;
    1479     double rate = 0;
     1479    double bytes = 0;
    14801480
    14811481    assert( manager != NULL );
    14821482    assert( direction==TR_UP || direction==TR_DOWN );
    14831483
    1484     for( i=0; i<BANDWIDTH_PULSES_PER_SECOND; ++i )
    1485         rate += manager->rateHistory[direction][i];
    1486 
    1487     return rate / 1024.0;
     1484    for( i=0; i<BANDWIDTH_PULSE_HISTORY; ++i )
     1485        bytes += manager->rateHistory[direction][i];
     1486
     1487    return ( BANDWIDTH_PULSES_PER_SECOND * bytes )
     1488         / ( BANDWIDTH_PULSE_HISTORY * 1024 );
    14881489}
    14891490
     
    20172018static double
    20182019allocateHowMuch( double           desiredAvgKB,
    2019                  const double   * history,
    2020                  int              pulseNumber )
    2021 {
    2022     int i;
    2023     int oldest = ( pulseNumber + 1 ) % BANDWIDTH_PULSES_PER_SECOND;
     2020                 const double   * history )
     2021{
    20242022    const double baseline = desiredAvgKB * 1024.0 / BANDWIDTH_PULSES_PER_SECOND;
    20252023    const double min = baseline * 0.66;
    20262024    const double max = baseline * 1.33;
    2027     double bytes;
    2028 
    2029     bytes = desiredAvgKB * 1024.0;
    2030     for( i=0; i<BANDWIDTH_PULSES_PER_SECOND; ++i )
    2031         if( i != oldest )
    2032             bytes -= history[i];
    2033 
    2034     /* clamp the return value to lessen any oscillation */
    2035     bytes = MAX( bytes, min );
    2036     bytes = MIN( bytes, max );
    2037     return bytes;
     2025    int i;
     2026    double usedBytes;
     2027    double n;
     2028    double clamped;
     2029
     2030    for( usedBytes=i=0; i<BANDWIDTH_PULSE_HISTORY; ++i )
     2031        usedBytes += history[i];
     2032
     2033    n = ( desiredAvgKB * 1024.0 ) * ( BANDWIDTH_PULSE_HISTORY + 1.0 ) / BANDWIDTH_PULSES_PER_SECOND - usedBytes;
     2034
     2035    /* clamp the return value to lessen oscillation */
     2036    clamped = n;
     2037    clamped = MAX( clamped, min );
     2038    clamped = MIN( clamped, max );
     2039/*fprintf( stderr, "desiredAvgKB is %.2f, rate is %.2f, allocating %.2f (%.2f)\n", desiredAvgKB, ((usedBytes*BANDWIDTH_PULSES_PER_SECOND)/BANDWIDTH_PULSE_HISTORY)/1024.0, clamped/1024.0, n/1024.0 );*/
     2040    return clamped;
    20382041}
    20392042
    20402043/**
    2041  * Distirbutes a fixed amount of bandwidth among a set of peers.
     2044 * Distributes a fixed amount of bandwidth among a set of peers.
    20422045 *
    2043  * @param peerArray peers whose client-to-peer bandwidth will be adjusted
     2046 * @param peerArray peers whose client-to-peer bandwidth will be set
    20442047 * @param direction whether to allocate upload or download bandwidth
    20452048 * @param history recent bandwidth history for these peers
    2046  * @param pulseNumber index of the current pulse in the history array
    20472049 * @param desiredAvgKB overall bandwidth goal for this set of peers
    20482050 */
     
    20512053                  const tr_direction    direction,
    20522054                  const double        * history,
    2053                   int                   pulseNumber,
    20542055                  double                desiredAvgKB )
    20552056{
    20562057    const int peerCount = tr_ptrArraySize( peerArray );
     2058    const double bytes = allocateHowMuch( desiredAvgKB, history );
     2059    const double welfareBytes = MIN( 2048, bytes * 0.2 );
     2060    const double meritBytes = MAX( 0, bytes - welfareBytes );
    20572061    tr_peer ** peers = (tr_peer**) tr_ptrArrayBase( peerArray );
    20582062    tr_peer ** candidates = tr_new( tr_peer*, peerCount );
    2059     const double bytes = allocateHowMuch( desiredAvgKB, history, pulseNumber );
    2060     const double welfareBytes = MIN( 2048, bytes * 0.2 );
    2061     const double meritBytes = MAX( 0, bytes - welfareBytes );
    20622063    int i;
    20632064    int candidateCount;
     
    20962097countHandshakeBandwidth( tr_ptrArray * handshakes, tr_direction direction )
    20972098{
     2099    const int n = tr_ptrArraySize( handshakes );
    20982100    int i;
    2099     size_t total = 0;
    2100     const int n = tr_ptrArraySize( handshakes );
    2101     for( i=0; i<n; ++i ) {
     2101    size_t total;
     2102    for( i=total=0; i<n; ++i ) {
    21022103         tr_peerIo * io = tr_handshakeGetIO( tr_ptrArrayNth( handshakes, i ) );
    21032104         total += tr_peerIoGetBandwidthUsed( io, direction );
     
    21092110countPeerBandwidth( tr_ptrArray * peers, tr_direction direction )
    21102111{
     2112    const int n = tr_ptrArraySize( peers );
    21112113    int i;
    2112     size_t total = 0;
    2113     const int n = tr_ptrArraySize( peers );
    2114     for( i=0; i<n; ++i )
     2114    size_t total;
     2115    for( i=total=0; i<n; ++i )
    21152116    {
    21162117         tr_peer * peer = tr_ptrArrayNth( peers, i );
     
    21232124givePeersUnlimitedBandwidth( tr_ptrArray * peers, tr_direction direction )
    21242125{
     2126    const int n = tr_ptrArraySize( peers );
    21252127    int i;
    2126     const int n = tr_ptrArraySize( peers );
    21272128    for( i=0; i<n; ++i )
    21282129    {
     
    21352136pumpAllPeers( tr_peerMgr * mgr )
    21362137{
     2138    const int torrentCount = tr_ptrArraySize( mgr->torrents );
    21372139    int i, j;
    2138     const int torrentCount = tr_ptrArraySize( mgr->torrents );
    21392140    for( i=0; i<torrentCount; ++i )
    21402141    {
     
    21972198                setPeerBandwidth( t->peers, direction,
    21982199                                  t->tor->rateHistory[direction],
    2199                                   pulseNumber,
    22002200                                  tr_torrentGetSpeedLimit( t->tor,
    22012201                                                           direction ) );
     
    22272227        setPeerBandwidth( globalPool, direction,
    22282228                          mgr->globalPoolHistory[direction],
    2229                           pulseNumber,
    22302229                          tr_sessionGetSpeedLimit( session, direction ) );
    22312230
     
    22462245
    22472246    /* keep track of how far we are into the cycle */
    2248     if( ++mgr->bandwidthPulseNumber == BANDWIDTH_PULSES_PER_SECOND )
     2247    if( ++mgr->bandwidthPulseNumber == BANDWIDTH_PULSE_HISTORY )
    22492248        mgr->bandwidthPulseNumber = 0;
    22502249
  • trunk/libtransmission/session.h

    r6782 r6784  
    3636#endif
    3737
    38 /**
    39  * How frequently to reallocate peer bandwidth.
    40  */
    41 #define BANDWIDTH_PULSES_PER_SECOND 5
     38enum
     39{
     40    /* How frequently to reallocate peer bandwidth. */
     41    BANDWIDTH_PULSES_PER_SECOND = 8,
     42
     43    /* HOw many pulses to remember for averaging the current speed */
     44    BANDWIDTH_PULSE_HISTORY = ( BANDWIDTH_PULSES_PER_SECOND * 2 )
     45};
     46
    4247
    4348typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
  • trunk/libtransmission/torrent.h

    r6782 r6784  
    181181    int                        uniqueId;
    182182
    183     double                     rateHistory[2][BANDWIDTH_PULSES_PER_SECOND];
     183    double                     rateHistory[2][BANDWIDTH_PULSE_HISTORY];
    184184
    185185};
Note: See TracChangeset for help on using the changeset viewer.