[PATCH] use CLOCK_MONOTONIC_COARSE posix timer instead of CLOCK_MONOTONIC in Xorg

ykzhao yakui.zhao at intel.com
Mon Aug 23 20:05:23 PDT 2010


On Tue, 2010-08-24 at 10:12 +0800, Daniel Stone wrote:
> Hi,
> 
> On Tue, Aug 24, 2010 at 08:55:22AM +0800, ykzhao wrote:
> > What Mark mentioned is that the CLOCK_MONOTONIC_COARSE posix timer is
> > not supported while the corresponding ID is used for other posix timer.
> > Right? 
> 
> 6 has no meaning to clock_gettime().  CLOCK_MONOTONIC_COARSE is the only
> thing that has any meaning: if it's not defined, you can't just invent a
> definition and hope that it works.  That's why the spec says
> CLOCK_MONOTONIC_COARSE and not 6.

In theory the CLOCK_MONOTONIC_COARSE posix timer id will be defined in
the /usr/include/bits/time.h.

Maybe there is no definition of "CLOCK_MONOTONIC_COARSE"
in /usr/include/bits/time.h when compiling the xorg. But the xorg is
executed on the linux kernel that supports the CLOCK_MONOTONIC_COARSE
posix timer.(In fact for most previous Linux distribution there is no
definition of CLOCK_MONOTONIC_COARSE in /usr/include/bits/time.h). 
If it is executed on the kernel that doesn't support the
CLOCK_MONOTONIC_COARSE timer, it will fallback to the CLOCK_MONOTOIC
posix timer(the function of clock_getres will return the invalid value).

Do we need to consider the above scenario? If the above scenario doesn't
need to be cared, I will update the patch to assure that
the CLOCK_MONOTONIC_COARSE posix timer will be tried only when there
exists the corresponding definition. 

> > If so, is there an approach that helps us to detect whether the
> > CLOCK_MONOTONIC_COARSE posix timer is supported on one OS?
> 
> Try the following completely untested patch (hey, it compiles).  It's
> not perfect though: if CLOCK_MONOTONIC or CLOCK_MONOTONIC_COARSE were
> ever 0, we'd make two or three syscalls for GetTimeInMillis() instead of
> one, and if either of them were ~0L, we'd never use them.

the corresponding code is put under the condition definition of
MONOTONIC_CLOCK. This is already checked by using configure script.

    In theory the CLOCK_MONOTONIC exists if the MONOTONIC_CLOCK is
defined.  Not sure whether it is still necessary to check the
CLOCK_MONOTONIC again?

Best regards.
   
> 
> Cheers,
> Daniel
> 
> diff --git a/os/utils.c b/os/utils.c
> index 51455cc..a1659ec 100644
> --- a/os/utils.c
> +++ b/os/utils.c
> @@ -427,7 +427,20 @@ GetTimeInMillis(void)
>  
>  #ifdef MONOTONIC_CLOCK
>      struct timespec tp;
> -    if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
> +    static clockid_t clockid;
> +    if (!clockid) {
> +#ifdef CLOCK_MONOTONIC_COARSE
> +        if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
> +            (tp.tv_nsec / 1000) <= 1000)
> +            clockid = CLOCK_MONOTONIC_COARSE;
> +        else
> +#endif
> +        if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
> +            clockid = CLOCK_MONOTONIC;
> +        else
> +            clockid = ~0L;
> +    }
> +    if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
>          return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
>  #endif
>  



More information about the xorg-devel mailing list