(trunk libT) #2610 "avoid unnecessary calls to time(NULL)"

This commit is contained in:
Charles Kerr 2009-11-26 18:47:08 +00:00
parent d096b39f4b
commit e1c6b792aa
15 changed files with 110 additions and 57 deletions

View File

@ -391,7 +391,7 @@ tierNew( tr_torrent * tor )
{
tr_tier * t;
static int nextKey = 1;
const time_t now = time( NULL );
const time_t now = tr_time( );
t = tr_new0( tr_tier, 1 );
t->key = nextKey++;
@ -817,7 +817,7 @@ static tr_bool
tierCanManualAnnounce( const tr_tier * tier )
{
return tier->isRunning
&& tier->manualAnnounceAllowedAt <= time( NULL );
&& tier->manualAnnounceAllowedAt <= tr_time( );
}
tr_bool
@ -900,22 +900,22 @@ torrentSetNextAnnounce( tr_torrent * tor, const char * announceEvent, time_t ann
void
tr_announcerManualAnnounce( tr_torrent * tor )
{
torrentSetNextAnnounce( tor, "manual", time( NULL ) );
torrentSetNextAnnounce( tor, "manual", tr_time( ) );
}
void
tr_announcerTorrentStarted( tr_torrent * tor )
{
torrentSetNextAnnounce( tor, "started", time( NULL ) );
torrentSetNextAnnounce( tor, "started", tr_time( ) );
}
void
tr_announcerTorrentStopped( tr_torrent * tor )
{
torrentSetNextAnnounce( tor, "stopped", time( NULL ) );
torrentSetNextAnnounce( tor, "stopped", tr_time( ) );
}
void
tr_announcerTorrentCompleted( tr_torrent * tor )
{
torrentSetNextAnnounce( tor, "completed", time( NULL ) );
torrentSetNextAnnounce( tor, "completed", tr_time( ) );
}
void
tr_announcerChangeMyPort( tr_torrent * tor )
@ -980,7 +980,7 @@ compareTiers( const void * va, const void * vb )
/* working domains come before non-working */
if( !ret ) {
const time_t now = time( NULL );
const time_t now = tr_time( );
af = tierIsNotResponding( a, now );
bf = tierIsNotResponding( b, now );
if( af != bf )
@ -1356,7 +1356,7 @@ tierAnnounce( tr_announcer * announcer, tr_tier * tier )
char * url;
struct announce_data * data;
const tr_torrent * tor = tier->tor;
const time_t now = time( NULL );
const time_t now = tr_time( );
assert( !tier->isAnnouncing );
@ -1452,7 +1452,7 @@ onScrapeDone( tr_session * session,
tr_announcer * announcer = session->announcer;
struct announce_data * data = vdata;
tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
const time_t now = time( NULL );
const time_t now = tr_time( );
tr_bool success = FALSE;
if( announcer )
@ -1528,7 +1528,7 @@ tierScrape( tr_announcer * announcer, tr_tier * tier )
const char * scrape;
struct evbuffer * buf;
struct announce_data * data;
const time_t now = time( NULL );
const time_t now = tr_time( );
assert( tier );
assert( !tier->isScraping );
@ -1597,7 +1597,7 @@ announceMore( tr_announcer * announcer )
const tr_bool canAnnounce = announcer->announceSlotsAvailable > 0;
const tr_bool canScrape = announcer->scrapeSlotsAvailable > 0;
tr_torrent * tor = NULL;
const time_t now = time( NULL );
const time_t now = tr_time( );
if( announcer->announceSlotsAvailable > 0 )
{
@ -1705,7 +1705,7 @@ tr_announcerStats( const tr_torrent * torrent,
int out = 0;
int tierCount;
tr_tracker_stat * ret;
const time_t now = time( NULL );
const time_t now = tr_time( );
assert( tr_isTorrent( torrent ) );

View File

@ -99,13 +99,13 @@ tr_natpmpClose( tr_natpmp * nat )
static int
canSendCommand( const struct tr_natpmp * nat )
{
return time( NULL ) >= nat->commandTime;
return tr_time( ) >= nat->commandTime;
}
static void
setCommandTime( struct tr_natpmp * nat )
{
nat->commandTime = time( NULL ) + COMMAND_WAIT_SECS;
nat->commandTime = tr_time( ) + COMMAND_WAIT_SECS;
}
int
@ -189,7 +189,7 @@ tr_natpmpPulse( struct tr_natpmp * nat,
if( isEnabled && !nat->isMapped && nat->hasDiscovered )
nat->state = TR_NATPMP_SEND_MAP;
else if( nat->isMapped && time( NULL ) >= nat->renewTime )
else if( nat->isMapped && tr_time( ) >= nat->renewTime )
nat->state = TR_NATPMP_SEND_MAP;
}
@ -214,7 +214,7 @@ tr_natpmpPulse( struct tr_natpmp * nat,
{
nat->state = TR_NATPMP_IDLE;
nat->isMapped = 1;
nat->renewTime = time( NULL ) + LIFETIME_SECS;
nat->renewTime = tr_time( ) + LIFETIME_SECS;
nat->port = resp.pnu.newportmapping.privateport;
tr_ninf( getKey( ), _(
"Port %d forwarded successfully" ), nat->port );

View File

@ -612,7 +612,7 @@ tr_globalIPv6( void )
static unsigned char ipv6[16];
static time_t last_time = 0;
static int have_ipv6 = 0;
const time_t now = time( NULL );
const time_t now = tr_time( );
/* Re-check every half hour */
if( last_time < now - 1800 )

View File

@ -385,7 +385,7 @@ tr_peerIoNew( tr_session * session,
io->socket = socket;
io->isIncoming = isIncoming != 0;
io->hasFinishedConnecting = FALSE;
io->timeCreated = time( NULL );
io->timeCreated = tr_time( );
io->inbuf = evbuffer_new( );
io->outbuf = evbuffer_new( );
tr_bandwidthConstruct( &io->bandwidth, session, parent );

View File

@ -408,7 +408,7 @@ removePeer( Torrent * t, tr_peer * peer )
assert( torrentIsLocked( t ) );
assert( atom );
atom->time = time( NULL );
atom->time = tr_time( );
removed = tr_ptrArrayRemoveSorted( &t->peers, peer, peerCompare );
assert( removed == peer );
@ -916,7 +916,7 @@ tr_peerMgrGetNextRequests( tr_torrent * tor,
Torrent * t;
struct weighted_piece * pieces;
const tr_bitset * have = &peer->have;
const time_t now = time( NULL );
const time_t now = tr_time( );
/* sanity clause */
assert( tr_isTorrent( tor ) );
@ -1032,7 +1032,7 @@ refillUpkeep( void * vmgr )
tr_peerMgr * mgr = vmgr;
managerLock( mgr );
now = time( NULL );
now = tr_time( );
too_old = now - REQUEST_TTL_SECS;
tor = NULL;
@ -1215,7 +1215,7 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
case TR_PEER_PEER_GOT_DATA:
{
const time_t now = time( NULL );
const time_t now = tr_time( );
tr_torrent * tor = t->tor;
tr_torrentSetActivityDate( tor, now );
@ -1263,7 +1263,7 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
case TR_PEER_CLIENT_GOT_DATA:
{
const time_t now = time( NULL );
const time_t now = tr_time( );
tr_torrent * tor = t->tor;
tr_torrentSetActivityDate( tor, now );
@ -1410,7 +1410,6 @@ getDefaultShelfLife( uint8_t from )
static void
ensureAtomExists( Torrent * t,
const time_t now,
const tr_address * addr,
const tr_port port,
const uint8_t flags,
@ -1429,7 +1428,7 @@ ensureAtomExists( Torrent * t,
a->port = port;
a->flags = flags;
a->from = from;
a->shelf_date = now + getDefaultShelfLife( from ) + jitter;
a->shelf_date = tr_time( ) + getDefaultShelfLife( from ) + jitter;
tr_ptrArrayInsertSorted( &t->pool, a, compareAtomsByAddress );
tordbg( t, "got a new atom: %s", tr_atomAddrStr( a ) );
@ -1500,11 +1499,10 @@ myHandshakeDoneCB( tr_handshake * handshake,
else /* looking good */
{
struct peer_atom * atom;
const time_t now = time( NULL );
ensureAtomExists( t, now, addr, port, 0, TR_PEER_FROM_INCOMING );
ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING );
atom = getExistingAtom( t, addr );
atom->time = now;
atom->time = tr_time( );
atom->piece_data_time = 0;
if( atom->myflags & MYFLAG_BANNED )
@ -1614,7 +1612,7 @@ tr_peerMgrAddPex( tr_torrent * tor,
if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->addr ) )
if( tr_isValidPeerAddress( &pex->addr, pex->port ) )
ensureAtomExists( t, time( NULL ), &pex->addr, pex->port, pex->flags, from );
ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from );
managerUnlock( t->manager );
}
@ -2596,7 +2594,7 @@ reconnectTorrent( Torrent * t )
{
static time_t prevTime = 0;
static int newConnectionsThisSecond = 0;
const time_t now = time( NULL );
const time_t now = tr_time( );
if( prevTime != now )
{
@ -3037,7 +3035,7 @@ atomPulse( void * vmgr )
/* if there's room, keep the best of what's left */
i = 0;
if( keepCount < maxAtomCount ) {
tr_now = time( NULL );
tr_now = tr_time( );
qsort( test, testCount, sizeof( struct peer_atom * ), compareAtomPtrsByShelfDate );
while( i<testCount && keepCount<maxAtomCount )
keep[keepCount++] = test[i++];

View File

@ -783,7 +783,7 @@ void
tr_peerMsgsSetChoke( tr_peermsgs * msgs,
int choke )
{
const time_t now = time( NULL );
const time_t now = tr_time( );
const time_t fibrillationTime = now - MIN_CHOKE_PERIOD_SEC;
assert( msgs );
@ -1996,7 +1996,7 @@ static int
peerPulse( void * vmsgs )
{
tr_peermsgs * msgs = vmsgs;
const time_t now = time( NULL );
const time_t now = tr_time( );
if ( tr_isPeerIo( msgs->peer->io ) ) {
updateDesiredRequestCount( msgs, now );
@ -2325,7 +2325,7 @@ sendPex( tr_peermsgs * msgs )
tr_free( diffs6.dropped );
tr_free( newPex6 );
/*msgs->clientSentPexAt = time( NULL );*/
/*msgs->clientSentPexAt = tr_time( );*/
}
}

View File

@ -92,7 +92,7 @@ struct tr_rpc_server
static char*
get_current_session_id( struct tr_rpc_server * server )
{
const time_t now = time( NULL );
const time_t now = tr_time( );
if( !server->sessionId || ( now >= server->sessionIdExpiresAt ) )
{
@ -422,7 +422,7 @@ serve_file( struct evhttp_request * req,
else
{
struct evbuffer * out;
const time_t now = time( NULL );
const time_t now = tr_time( );
errno = error;
out = evbuffer_new( );

View File

@ -150,7 +150,7 @@ getTorrents( tr_session * session,
if( !strcmp( str, "recently-active" ) )
{
tr_torrent * tor = NULL;
const time_t now = time( NULL );
const time_t now = tr_time( );
const time_t window = RECENTLY_ACTIVE_SECONDS;
const int n = tr_sessionCountTorrents( session );
torrents = tr_new0( tr_torrent *, n );
@ -625,7 +625,7 @@ torrentGet( tr_session * session,
if( tr_bencDictFindStr( args_in, "ids", &strVal ) && !strcmp( strVal, "recently-active" ) ) {
int n = 0;
tr_benc * d;
const time_t now = time( NULL );
const time_t now = tr_time( );
const int interval = RECENTLY_ACTIVE_SECONDS;
tr_benc * removed_out = tr_bencDictAddList( args_out, "removed", 0 );
while(( d = tr_bencListChild( &session->removedTorrents, n++ ))) {

View File

@ -628,6 +628,22 @@ tr_sessionInit( const char * tag,
static void useAltSpeed( tr_session * session, tr_bool enabled, tr_bool byUser );
static void useAltSpeedTime( tr_session * session, tr_bool enabled, tr_bool byUser );
static void
onNowTimer( int foo UNUSED, short bar UNUSED, void * vsession )
{
struct timeval tv;
tr_session * session = vsession;
assert( tr_isSession( session ) );
assert( session->nowTimer != NULL );
/* schedule the next timer for right after the next second begins */
gettimeofday( &tv, NULL );
tr_timerAdd( session->nowTimer, 0, 1000000 - tv.tv_usec );
tr_timeUpdate( tv.tv_sec );
/* fprintf( stderr, "time %zu sec, %zu microsec\n", (size_t)tr_time(), (size_t)tv.tv_usec ); */
}
static void
tr_sessionInitImpl( void * vdata )
{
@ -646,6 +662,10 @@ tr_sessionInitImpl( void * vdata )
tr_sessionGetDefaultSettings( data->configDir, &settings );
tr_bencMergeDicts( &settings, clientSettings );
session->nowTimer = tr_new0( struct event, 1 );
evtimer_set( session->nowTimer, onNowTimer, session );
onNowTimer( 0, 0, session );
#ifndef WIN32
/* Don't exit when writing on a broken socket */
signal( SIGPIPE, SIG_IGN );
@ -1516,6 +1536,10 @@ sessionCloseImpl( void * vsession )
tr_free( session->saveTimer );
session->saveTimer = NULL;
evtimer_del( session->nowTimer );
tr_free( session->nowTimer );
session->nowTimer = NULL;
evtimer_del( session->altTimer );
tr_free( session->altTimer );
session->altTimer = NULL;

View File

@ -130,6 +130,7 @@ struct tr_session
tr_benc * metainfoLookup;
struct event * altTimer;
struct event * nowTimer;
struct event * saveTimer;
/* monitors the "global pool" speeds */

View File

@ -113,7 +113,7 @@ tr_statsInit( tr_session * session )
loadCumulativeStats( session, &stats->old );
stats->single.sessionCount = 1;
stats->startTime = time( NULL );
stats->startTime = tr_time( );
session->sessionStats = stats;
}
@ -167,7 +167,7 @@ tr_sessionGetStats( const tr_session * session,
if( stats )
{
*setme = stats->single;
setme->secondsActive = time( NULL ) - stats->startTime;
setme->secondsActive = tr_time( ) - stats->startTime;
updateRatio( setme );
}
}
@ -199,7 +199,7 @@ tr_sessionClearStats( tr_session * session )
zero.secondsActive = 0;
session->sessionStats->single = session->sessionStats->old = zero;
session->sessionStats->startTime = time( NULL );
session->sessionStats->startTime = tr_time( );
}
/**

View File

@ -671,8 +671,8 @@ torrentInit( tr_torrent * tor, const tr_ctor * ctor )
tr_torrentUncheck( tor );
tr_torrentSetAddedDate( tor, time( NULL ) ); /* this is a default value to be
overwritten by the resume file */
tr_torrentSetAddedDate( tor, tr_time( ) ); /* this is a default value to be
overwritten by the resume file */
torrentInitFromInfo( tor );
loaded = tr_torrentLoadResume( tor, ~0, ctor );
@ -891,7 +891,7 @@ tr_torrentInfo( const tr_torrent * tor )
const tr_stat *
tr_torrentStatCached( tr_torrent * tor )
{
const time_t now = time( NULL );
const time_t now = tr_time( );
return tr_isTorrent( tor ) && ( now == tor->lastStatTime )
? &tor->stats
@ -905,7 +905,7 @@ tr_torrentSetVerifyState( tr_torrent * tor, tr_verify_state state )
assert( state==TR_VERIFY_NONE || state==TR_VERIFY_WAIT || state==TR_VERIFY_NOW );
tor->verifyState = state;
tor->anyDate = time( NULL );
tor->anyDate = tr_time( );
}
tr_torrent_activity
@ -943,7 +943,7 @@ tr_torrentStat( tr_torrent * tor )
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
tor->lastStatTime = time( NULL );
tor->lastStatTime = tr_time( );
s = &tor->stats;
s->id = tor->uniqueId;
@ -1354,7 +1354,7 @@ checkAndStartImpl( void * vtor )
}
else
{
const time_t now = time( NULL );
const time_t now = tr_time( );
tor->isRunning = TRUE;
tor->needsSeedRatioCheck = TRUE;
tor->error = TR_STAT_OK;
@ -1533,7 +1533,7 @@ closeTorrent( void * vtor )
d = tr_bencListAddDict( &tor->session->removedTorrents, 2 );
tr_bencDictAddInt( d, "id", tor->uniqueId );
tr_bencDictAddInt( d, "date", time( NULL ) );
tr_bencDictAddInt( d, "date", tr_time( ) );
stopTorrent( tor );
@ -1682,7 +1682,7 @@ tr_torrentRecheckCompleteness( tr_torrent * tor )
{
tr_announcerTorrentCompleted( tor );
tor->doneDate = tor->anyDate = time( NULL );
tor->doneDate = tor->anyDate = tr_time( );
}
tr_torrentSetDirty( tor );

View File

@ -44,6 +44,7 @@
#include "platform.h"
#include "version.h"
static tr_lock * messageLock = NULL;
static int messageLevel = 0;
static tr_bool messageQueuing = FALSE;
@ -56,6 +57,23 @@ static tr_msg_list ** messageQueueTail = &messageQueue;
static void OutputDebugString( const void * unused UNUSED ) { }
#endif
/***
****
***/
time_t transmission_now = 0;
void
tr_timeUpdate( time_t now )
{
transmission_now = now;
}
/***
****
***/
static void
tr_msgInit( void )
{
@ -201,15 +219,13 @@ char*
tr_getLogTimeStr( char * buf, int buflen )
{
char tmp[64];
time_t now;
struct tm now_tm;
struct timeval tv;
int milliseconds;
now = time( NULL );
gettimeofday( &tv, NULL );
tr_localtime_r( &now, &now_tm );
tr_localtime_r( &tv.tv_sec, &now_tm );
strftime( tmp, sizeof( tmp ), "%H:%M:%S", &now_tm );
milliseconds = (int)( tv.tv_usec / 1000 );
tr_snprintf( buf, buflen, "%s.%03d", tmp, milliseconds );
@ -325,7 +341,7 @@ tr_msg( const char * file, int line,
tr_msg_list * newmsg;
newmsg = tr_new0( tr_msg_list, 1 );
newmsg->level = level;
newmsg->when = time( NULL );
newmsg->when = tr_time( );
newmsg->message = tr_strdup( buf );
newmsg->file = file;
newmsg->line = line;

View File

@ -19,7 +19,7 @@
#include <stdio.h> /* FILE* */
#include <string.h> /* memcpy()* */
#include <stdlib.h> /* malloc() */
#include <time.h> /* time_t* */
#include <time.h> /* time_t */
#include "transmission.h"
@ -430,6 +430,20 @@ int tr_moveFile( const char * oldpath, const char * newpath,
void tr_removeElementFromArray( void * array, int index_to_remove,
size_t sizeof_element, size_t nmemb );
/***
****
***/
extern time_t transmission_now;
static TR_INLINE time_t tr_time( void ) { return transmission_now; }
void tr_timeUpdate( time_t now );
/***
****
***/
#ifdef __cplusplus
}
#endif

View File

@ -57,7 +57,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
tr_piece_index_t pieceIndex = 0;
const int64_t buflen = tor->info.pieceSize;
uint8_t * buffer = tr_new( uint8_t, buflen );
const time_t begin = time( NULL );
const time_t begin = tr_time( );
time_t end;
SHA1_Init( &sha );
@ -127,7 +127,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
changed = TRUE;
}
tr_torrentSetPieceChecked( tor, pieceIndex, TRUE );
now = time( NULL );
now = tr_time( );
tor->anyDate = now;
/* sleeping even just a few msec per second goes a long
@ -159,7 +159,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
tr_free( buffer );
/* stopwatch */
end = time( NULL );
end = tr_time( );
tr_tordbg( tor, "it took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
(int)(end-begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(end-begin))) );