使用rdtsc计算系统时间 [英] Calculate system time using rdtsc

查看:373
本文介绍了使用rdtsc计算系统时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定CPU中的所有内核具有相同的频率,从技术上讲,我可以每毫秒左右为每个内核同步系统时间和时间戳计数器对.然后基于当前运行的内核,我可以获取当前的rdtsc值,并使用滴答增量除以内核频率,从而能够估计自上次同步系统时间和时间戳以来经过的时间计数器对并推算当前系统时间,而不会从我当前线程中产生系统调用的开销(假设不需要锁即可检索上述数据). 从理论上讲,这很有效,但是在实践中,我发现有时候我会得到比我期望的更多的滴答声,也就是说,如果我的核心频率是1GHz,并且我在1毫秒前使用了系统时间和时间戳记计数器对,那么我期望看到一个增量在大约10 ^ 6的价格变动中,但实际上我发现它可以在10 ^ 6到10 ^ 7之间的任何地方. 我不确定发生了什么问题,任何人都可以分享他对如何使用rdtsc计算系统时间的想法吗?我的主要目标是避免每次我想知道系统时间时都需要执行系统调用,并且能够在用户空间中进行计算,这将使我对此有一个很好的估计(目前,我为此定义了一个很好的估计距实际系统时间间隔为10微秒.

Suppose all the cores in my CPU have same frequency, technically I can synchronize system time and time stamp counter pairs for each core every millisecond or so. Then based on the current core I'm running with, I can take the current rdtsc value and using the tick delta divided by the core frequency I'm able to estimate the time passed since I last synchronized the system time and time stamp counter pair and to deduce the current system time without the overhead of system call from my current thread (assuming no locks are needed to retrieve the above data). This works great in theory but in practice I found that sometimes I get more ticks then I would expect, that is, if my core frequency is 1GHz and I took system time and time stamp counter pair 1 millisecond ago I would expect to see a delta in the ticks which is around 10^6 ticks, but actually I found it can be anywhere between 10^6 and 10^7. I'm not sure what is wrong, can anyone share his thoughts on how to calculate system time using rdtsc? My main objective is to avoid the need to perform system call every time I want to know the system time and be able to perform a calculation in user space that will give my a good estimation of it (currently I define a good estimation as a result which is with in 10 micro seconds interval from the real system time.

推荐答案

不要那样做-直接使用自己

Don't do that -using yourself directly the RDTSC machine instruction- (because your OS scheduler could reschedule other threads or processes at arbitrary moments, or slow down the clock). Use a function provided by your library or OS.

我的主要目的是避免每次我想知道系统时间时都需要执行系统调用

My main objective is to avoid the need to perform system call every time I want to know the system time

在Linux上,阅读 time(7),然后使用 clock_gettime(2)确实非常快(并且不涉及任何系统调用),都要感谢

On Linux, read time(7) then use clock_gettime(2) which is really quick (and does not involve any slow system call) thanks to vdso(7).

在符合C ++ 11的实现中,只需使用标准的 <chrono>标头.而且标准C具有 clock(3)(精确到微秒) .两者都将在Linux上使用足够好的时间测量功能(因此间接 vdso)

On a C++11 compliant implementation, simply use the standard <chrono> header. And standard C has clock(3) (giving microsecond precision). Both would use on Linux good enough time measurement functions (so indirectly vdso)

我上次测量clock_gettime的时间通常少于每次通话4纳秒.

Last time I measured clock_gettime it often took less than 4 nanoseconds per call.

这篇关于使用rdtsc计算系统时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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