理解clock_gettime的问题
我在使用可通过 clock_gettime
访问的不同时钟时遇到困难。我特别感兴趣的是:
CLOCK_REALTIME
CLOCK_PROCESS_CPUTIME_ID
CLOCK_THREAD_COUTIME_ID
我阅读了联机帮助页,但它对我没有多大帮助。我使用clock_gettime
来为我的探查器在通过套接字发送收集的数据时生成时间戳。我注意到以下差异:
CLOCK_REALTIME
我使用此时钟从分析器收到的事件有时顺序错误。时间戳以较高的值开始,但不会高很多。通常,最后的消息(具有较高时间戳的消息)首先出现,然后出现具有较低值的时间戳。
CLOCK_PROCESS_CPUTIME_ID
CLOCK_THREAD_COUTIME_ID
我发现两个时钟没有区别,尽管它们以较小的值开始并且总是正确排序。
我无法解释这种行为。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的系统时钟源可能设置为 TSC 而不是 HPET。
一般来说,在现代多核系统中,HPET 是一个更准确和一致的新系统,而 TSC 是一个性能更高的旧系统。
找到当前时钟源。
在 openSUSE 上,您可以通过
cat /sys/devices/system/clocksource/clocksource0/current_clocksource
要在 openSUSE 上将时钟源设置为 HPET,请执行
echo 'hpet ' > /sys/devices/system/clocksource/clocksource0/current_clocksource
进一步阅读:
http://en. wikipedia.org/wiki/HPET
http://en.wikipedia.org/wiki/Time_Stamp_Counter< /a>
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
CLOCK_REALTIME
使您可以访问实时时钟,即存储当前日期和时间的时钟。CLOCK_MONOTONIC
使您可以访问一个永远不会倒退的时钟,您可能应该使用这个而不是CLOCK_REALTIME
。CLOCK_PROCESS_CPUTIME_ID
使您可以访问特定于当前进程的时钟,从而获得该进程的 CPU 时间(CPU 运行该特定进程所花费的时间)。CLOCK_THREAD_CPUTIME_ID
使您可以访问特定于当前线程的时钟,从而获得进程的 CPU 时间(CPU 运行该特定线程所花费的时间)。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 ofCLOCK_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).