]> granicus.if.org Git - transmission/commitdiff
(trunk libT) #3950 "use libevent's cached gettimeofday() value when appropriate"...
authorJordan Lee <jordan@transmissionbt.com>
Sat, 29 Jan 2011 18:59:23 +0000 (18:59 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sat, 29 Jan 2011 18:59:23 +0000 (18:59 +0000)
libtransmission/peer-io.c
libtransmission/peer-mgr.c
libtransmission/session.c
libtransmission/session.h

index 88c3271c49f4b66e1e21b6659313d685c0854f8c..b331cd09aa34cb340441eddd292278ea19b43bbd 100644 (file)
@@ -86,6 +86,7 @@ struct tr_datatype
     size_t   length;
 };
 
+
 /***
 ****
 ***/
@@ -99,7 +100,7 @@ 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( );
+        const uint64_t now = tr_sessionGetTimeMsec( io->session );
 
         tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData, now );
 
@@ -139,6 +140,8 @@ canReadWrapper( tr_peerIo * io )
     /* try to consume the input buffer */
     if( io->canRead )
     {
+        const uint64_t now = tr_sessionGetTimeMsec( io->session );
+
         tr_sessionLock( session );
 
         while( !done && !err )
@@ -148,7 +151,6 @@ canReadWrapper( tr_peerIo * io )
             const int ret = io->canRead( io, io->userData, &piece );
             const size_t used = oldLen - evbuffer_get_length( io->inbuf );
             const unsigned int overhead = guessPacketOverhead( used );
-            const uint64_t now = tr_time_msec( );
 
             assert( tr_isPeerIo( io ) );
 
index de183c474c2268d70b07aa60b9fca2a8b5b6810d..cc8dec10228fbb67e419df61a33d91ca4fe21e25 100644 (file)
@@ -2750,12 +2750,12 @@ rechokeUploads( Torrent * t, const uint64_t now )
 static void
 rechokePulse( int foo UNUSED, short bar UNUSED, void * vmgr )
 {
-    uint64_t now;
     tr_torrent * tor = NULL;
     tr_peerMgr * mgr = vmgr;
+    const uint64_t now = tr_sessionGetTimeMsec( mgr->session );
+
     managerLock( mgr );
 
-    now = tr_time_msec( );
     while(( tor = tr_torrentNext( mgr->session, tor ))) {
         if( tor->isRunning ) {
             rechokeUploads( tor->torrentPeers, now );
@@ -3113,7 +3113,7 @@ reconnectPulse( int foo UNUSED, short bar UNUSED, void * vmgr )
     tr_torrent * tor;
     tr_peerMgr * mgr = vmgr;
     const time_t now_sec = tr_time( );
-    const uint64_t now_msec = tr_time_msec( );
+    const uint64_t now_msec = tr_sessionGetTimeMsec( mgr->session );
 
     /**
     ***  enforce the per-session and per-torrent peer limits
index cc3229f2ad8ce43583ae0cca92ac492c47a5a81d..0c1b18b8149c0098589b4f0e5ee0b4fdeb50248d 100644 (file)
@@ -2558,3 +2558,36 @@ tr_sessionSetWebConfigFunc( tr_session * session, void (*func)(tr_session*, void
 {
     session->curl_easy_config_func = func;
 }
+
+/***
+****
+***/
+
+uint64_t
+tr_sessionGetTimeMsec( tr_session * session )
+{
+    struct timeval tv;
+
+    if( event_base_gettimeofday_cached( session->event_base, &tv ) )
+    {
+        return tr_time_msec( );
+    }
+    else
+    {
+        /* event_base_gettimeofday_cached() might be implemented using
+           clock_gettime(CLOCK_MONOTONIC), so calculate the offset to
+           real time... */
+        static uint64_t offset;
+        static tr_bool offset_calculated = FALSE;
+
+        const uint64_t val = (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
+
+        if( !offset_calculated )
+        {
+            offset = tr_time_msec() - val;
+            offset_calculated = TRUE;
+        }
+
+        return val + offset;
+    }
+}
index ce8239460ca16ff8da753412342a0bd26d3645ea..792768aa1b694d368d8b2280018ecf7a0a969e0f 100644 (file)
@@ -305,4 +305,14 @@ tr_bool  tr_sessionGetActiveSpeedLimit_Bps( const tr_session  * session,
                                             int               * setme );
 
 
+/**
+ * Tries to use libevent's cached timeval so we can avoid excessive calls
+ * to gettimeofday().
+ *
+ * This isn't for all uses, but should be reasonably accurate when called
+ * near the beginning of a libevent callback.
+ */
+uint64_t tr_sessionGetTimeMsec( tr_session * session );
+
+
 #endif