如何检测 RDTSC 是否返回恒定速率计数器值?
AMD 和 Intel 的大多数新型 CPU 似乎都将 rdtsc 实现为恒定速率计数器,从而避免了由于 TurboBoost 或省电设置等原因而导致频率变化所引起的问题。
由于 rdtsc 比 QueryPerformanceCounter 更适合性能测量,因为它的开销要低得多,因此我希望尽可能使用它。
如何可靠地检测 rdtsc 是否是恒定速率计数器?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以使用
CPUID
来告诉你。来自 CPUID Fn8000_0007_EDX 位 8 的文档:You can use
CPUID
to tell you. From the docs on CPUID Fn8000_0007_EDX bit 8:我知道距离提出最初的问题已有很长时间了,但我只能指出,检查处理器的代/型号绝对是错误的做法。首先,代码很容易出错,从而无法在下一代处理器上运行(因为系列/型号并不总是“线性”),其次,仅仅因为处理器是“较晚的处理器”,系列/型号”而不是您知道此功能适用的产品,这并不能保证该功能存在。很有可能,但我已经看到很多代码做得很糟糕,因此“新处理器问世,代码出错”。
使用 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.
只需使用
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我确实计算了一秒钟内的滴答数,然后与 /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