Linux CLOCK_MONOTONIC 和 CLOCK_MONOTONIC_RAW 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14270300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:38:09  来源:igfitidea点击:

What is the difference between CLOCK_MONOTONIC & CLOCK_MONOTONIC_RAW?

linuxubuntutiming

提问by Matt

According to the Linux man page under Ubuntu

根据 Ubuntu 下的 Linux 手册页

CLOCK_MONOTONIC
      Clock that cannot be set and  represents  monotonic  time  since
      some unspecified starting point.

CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
      Similar  to  CLOCK_MONOTONIC, but provides access to a raw hard‐
      ware-based time that is not subject to NTP adjustments.

According to the webster online dictionary Monotonic means:

根据韦氏在线词典 Monotonic 的意思是:

2: having the property either of never increasing or of never decreasing as the values of the independent variable or the subscripts of the terms increase.

2:随着自变量的值或项的下标的增加,具有永不增加或永不减少的性质。

In other words, it won't jump backwards. I can see that this would be an important property if you were timing some code.

换句话说,它不会向后跳。我可以看到,如果您正在计时一些代码,这将是一个重要的属性。

However, the difference between the normal and raw version isn't clear. Can someone shed some light into how NTP can still affect CLOCK_MONOTONIC?

但是,普通版本和原始版本之间的区别尚不清楚。有人可以解释一下 NTP 如何仍然影响 CLOCK_MONOTONIC 吗?

采纳答案by Ben Hymanson

CLOCK_MONOTONICnever experiences discontinuities due to NTP time adjustments, but it doeschange in frequency as NTP learns what error exists between the local oscillator and the upstream servers.

CLOCK_MONOTONIC不会因 NTP 时间调整而出现中断,但随着 NTP 了解本地振​​荡器和上游服务器之间存在什么错误,它的频率确实会发生变化。

CLOCK_MONOTONIC_RAWis simply the local oscillator, not disciplined by NTP. This could be very useful if you want to implement some other time synchronization algorithm against a clock which is not fighting you due to NTP. While ntpd (the reference implementation of NTP protocol and the most widespread NTP daemon) is reputed to be "gentle" with time adjustments, it's more accurate to say it's gentle with the absolute time. It's willing to slew the clock by 500ppm which is pretty dramatic if you're in a position to measure your clock frequency against some other standard.

CLOCK_MONOTONIC_RAW只是本地振荡器,不受 NTP 约束。如果您想针对由于 NTP 而不会与您争斗的时钟实施其他时间同步算法,这可能非常有用。虽然 ntpd(NTP 协议的参考实现和最广泛的 NTP 守护进程)被认为对时间调整“温和”,但更准确地说,它对绝对时间很温和。它愿意将时钟摆动 500 ppm,如果您能够根据其他标准测量时钟频率,这将非常引人注目。

The utility of CLOCK_MONOTONIC_RAWis going to be limited until facilities like pthread_timedwait_monotonicoffer an option to use that timebase.

的效用CLOCK_MONOTONIC_RAW将受到限制,直到诸如pthread_timedwait_monotonic提供使用该时基的选项的设施。

回答by Anton Kovalenko

ntpddoesn't cause the time to jumpif the difference is below a certain threshold. adjtime-like adjustment is used instead, affecting both CLOCK_MONOTONICand CLOCK_REALTIME(but not CLOCK_MONOTONIC_RAW, apparently).

ntpd如果差异低于某个阈值,则不会导致时间跳跃。改为使用类似 adjtime 的调整,同时影响CLOCK_MONOTONICCLOCK_REALTIME(但CLOCK_MONOTONIC_RAW显然不影响)。