[LEAPSECS] Coding this week, and a trick for timeouts over leap seconds.
Warner Losh
imp at bsdimp.com
Sun Oct 2 12:27:25 EDT 2011
clock_gettime() with CLOCK_MONOTONIC as the clockid_t works well for systems that support it.
On Oct 2, 2011, at 2:21 AM, Hal Murray wrote:
>
>> Try using clock() instead of gettimeofday_in_millisecs(). The former nicely
>> increments with CLOCKS_PER_SEC resolution and is immune from UTC, timezones,
>> and leap seconds. At least it does on windows. Can someone comment on unix/
>> linux?
>
> My Linux box has a man page that says:
> The clock() function returns an approximation of processor time
> used by the program.
> So I don't think that's interesting for wait-a-while.
>
> select, sleep, and usleep are the wait-a-while routines on POSIX systems. I
> don't know how they are actually implemented. I could easily imagine
> somebody turning them into wait until now+delta which might get confused by
> leap seconds.
Most systems don't keep their time in leap-second-confusing units. Typically, they keep a time since boot (which might wrap) and then offset the human interesting form to that number. This makes most, but not all, interfaces immune to the general problem of time jumps. pthread_cond_timedwait is the notable exception, since it specifies an absolute time to wait until.
> unsigned int sleep(unsigned int seconds);
> sleep() makes the calling process sleep until seconds seconds
> have elapsed or a signal arrives which is not ignored.
>
> int usleep(useconds_t usec);
> The usleep() function suspends execution of the calling process
> for (at least) usec microseconds. The sleep may be lengthened
> slightly by any system activity or by the time spent processing
> the call or by the granularity of system timers.
>
> int select(int nfds, fd_set *readfds, fd_set *writefds,
> fd_set *exceptfds, struct timeval *timeout);
> timeout is an upper bound on the amount of time elapsed before
> select() returns. If both fields of the timeval stucture are
> zero, then select() returns immediately. (This is useful for
> polling.) If timeout is NULL (no timeout), select() can block
> indefinitely.
>
> If I wanted to test/debug this sort of code, I would use the cycle-counter.
> Most modern chips have one. On i386 it's called TSC. It's not "standard" or
> portable, but you can hide it in a subroutine if you want to try to make the
> code somewhat clean.
>
> Should we setup a web page with a recipe to fake a leap second at midnight
> tonight?
> I think it would be something like:
> kill ntpd
> run a program to tell the kernel leap-tonight
> wait till midnight
> run a program to tell the kernel no-leap
> restart ntpd
>
> I think that needs a simple program to set the leap-tonight bits.
This will handle the specific case of a leap second. However, date `date -v -1S +%Y%m%d%H%M.%S` would be a lot easier and handle the ntpd "step once" issue that might come into play. Just about any discontinuity in time is what you have to worry about. The specific leap-second one is merely annoying.
Warner
More information about the LEAPSECS
mailing list