Changeset 12170


Ignore:
Timestamp:
Mar 15, 2011, 6:11:31 PM (11 years ago)
Author:
jordan
Message:

(trunk libT) cpu load improvements based on profiling by gunzip

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r12072 r12170  
    3535getSpeed_Bps( const struct bratecontrol * r, unsigned int interval_msec, uint64_t now )
    3636{
    37     uint64_t       bytes = 0;
    38     const uint64_t cutoff = (now?now:tr_time_msec()) - interval_msec;
    39     int            i = r->newest;
    40 
    41     for( ;; )
    42     {
    43         if( r->transfers[i].date <= cutoff )
    44             break;
    45 
    46         bytes += r->transfers[i].size;
    47 
    48         if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
    49         if( i == r->newest ) break; /* we've come all the way around */
    50     }
    51 
    52     return (unsigned int)(( bytes * 1000u ) / interval_msec);
     37    if( !now )
     38        now = tr_time_msec();
     39
     40    if( now != r->cache_time )
     41    {
     42        int i = r->newest;
     43        uint64_t bytes = 0;
     44        const uint64_t cutoff = now - interval_msec;
     45        struct bratecontrol * rvolatile = (struct bratecontrol*) r;
     46
     47        for( ;; )
     48        {
     49            if( r->transfers[i].date <= cutoff )
     50                break;
     51
     52            bytes += r->transfers[i].size;
     53
     54            if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
     55            if( i == r->newest ) break; /* we've come all the way around */
     56        }
     57
     58        rvolatile->cache_val = (unsigned int)(( bytes * 1000u ) / interval_msec);
     59        rvolatile->cache_time = now;
     60    }
     61
     62    return r->cache_val;
    5363}
    5464
     
    6474        r->transfers[r->newest].size = size;
    6575    }
     76
     77    /* invalidate cache_val*/
     78    r->cache_time = 0;
    6679}
    6780
     
    89102    b->session = session;
    90103    b->children = TR_PTR_ARRAY_INIT;
    91     b->magicNumber = MAGIC_NUMBER;
     104    b->magicNumber = BANDWIDTH_MAGIC_NUMBER;
    92105    b->band[TR_UP].honorParentLimits = TRUE;
    93106    b->band[TR_DOWN].honorParentLimits = TRUE;
     
    216229    while( n > 1 )
    217230    {
    218         const size_t increment = 256;//1024;
     231        const size_t increment = 512;//1024;
    219232        const int bytesUsed = tr_peerIoFlush( peers[i], dir, increment );
    220233
     
    320333        if( b->band[dir].isLimited )
    321334        {
    322             double current = tr_bandwidthGetRawSpeed_Bps( b, now, TR_DOWN );
    323             double desired = tr_bandwidthGetDesiredSpeed_Bps( b, TR_DOWN );
    324             double r = desired > 0.001 ? current / desired : 0;
    325             size_t i;
    326 
    327335            byteCount = MIN( byteCount, b->band[dir].bytesLeft );
    328336
    329                  if( r > 1.0 ) i = 0;
    330             else if( r > 0.9 ) i = byteCount * 0.9;
    331             else if( r > 0.8 ) i = byteCount * 0.8;
    332             else               i = byteCount;
    333 
    334             //fprintf( stderr, "--> %.4f  (%f... %f) [%zu --> %zu]\n", r, current, desired, byteCount, i );
    335             byteCount = i;
     337            /* if we're getting close to exceeding the speed limit,
     338             * clamp down harder on the bytes available */
     339            if( byteCount > 0 )
     340            {
     341                double current = tr_bandwidthGetRawSpeed_Bps( b, now, TR_DOWN );
     342                double desired = tr_bandwidthGetDesiredSpeed_Bps( b, TR_DOWN );
     343                double r = desired >= 1 ? current / desired : 0;
     344
     345                     if( r > 1.0 ) byteCount = 0;
     346                else if( r > 0.9 ) byteCount *= 0.8;
     347                else if( r > 0.8 ) byteCount *= 0.9;
     348            }
    336349        }
    337350
  • trunk/libtransmission/bandwidth.h

    r12074 r12170  
    3939    GRANULARITY_MSEC = 200,
    4040    HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ),
    41     MAGIC_NUMBER = 43143
     41    BANDWIDTH_MAGIC_NUMBER = 43143
    4242};
    4343
     
    4848    int newest;
    4949    struct { uint64_t date, size; } transfers[HISTORY_SIZE];
     50    uint64_t cache_time;
     51    unsigned int cache_val;
    5052};
    5153
     
    142144static inline tr_bool tr_isBandwidth( const tr_bandwidth  * b )
    143145{
    144     return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
     146    return ( b != NULL ) && ( b->magicNumber == BANDWIDTH_MAGIC_NUMBER );
    145147}
    146148
  • trunk/libtransmission/peer-io.c

    r12141 r12170  
    3939#include "utils.h"
    4040
    41 
    42 #define MAGIC_NUMBER 206745
    4341
    4442#ifdef WIN32
     
    140138    dbgmsg( io, "canRead" );
    141139
    142     assert( tr_isPeerIo( io ) );
    143     assert( tr_isSession( io->session ) );
    144140    tr_peerIoRef( io );
    145141
     
    161157            const unsigned int overhead = guessPacketOverhead( used );
    162158
    163             assert( tr_isPeerIo( io ) );
    164 
    165159            if( piece || (piece!=used) )
    166160            {
     
    198192    }
    199193
    200     assert( tr_isPeerIo( io ) );
    201194    tr_peerIoUnref( io );
    202 }
    203 
    204 tr_bool
    205 tr_isPeerIo( const tr_peerIo * io )
    206 {
    207     return ( io != NULL )
    208         && ( io->magicNumber == MAGIC_NUMBER )
    209         && ( io->refCount >= 0 )
    210         && ( tr_isBandwidth( &io->bandwidth ) )
    211         && ( tr_isAddress( &io->addr ) );
    212195}
    213196
     
    565548
    566549    io = tr_new0( tr_peerIo, 1 );
    567     io->magicNumber = MAGIC_NUMBER;
     550    io->magicNumber = PEER_IO_MAGIC_NUMBER;
    568551    io->refCount = 1;
    569552    io->crypto = tr_cryptoNew( torrentHash, isIncoming );
  • trunk/libtransmission/peer-io.h

    r12141 r12170  
    145145#define tr_peerIoUnref(io) tr_peerIoUnrefImpl( __FILE__, __LINE__, (io) );
    146146
    147 tr_bool     tr_isPeerIo         ( const tr_peerIo         * io );
    148 
     147#define PEER_IO_MAGIC_NUMBER 206745
     148
     149static inline tr_bool
     150tr_isPeerIo( const tr_peerIo * io )
     151{
     152    return ( io != NULL )
     153        && ( io->magicNumber == PEER_IO_MAGIC_NUMBER )
     154        && ( io->refCount >= 0 )
     155        && ( tr_isBandwidth( &io->bandwidth ) )
     156        && ( tr_isAddress( &io->addr ) );
     157}
    149158
    150159/**
     
    356365tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
    357366{
    358     assert( tr_isPeerIo( io ) );
    359 
    360367    return tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0;
    361368}
     
    364371tr_peerIoGetPieceSpeed_Bps( const tr_peerIo * io, uint64_t now, tr_direction dir )
    365372{
    366     assert( tr_isPeerIo( io ) );
    367     assert( tr_isDirection( dir ) );
    368 
    369373    return tr_bandwidthGetPieceSpeed_Bps( &io->bandwidth, now, dir );
    370374}
Note: See TracChangeset for help on using the changeset viewer.