-   Programming (
-   -   clock_gettime(CLOCK_MONOTONIC,..) fails (

nathan2225 07-18-2006 08:50 AM

clock_gettime(CLOCK_MONOTONIC,..) fails
Hi i want to use a high resolution clock to support my realtime app.
I was using gettimeofday() b4, but now i want to sync time at some machines and now this timebase is no longer a stable one.

So i tried to use not CLOCK_REALTIME and found CLOCK_MONOTONIC.

Had to link librt to my app, compiles fine but :

If i call clock_gettime with clockid CLOCK_MONOTONIC on my target machine, i got a retval of -1, meaning this is not supported by the system.
Running my app with the same kernel on my developing machine, this works quite fine but on the target system (a cyrix C3-2) it just returns -1 !!

Can somebody explain me, why on my cyrix machine it does not work and on a plain P4 desktop it does (with the same (self-compiled) kernel)? Can it be a problem with glibc or really with the hardware ?

I also found out, that on the cyrix, the clock_getres for REALTIME gaves me about 10ms and on the P4 it says about 1ms .....

Does someone know other methods to get a monothonic clock with at least 1ms res ?

Btw, i'm using kernel 2.6.16-16


Matir 07-18-2006 10:43 AM

Moved: This thread is more suitable in programming and has been moved accordingly to help your thread/question get the exposure it deserves.

Matir 07-18-2006 10:44 AM

Check if the symbol _POSIX_MONOTONIC_CLOCK is defined. If not, your glibc is insufficiently new. Also, what does errno show? EINVAL?

nathan2225 07-18-2006 03:23 PM

Good idea, i'll check tomorrow.

How can i test if these options are defined in the glibc
(At runtime, by cmdline check) ?

Matir 07-18-2006 03:31 PM

Try something like:

#error No monotonic clock available!

nathan2225 07-19-2006 02:06 AM

Hmm, the include files (bits/posix_opt.h) say :


which has to say : monotonic clock might be available, has to be checked at runtime !

OK, so i tried the sysconf call sysconf(_SC_MONOTONIC_CLOCK) :

And surprise, surprise, running my app on target system (VIA/Cyrix C3-2 CPU) with glibc-2.3.6, it says : -1 (not available)

running app on my workstation delivers : 1 !!!

So whom can i blame for this ? The glibc, the kernel, the hardware ????


And because u asked: the retval of clock_gettime is : Invalid argument (errno 22)

nathan2225 07-21-2006 04:52 AM

Success !
OK, i've found out : It was the glibc

Somehow all the slackware packages (i'm using slackware tgz packages) were compiled with emulated timers, not using the timers offered by the OS.
I tried a debian compiled glibc package and it suddenly worked, hurray !!

Thanks to Matir for the hints !


Matir 07-21-2006 09:35 AM

No problem. I learned quite a bit from this thread myself. I'm glad we were able to help you out.

All times are GMT -5. The time now is 01:42 PM.