From 01ab067e9c9c1d0974866287f14e8131cdbdcdbe Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 24 Oct 2010 01:08:08 +0000 Subject: [PATCH] (trunk libT) #3662 "libtransmission cpu optimization: fewer calls to tr_time_msec()" -- fixed. --- libtransmission/bandwidth.c | 23 +++++++---------------- libtransmission/bandwidth.h | 3 ++- libtransmission/fdlimit.c | 8 ++++---- libtransmission/peer-io.c | 18 ++++++++++++------ libtransmission/peer-mgr.c | 23 ++++++++++++----------- libtransmission/peer-msgs.c | 11 ++++++----- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/libtransmission/bandwidth.c b/libtransmission/bandwidth.c index ff2e93a9f..b3098e843 100644 --- a/libtransmission/bandwidth.c +++ b/libtransmission/bandwidth.c @@ -343,12 +343,12 @@ tr_bandwidthGetPieceSpeed_Bps( const tr_bandwidth * b, const uint64_t now, const return getSpeed_Bps( &b->band[dir].piece, HISTORY_MSEC, now ); } -static void -bandwidthUsedImpl( tr_bandwidth * b, - tr_direction dir, - size_t byteCount, - tr_bool isPieceData, - uint64_t now ) +void +tr_bandwidthUsed( tr_bandwidth * b, + tr_direction dir, + size_t byteCount, + tr_bool isPieceData, + uint64_t now ) { struct tr_band * band; @@ -372,14 +372,5 @@ fprintf( stderr, "%p consumed %5zu bytes of %5s data... was %6zu, now %6zu left\ bytesUsed( now, &band->piece, byteCount ); if( b->parent != NULL ) - bandwidthUsedImpl( b->parent, dir, byteCount, isPieceData, now ); -} - -void -tr_bandwidthUsed( tr_bandwidth * b, - tr_direction dir, - size_t byteCount, - tr_bool isPieceData ) -{ - bandwidthUsedImpl( b, dir, byteCount, isPieceData, tr_time_msec( ) ); + tr_bandwidthUsed( b->parent, dir, byteCount, isPieceData, now ); } diff --git a/libtransmission/bandwidth.h b/libtransmission/bandwidth.h index 84893c087..c78ec54d4 100644 --- a/libtransmission/bandwidth.h +++ b/libtransmission/bandwidth.h @@ -228,7 +228,8 @@ unsigned int tr_bandwidthGetPieceSpeed_Bps( const tr_bandwidth * bandwidth, void tr_bandwidthUsed ( tr_bandwidth * bandwidth, tr_direction direction, size_t byteCount, - tr_bool isPieceData ); + tr_bool isPieceData, + uint64_t now ); /****** ******* diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index e6c387fee..2deed8647 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -74,7 +74,7 @@ struct tr_openfile tr_file_index_t fileNum; char filename[TR_PATH_MAX]; int fd; - uint64_t date; + time_t date; }; struct tr_fdInfo @@ -474,7 +474,7 @@ tr_fdFileGetCached( tr_session * session, if( ( match != NULL ) && ( !doWrite || match->isWritable ) ) { - match->date = tr_time_msec( ); + match->date = tr_time( ); return match->fd; } @@ -532,7 +532,7 @@ tr_fdFileCheckout( tr_session * session, dbgmsg( "it's not already open. looking for an open slot or an old file." ); while( winner < 0 ) { - uint64_t date = tr_time_msec( ) + 1; + time_t date = tr_time( ) + 1; /* look for the file that's been open longest */ for( i=0; iopenFileLimit; ++i ) @@ -582,7 +582,7 @@ tr_fdFileCheckout( tr_session * session, dbgmsg( "checking out '%s' in slot %d", filename, winner ); o->torrentId = torrentId; o->fileNum = fileNum; - o->date = tr_time_msec( ); + o->date = tr_time( ); return o->fd; } diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 301bf3e48..1eeea1e28 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -98,11 +98,12 @@ didWriteWrapper( tr_peerIo * io, unsigned int bytes_transferred ) const unsigned int payload = MIN( next->length, bytes_transferred ); const unsigned int overhead = guessPacketOverhead( payload ); + const uint64_t now = tr_time_msec( ); - tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData ); + tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData, now ); if( overhead > 0 ) - tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE ); + tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE, now ); if( io->didWrite ) io->didWrite( io, payload, next->isPieceData, io->userData ); @@ -149,11 +150,16 @@ canReadWrapper( tr_peerIo * io ) assert( tr_isPeerIo( io ) ); - if( piece ) - tr_bandwidthUsed( &io->bandwidth, TR_DOWN, piece, TRUE ); + if( piece || (piece!=used) ) + { + const uint64_t now = tr_time_msec( ); + + if( piece ) + tr_bandwidthUsed( &io->bandwidth, TR_DOWN, piece, TRUE, now ); - if( used != piece ) - tr_bandwidthUsed( &io->bandwidth, TR_DOWN, used - piece, FALSE ); + if( used != piece ) + tr_bandwidthUsed( &io->bandwidth, TR_DOWN, used - piece, FALSE, now ); + } switch( ret ) { diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 91c86ed3f..88f5b0f1f 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -2918,7 +2918,9 @@ shouldPeerBeClosed( const Torrent * t, static void sortPeersByLivelinessReverse( tr_peer ** peers, void ** clientData, int n, uint64_t now ); static tr_peer ** -getPeersToClose( Torrent * t, tr_close_type_t closeType, const time_t now, int * setmeSize ) +getPeersToClose( Torrent * t, tr_close_type_t closeType, + const uint64_t now_msec, const time_t now_sec, + int * setmeSize ) { int i, peerCount, outsize; tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( &t->peers, &peerCount ); @@ -2927,10 +2929,10 @@ getPeersToClose( Torrent * t, tr_close_type_t closeType, const time_t now, int * assert( torrentIsLocked( t ) ); for( i = outsize = 0; i < peerCount; ++i ) - if( shouldPeerBeClosed( t, peers[i], peerCount, now ) == closeType ) + if( shouldPeerBeClosed( t, peers[i], peerCount, now_sec ) == closeType ) ret[outsize++] = peers[i]; - sortPeersByLivelinessReverse ( ret, NULL, outsize, tr_time_msec( ) ); + sortPeersByLivelinessReverse ( ret, NULL, outsize, now_msec ); *setmeSize = outsize; return ret; @@ -2997,10 +2999,8 @@ closePeer( Torrent * t, tr_peer * peer ) } static void -closeBadPeers( Torrent * t ) +closeBadPeers( Torrent * t, const uint64_t now_msec, const time_t now_sec ) { - const time_t now = tr_time( ); - if( !t->isRunning ) { removeAllPeers( t ); @@ -3012,7 +3012,7 @@ closeBadPeers( Torrent * t ) struct tr_peer ** mustClose; /* disconnect the really bad peers */ - mustClose = getPeersToClose( t, TR_MUST_CLOSE, now, &mustCloseCount ); + mustClose = getPeersToClose( t, TR_MUST_CLOSE, now_msec, now_sec, &mustCloseCount ); for( i=0; isession, tor ))) if( tor->isRunning ) - enforceTorrentPeerLimit( tor->torrentPeers, now ); + enforceTorrentPeerLimit( tor->torrentPeers, now_msec ); /* if we're over the per-session peer limits, cull some peers */ - enforceSessionPeerLimit( mgr->session, now ); + enforceSessionPeerLimit( mgr->session, now_msec ); /* remove crappy peers */ tor = NULL; while(( tor = tr_torrentNext( mgr->session, tor ))) - closeBadPeers( tor->torrentPeers ); + closeBadPeers( tor->torrentPeers, now_msec, now_sec ); /* try to make new peer connections */ makeNewPeerConnections( mgr, MAX_CONNECTIONS_PER_PULSE ); diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index 4ad14260c..efec44531 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -1359,7 +1359,7 @@ readBtPiece( tr_peermsgs * msgs, } } -static void updateDesiredRequestCount( tr_peermsgs * msgs, uint64_t now ); +static void updateDesiredRequestCount( tr_peermsgs * msgs ); static int readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen ) @@ -1398,7 +1398,7 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen ) case BT_UNCHOKE: dbgmsg( msgs, "got Unchoke" ); msgs->peer->clientIsChoked = 0; - updateDesiredRequestCount( msgs, tr_time_msec( ) ); + updateDesiredRequestCount( msgs ); break; case BT_INTERESTED: @@ -1685,7 +1685,7 @@ tr_peerMsgsIsReadingBlock( const tr_peermsgs * msgs, tr_block_index_t block ) **/ static void -updateDesiredRequestCount( tr_peermsgs * msgs, uint64_t now ) +updateDesiredRequestCount( tr_peermsgs * msgs ) { const tr_torrent * const torrent = msgs->torrent; @@ -1708,6 +1708,7 @@ updateDesiredRequestCount( tr_peermsgs * msgs, uint64_t now ) int irate_Bps; const int floor = 4; const int seconds = REQUEST_BUF_SECS; + const uint64_t now = tr_time_msec( ); /* Get the rate limit we should use. * FIXME: this needs to consider all the other peers as well... */ @@ -1975,7 +1976,7 @@ peerPulse( void * vmsgs ) const time_t now = tr_time( ); if ( tr_isPeerIo( msgs->peer->io ) ) { - updateDesiredRequestCount( msgs, tr_time_msec( ) ); + updateDesiredRequestCount( msgs ); updateBlockRequests( msgs ); updateMetadataRequests( msgs, now ); } @@ -2363,7 +2364,7 @@ tr_peerMsgsNew( struct tr_torrent * torrent, } tr_peerIoSetIOFuncs( m->peer->io, canRead, didWrite, gotError, m ); - updateDesiredRequestCount( m, tr_time_msec( ) ); + updateDesiredRequestCount( m ); return m; } -- 2.40.0