Re: How to read xtime

H. Peter Anvin (hpa@transmeta.com)
26 Feb 1999 07:07:38 GMT


Followup to: <199902260644.WAA21884@piglet.twiddle.net>
By author: David Miller <davem@twiddle.net>
In newsgroup: linux.dev.kernel
>
>
> Perhaps I am missing something, but I believe this is dealt with on
> Sparc already in a more clever and efficient way (the idea is actually
> by Van Jacobson to the best of my knowledge, his is the earliest
> implementation of this "trick" that I am aware of)). The algorithm,
> in pseudo assembly, is:
>
> retry: ldd [xtime], %reg1 /* load all 8 bytes of xtime */
> ld [timer_reg], %reg2 /* snapshot timer counter */
> ldd [xtime], %reg3 /* load second copy of xtime */
> cmp %reg1, %reg3 /* Did it change in between? */
> bne retry /* Yep, try once more */
> nop
>
> No locks, no interrupt disabling, etc. I suppose using this technique
> would be extremely troublesome if the architecture in question has no
> method to load an aligned doublet of time_t's at once. But I believe
> ix86 actually can.
>

Not unless you're talking floating-point/MMX, or using the RDTSC
instruction.

However, if you have a reasonable limit on how long you may be
interrupted (1/2 of the major increment) you can do this without
loops:

mov (xtime_sec),%ecx
mov (xtime_usec),%eax
mov (xtime_sec),%edx
cmp %eax,500000 ; 1/2 of the major increment
jb 1f
mov %ecx,%edx
1: ; Result in %edx:%eax

If the minor counter is less than halfway to rollover, it is safe to
assume the value read immediately *after* is stable; if it's more than
halfway, that the value read immediately *before* is stable. One
advantage with not having a loop is that you don't bias the output.

-hpa

-- 
"Linux is a very complete and sophisticated operating system.  There
are, and will be, large numbers of applications available for it."
    -- Paul Maritz, Group Vice President for Platforms And Applications,
       Microsoft Corporation [Reference at: http://www.kernel.org/~hpa/ms.html]

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/