如何检测 RDTSC 是否返回恒定速率计数器值?

发布于 2024-10-08 16:27:53 字数 202 浏览 3 评论 0 原文

AMD 和 Intel 的大多数新型 CPU 似乎都将 rdtsc 实现为恒定速率计数器,从而避免了由于 TurboBoost 或省电设置等原因而导致频率变化所引起的问题。

由于 rdtsc 比 QueryPerformanceCounter 更适合性能测量,因为它的开销要低得多,因此我希望尽可能使用它。

如何可靠地检测 rdtsc 是否是恒定速率计数器?

It seems most newer CPUs from both AMD and Intel implement rdtsc as a constant rate counter, avoiding the issues caused by frequency changing as a result of things like TurboBoost or power saving settings.

As rdtsc is a lot more suitable for performance measurements than QueryPerformanceCounter because of its much lower overhead, I would like to use it whenever possible.

How can I detect reliably if the rdtsc is a constant rate counter or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-10-15 16:27:53

你可以使用CPUID来告诉你。来自 CPUID Fn8000_0007_EDX 位 8 的文档

TscInvariant:TSC 不变。确保 TSC 速率在所有 P 状态、C 状态和停止授权转换(例如 STPCLK 节流)中保持不变;因此 TSC 适合用作时间源。 0 = 不做出此类保证,软件应避免尝试使用 TSC 作为时间源。

You can use CPUID to tell you. From the docs on CPUID Fn8000_0007_EDX bit 8:

TscInvariant: TSC invariant. The TSC rate is ensured to be invariant across all P-States, C-States, and stop grant transitions (such as STPCLK Throttling); therefore the TSC is suitable for use as a source of time. 0 = No such guarantee is made and software should avoid attempting to use the TSC as a source of time.

强者自强 2024-10-15 16:27:53

我知道距离提出最初的问题已有很长时间了,但我只能指出,检查处理器的代/型号绝对是错误的做法。首先,代码很容易出错,从而无法在下一代处理器上运行(因为系列/型号并不总是“线性”),其次,仅仅因为处理器是“较晚的处理器”,系列/型号”而不是您知道此功能适用的产品,这并不能保证该功能存在。很有可能,但我已经看到很多代码做得很糟糕,因此“新处理器问世,代码出错”。

使用 CPUID 位检查处理器是否具有正确的位。

I know it's a long time since the original question was asked, but can I just point out that checking the generation/model of the processor is absolutely the WRONG thing to do. First of all, it's very easy to get the code wrong so that it doesn't work on future generation processors (because the family/model numbers aren't always "linear"), and secondly, just because a processor is "a later family/model" than the ones you knew this works on, it's not a guarantee that the feature is there. It's LIKELY, but I've seen plenty of code that does this badly, and thus "new processor comes out, and the code gets things wrong".

Use the CPUID bit to check if the processor has the correct bit or not.

断舍离 2024-10-15 16:27:53

只需使用CPUID来检测CPU代数,看看它是否使用常量计数器。不过,我建议使用分析 API,例如 AMD 的 codeanalyst sdk 会做得很好

just use CPUID to detect the generation of the CPU, see if its using constant counters. I'd however, suggest using a profiling API instead though, something like AMD's codeanalyst sdk would do well

无言温柔 2024-10-15 16:27:53

我确实计算了一秒钟内的滴答数,然后与 /proc/cpuinfo 中通知的时钟进行比较。它仅适用于禁用动态时钟的情况。查看来源:https://github.com/petersenna/rdtscbench

I do count the number o ticks in a second and then compare to the informed clock at /proc/cpuinfo. It only works with dynamic clock disabled. See the source: https://github.com/petersenna/rdtscbench

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文