Skip to content

Instantly share code, notes, and snippets.

@g-andrade
Created May 7, 2020 12:24
Show Gist options
  • Save g-andrade/508c779a931dde14c22c6e96319caa24 to your computer and use it in GitHub Desktop.
Save g-andrade/508c779a931dde14c22c6e96319caa24 to your computer and use it in GitHub Desktop.
-module(drift_test).
-export([run/1]).
run(Timeout) ->
MonoStartTs = erlang:monotonic_time(millisecond),
OsStartTs = os:system_time(millisecond),
receive
after
Timeout ->
MonoFinishTs = erlang:monotonic_time(millisecond),
OsFinishTs = os:system_time(millisecond),
#{ milliseconds_elapsed =>
#{ according_to_monotonic_clock => MonoFinishTs - MonoStartTs,
according_to_os_clock => OsFinishTs - OsStartTs
}
}
end.
@g-andrade
Copy link
Author

Result of executing it with a 30 seconds timeout while suspending the computer for roughly 20 seconds within the timeout window.

> drift_test:run(30000).
#{milliseconds_elapsed =>
      #{according_to_monotonic_clock => 30091,
        according_to_os_clock => 51650}

OS clock is in the right here - according to my phone timer, execution was blocked for roughly ~50 seconds (even though a 30 seconds timeout was specified and the computer awoke just upon the 30 seconds window coming to a close.)

@g-andrade
Copy link
Author

Tested on OTP 21.3.8.15 and GNU/Linux with a 4.15.0 kernel.

Root cause of the issue appears to be the use of CLOCK_MONOTONIC instead of CLOCK_BOOTTIME when calling clock_gettime - I think.
(This doesn't necessarily apply to other platforms.)

Configuring ERTS with --enable-prefer-elapsed-monotonic-time-during-suspend solved the issue; this makes sense, as the default value of the setting appears to be no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment