[erlang-questions] now() seems to produce inconsistent timestamps.

Edwin Fine <>
Fri Aug 1 03:35:47 CEST 2008


Deep in the bowels of the C code for the Erlang emulator lurks this routine
(otp_src_R12B-3/erts/emulator/sys/unix/sys.c):

SysHrTime sys_gethrtime(void)
{
    struct timespec ts;
    long long result;
    if (*clock_gettime(CLOCK_**MONOTONIC,&ts)* != 0) {
    erl_exit(1,"Fatal, could not get clock_monotonic value!, "
         "errno = %d\n", errno);
    }
    result = ((long long) ts.tv_sec) * 1000000000LL +
    ((long long) ts.tv_nsec);
    return (SysHrTime) result;
}

This is used on some Unix and Unix-like systems (for example, my Ubuntu
Linux system) to get the high-resolution hardware timer, which sets the
microseconds part of the value returned by now().

There's a very telling note in the Linux man page for clock_gettime():

      The processors in an SMP system do not start all at exactly the same
time and therefore the timer registers are typically  running  at  an
offset.   Some
       architectures include code that attempts to limit these offsets on
bootup.  However, the code cannot guarantee to accurately tune the offsets.
Glibc con‐
       tains no provisions to deal with these offsets (unlike the Linux
Kernel).  Typically these offsets are small and therefore the effects may
be  negligible
       in most cases.

I can't guarantee it, but I imagine this is part of the discrepancy, and
that any operating system would have the same challenges because of SMP. In
any case, it would be quite a challenge to get two Erlang processes running
in different Erlang emulators to be so precisely synchronized that they
execute the now() call within 1000 microseconds of each other, even if you
could guarantee that each emulator was running on its own CPU.

Hope this helps.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080731/301d21e7/attachment.html>


More information about the erlang-questions mailing list