Merge pull request #1070 from majestrate/monotonic-time-2020-01-23

make all timestamps monotonic and run loopback testnet at 1/5 speed
pull/1071/head
Jeff 5 years ago committed by GitHub
commit 68b22735d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -195,6 +195,8 @@ endif(USE_NETNS)
if(TESTNET) if(TESTNET)
add_definitions(-DTESTNET=1) add_definitions(-DTESTNET=1)
# 5 times slower than realtime
add_definitions(-DTESTNET_SPEED=5)
endif(TESTNET) endif(TESTNET)
if(SHADOW) if(SHADOW)

@ -836,6 +836,9 @@ namespace libuv
{ {
if(m_Run) if(m_Run)
{ {
#ifdef TESTNET_SPEED
ms *= TESTNET_SPEED;
#endif
uv_timer_start(m_TickTimer, &OnTickTimeout, ms, 0); uv_timer_start(m_TickTimer, &OnTickTimeout, ms, 0);
uv_run(&m_Impl, UV_RUN_ONCE); uv_run(&m_Impl, UV_RUN_ONCE);
} }
@ -872,6 +875,9 @@ namespace libuv
Loop::call_after_delay(llarp_time_t delay_ms, Loop::call_after_delay(llarp_time_t delay_ms,
std::function< void(void) > callback) std::function< void(void) > callback)
{ {
#ifdef TESTNET_SPEED
delay_ms *= TESTNET_SPEED;
#endif
PendingTimer timer; PendingTimer timer;
timer.delay_ms = delay_ms; timer.delay_ms = delay_ms;
timer.callback = callback; timer.callback = callback;

@ -6,24 +6,39 @@ namespace llarp
{ {
using Clock_t = std::chrono::system_clock; using Clock_t = std::chrono::system_clock;
template < typename Res > template < typename Res, typename Clock >
static llarp_time_t static llarp_time_t
time_since_epoch() time_since_epoch()
{ {
return std::chrono::duration_cast< Res >( return std::chrono::duration_cast< Res >(Clock::now().time_since_epoch())
llarp::Clock_t::now().time_since_epoch())
.count(); .count();
} }
// use std::chrono because otherwise the network breaks with Daylight Savings const static llarp_time_t started_at_system =
// this time, it doesn't get truncated -despair time_since_epoch< std::chrono::milliseconds, Clock_t >();
// that concern is what drove me back to the POSIX C time functions
// in the first place const static llarp_time_t started_at_steady =
time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >();
/// get our uptime in ms
static llarp_time_t
time_since_started()
{
return time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >()
- started_at_steady;
}
llarp_time_t llarp_time_t
time_now_ms() time_now_ms()
{ {
static llarp_time_t lastTime = 0; static llarp_time_t lastTime = 0;
auto t = llarp::time_since_epoch< std::chrono::milliseconds >(); auto t = time_since_started();
#ifdef TESTNET_SPEED
t /= TESTNET_SPEED;
#endif
t += started_at_system;
if(t <= lastTime) if(t <= lastTime)
{ {
return lastTime; return lastTime;

@ -5,6 +5,7 @@
namespace llarp namespace llarp
{ {
/// get time right now as milliseconds, this is monotonic
llarp_time_t llarp_time_t
time_now_ms(); time_now_ms();
} // namespace llarp } // namespace llarp

Loading…
Cancel
Save