Get system timestamp less often (only after sleeping/selecting)

This commit is contained in:
Keith Winstein
2012-07-26 20:54:19 -04:00
parent 5e3ec2e037
commit a744004bf6
7 changed files with 150 additions and 44 deletions
+3 -43
View File
@@ -40,19 +40,13 @@
#include <errno.h>
#include <unistd.h>
#if HAVE_CLOCK_GETTIME
#include <time.h>
#elif HAVE_MACH_ABSOLUTE_TIME
#include <mach/mach_time.h>
#elif HAVE_GETTIMEOFDAY
#include <sys/time.h>
#endif
#include "dos_assert.h"
#include "byteorder.h"
#include "network.h"
#include "crypto.h"
#include "timestamp.h"
using namespace std;
using namespace Network;
using namespace Crypto;
@@ -386,41 +380,7 @@ int Connection::port( void ) const
uint64_t Network::timestamp( void )
{
#if HAVE_CLOCK_GETTIME
struct timespec tp;
if ( clock_gettime( CLOCK_MONOTONIC, &tp ) < 0 ) {
throw NetworkException( "clock_gettime", errno );
}
uint64_t millis = tp.tv_nsec / 1000000;
millis += uint64_t( tp.tv_sec ) * 1000;
return millis;
#elif HAVE_MACH_ABSOLUTE_TIME
static mach_timebase_info_data_t s_timebase_info;
if (s_timebase_info.denom == 0) {
mach_timebase_info(&s_timebase_info);
}
// NB: mach_absolute_time() returns "absolute time units"
// We need to apply a conversion to get milliseconds.
return ((mach_absolute_time() * s_timebase_info.numer) / (1000000 * s_timebase_info.denom));
#elif HAVE_GETTIMEOFDAY
// NOTE: If time steps backwards, timeouts may be confused.
struct timeval tv;
if ( gettimeofday(&tv, NULL) ) {
throw NetworkException( "gettimeofday", errno );
}
uint64_t millis = tv.tv_usec / 1000;
millis += uint64_t( tv.tv_sec ) * 1000;
return millis;
#else
# error "Don't know how to get a timestamp on this platform"
#endif
return frozen_timestamp();
}
uint16_t Network::timestamp16( void )