Problem of understanding clock_gettime

筅森魡賤 提交于 2019-12-06 14:50:11

问题


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

  • CLOCK_REALTIME
  • CLOCK_PROCESS_CPUTIME_ID
  • CLOCK_THREAD_COUTIME_ID

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:

  • CLOCK_REALTIME

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.

  • CLOCK_PROCESS_CPUTIME_ID
  • CLOCK_THREAD_COUTIME_ID

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

I cannot explain this behavior.


回答1:


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




回答2:


  • 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).


来源:https://stackoverflow.com/questions/5930621/problem-of-understanding-clock-gettime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!