Linux clock_gettime(CLOCK_MONOTONIC)奇怪的非单调行为

时间:2010-09-07 09:11:05

标签: linux gcc clock

伙计们,在我的应用程序中,我正在使用clock_gettime(CLOCK_MONOTONIC)来测量帧之间的增量时间(gamedev中的典型方法),并且我不时地遇到{{1}的奇怪行为} - 返回的值偶尔不是单调的(即上一个时间比当前时间更大)。

目前,如果发生这样的悖论,我只需跳过当前帧并开始处理下一帧。

问题是这怎么可能呢?这是clock_gettime(..)的Linux POSIX实现中的错误吗?我正在使用Ubuntu Server Edition 10.04(内核2.6.32-24,x86_64),gcc-4.4.3。

5 个答案:

答案 0 :(得分:22)

man clock_gettime说:

  

CLOCK_MONOTONIC_RAW (自Linux 2.6.28起;特定于Linux)

     

与CLOCK_MONOTONIC类似,但可以访问不受NTP调整影响的原始硬件时间。

由于CLOCK_MONOTONIC_RAW不是NTP调整的对象,我猜CLOCK_MONOTONIC可能是。

我们在使用2.6.18内核的Redhat Enterprise 5.0和一些特定的Itanium处理器时遇到了类似的问题。我们无法在同一操作系统上使用其他处理器重现它。它在RHEL 5.3中得到修复,内核稍微更新一些,还有一些Redhat补丁。

答案 1 :(得分:21)

看起来像是

的一个实例
commit 0696b711e4be45fa104c12329f617beb29c03f78
Author: Lin Ming <ming.m.lin@intel.com>
Date:   Tue Nov 17 13:49:50 2009 +0800

timekeeping: Fix clock_gettime vsyscall time warp

Since commit 0a544198 "timekeeping: Move NTP adjusted clock
multiplier to struct timekeeper" the clock multiplier of vsyscall is updated with
the unmodified clock multiplier of the clock source and not with the
NTP adjusted multiplier of the timekeeper.

This causes user space observerable time warps:
new CLOCK-warp maximum: 120 nsecs,  00000025c337c537 -> 00000025c337c4bf

有关修补程序,请参阅here。这包含在2.6.32.19中,但可能没有被Debian团队向后移植(?)。你应该看看它。

答案 2 :(得分:6)

尝试CLOCK_MONOTONIC_RAW

答案 3 :(得分:4)

当然听起来像是一个小虫。也许您应该在Ubuntu's bug tracker中报告。

答案 4 :(得分:-1)

这是一个linux错误。单调时钟中的任何调整都不能使其向后移动。你正在使用一个非常古老的内核和一个非常古老的发行版。

编辑:你确定你需要跳过框架吗?如果再次调用clock_gettime,会发生什么?

相关问题