Konrad Reiche
Konrad Reiche

Reputation: 29493

Problem of understanding clock_gettime

I am having difficulties with the different clocks which can be accessed by clock_gettime. Especially I am interested in:

I read the manpage, but it didn't help me very much. I use clock_gettime in order to generate timestamps for my profiler when it sends the gathered data via socket. I have noticed the following differences:

The events I receive from my profiler with this clocks are sometimes, in a wrong order. The timestamps start with a higher value, though not very much higher. Often the last messages (those with a higher timestamp) appear first and later the timestamps with a lower value.

I found no difference on both clocks, though they start with a lesser value and are always correctly ordered.

I cannot explain this behavior.

Upvotes: 2

Views: 3669

Answers (2)

James Brock
James Brock

Reputation: 3426

Your system clock source is probably set to TSC instead of HPET.

On modern multi-core systems in general, HPET is a newer system that is more accurate and consistent, and TSC is an older system that is more performant.

On openSUSE, you can find out what your current clocksource is by

cat /sys/devices/system/clocksource/clocksource0/current_clocksource

To set your clock source to HPET on openSUSE, do

echo 'hpet' > /sys/devices/system/clocksource/clocksource0/current_clocksource

Further reading:

http://en.wikipedia.org/wiki/HPET

http://en.wikipedia.org/wiki/Time_Stamp_Counter

Upvotes: 2

ninjalj
ninjalj

Reputation: 43688

  • CLOCK_REALTIME gives you access to the real time clock, i.e. the one that stores the current date and time.
  • CLOCK_MONOTONIC gives you access to a clock that never goes back in time, you should probably use this one instead of CLOCK_REALTIME.
  • CLOCK_PROCESS_CPUTIME_ID gives you acces to a clock specific to the current process, giving you the CPU time of the process (time spent by the CPU running that specific process).
  • CLOCK_THREAD_CPUTIME_ID gives you acces to a clock specific to the current thread, giving you the CPU time of the process (time spent by the CPU running that specific thread).

Upvotes: 2

Related Questions