OSX上的单调时钟 [英] Monotonic clock on OSX

查看:112
本文介绍了OSX上的单调时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CLOCK_MONOTONIC似乎不可用,所以clock_gettime已经用完.

CLOCK_MONOTONIC does not seem available, so clock_gettime is out.

我在某些地方读到mach_absolute_time()可能是正确的方法,但是在读到它是一个取决于cpu的值"后,立即让我想知道它是否在下面使用rtdsc.因此,该值可能随时间漂移,即使它是单调的.同样,线程亲和性问题可能导致与调用函数有显着不同的结果(使其在所有内核中都不单调).

I've read in some places that mach_absolute_time() might be the right way to go, but after reading that it was a 'cpu dependent value', it instantly made me wonder if it is using rtdsc underneath. Thus, the value could drift over time even if it is monotonic. Also, issues with thread affinity could result in meaningfully different results from calling the function (making it not monotonic across all cores).

当然,这只是猜测.有人知道mach_absolute_time实际如何工作吗?我实际上正在寻找替代clock_gettime(CLOCK_MONOTONIC ...或OSX的类似东西.无论时钟源是什么,我都希望至少毫秒级的精度和毫秒级的精度.

Of course, that is just speculation. Does anyone know how mach_absolute_time actually works? I'm actually looking for a replacement to clock_gettime(CLOCK_MONOTONIC... or something like it for OSX. No matter what the clock source is, I expect at least millisecond precision and millisecond accuracy.

我想了解哪些时钟可用,哪些时钟是单调的,如果某些时钟漂移,存在线程亲缘性问题,并非所有Mac硬件都支持或采取超高"数量的cpu周期执行.

I'd just like to understand what clocks are available, which clocks are monotonic, if certain clocks drift, have thread affinity issues, aren't supported on all Mac hardware, or take a 'super high' number of cpu cycles to execute.

以下是我可以找到的有关此主题的链接(有些链接已经失效,无法在archive.org上找到):

Here are the links I was able to find about this topic (some are already dead links and not findable on archive.org):

https://developer.apple.com/library/mac/#qa/qa1398 /_index.html http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/ http://www.meandmark.com/timing.pdf

https://developer.apple.com/library/mac/#qa/qa1398/_index.html http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/ http://www.meandmark.com/timing.pdf

谢谢! 布雷特(Brett)

Thanks! Brett

推荐答案

Mach内核提供对系统时钟的访问,其中至少一个(SYSTEM_CLOCK)是

The Mach kernel provides access to system clocks, out of which at least one (SYSTEM_CLOCK) is advertised by the documentation as being monotonically incrementing.

#include <mach/clock.h>
#include <mach/mach.h>

clock_serv_t cclock;
mach_timespec_t mts;

host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);

mach_timespec_t具有纳秒精度.不过,我不确定准确度.

mach_timespec_t has nanosecond precision. I'm not sure about the accuracy, though.

Mac OS X支持三个时钟:

Mac OS X supports three clocks:

  • SYSTEM_CLOCK返回自启动时间以来的时间;
  • CALENDAR_CLOCK返回从1970-01-01开始的UTC时间;
  • REALTIME_CLOCK已过时,并且在其当前实现中与SYSTEM_CLOCK相同.
  • SYSTEM_CLOCK returns the time since boot time;
  • CALENDAR_CLOCK returns the UTC time since 1970-01-01;
  • REALTIME_CLOCK is deprecated and is the same as SYSTEM_CLOCK in its current implementation.

clock_get_time 文档除非有人调用clock_set_time,否则它们是单调递增的.呼叫clock_set_time不利的,因为它可能会破坏时钟的单调属性,实际上是当前实现返回 不执行任何操作.

The documentation for clock_get_time says the clocks are monotonically incrementing unless someone calls clock_set_time. Calls to clock_set_time are discouraged as it could break the monotonic property of the clocks, and in fact, the current implementation returns KERN_FAILURE without doing anything.

这篇关于OSX上的单调时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆