27

伙计们,在我的应用程序中,我使用clock_gettime(CLOCK_MONOTONIC)它来测量帧之间的增量时间(gamedev 中的一种典型方法),并且有时我会遇到一种奇怪的行为clock_gettime(..)- 返回值偶尔不是单调的(即之前的时间大于当前时间)。

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

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

4

5 回答 5

23

man clock_gettime说:

CLOCK_MONOTONIC_RAW(从 Linux 2.6.28 开始;特定于 Linux)

与 CLOCK_MONOTONIC 类似,但提供对不受 NTP 调整的基于硬件的原始时间的访问。

由于CLOCK_MONOTONIC_RAW不是 NTP 调整的主题,我想CLOCK_MONOTONIC可能是。

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

于 2010-09-07T09:23:30.217 回答
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

请参阅此处获取补丁。这包含在 2.6.32.19 中,但可能没有被 Debian 团队向后移植(?)。你应该检查一下。

于 2010-09-07T09:29:49.150 回答
6

试试CLOCK_MONOTONIC_RAW

于 2010-09-07T09:15:27.030 回答
4

对我来说当然听起来像是一个错误。也许你应该在Ubuntu 的 bug tracker中报告它。

于 2010-09-07T09:16:05.423 回答
0

It's a linux bug. No ajustment in a monotonic clock can make it go backwards. You're using a very old kernel and a very old distribution.

Edit: are you sure you need to skip the frame ? If you call clock_gettime again, what happens ?

于 2013-03-16T15:24:38.537 回答