关于使用CPU时钟测量时间的问题 [英] Questions on Measuring Time Using the CPU Clock

查看:169
本文介绍了关于使用CPU时钟测量时间的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在各种操作系统上使用CPU时钟计数器获取时间增量的标准方法.我的问题是,出于节能目的,这样的操作系统如何考虑CPU频率的变化.最初,我认为这可以基于以下事实来解释:操作系统使用特定的调用来测量频率,以根据使用的内核,当前设置的频率等获得校正的频率,但是后来我意识到,这不是事实如果在两个时钟查询之间将CPU频率降低并提高回原始值,则使时间增量不准确.

I'm aware of the standard methods of getting time deltas using CPU clock counters on various operating systems. My question is, how do such operating systems account for the change in CPU frequency for power saving purposes. I initially thought this could be explained based on the fact that OS's use specific calls to measure frequency to get the corrected frequency based on which core is being used, what frequency it's currently set to, etc. But then I realized, wouldn't that make any time delta inaccurate if the CPU frequency was lowered and raised back to it's original value in between two clock queries.

例如,采取以下情形:

查询CPU周期.操作系统降低CPU频率以节省功率.其他一些代码在这里运行.操作系统提高了CPU频率以提高性能.查询CPU周期.计算增量为周期差除以频率.

Query the CPU cycles. Operating system lowers CPU frequency for power saving. Some other code is run here. Operating system raises CPU frequency for performance. Query the CPU cycles. Calculate delta as cycle difference divided by frequency.

这将产生不正确的增量,因为两个查询之间的CPU频率不是恒定的.必须使用CPU周期来处理时间增量的操作系统或程序如何解决此问题?

This would yield an inaccurate delta since the CPU frequency was not constant between the two queries. How is this worked around by the operating system or programs that have to work with time deltas using CPU cycles?

推荐答案

请参阅此使用rdtsc进行错误的时钟周期测量

还有更多的处理方式

  1. 将CPU时钟设置为最大

阅读上面的链接以了解操作方法吗?

read the link above to see how to do it?

使用PIT代替RDTSC

PIT 是可编程中断计时器(如果我没记错的话,是Intel 8253),自x286以来(甚至可能更早),它存在于所有PC主板上,但分辨率仅为〜119KHz,并非所有操作系统都能您可以访问它.

PIT is programmable interrupt timer (Intel 8253 if I remember correctly) it is present on all PC motherboards since x286 (and maybe even before) but the resolution is only ~119KHz and not all OS give you access to it.

组合PIT和RDTSC

在足够稳定的情况下,通过 PIT 反复测量 CPU 时钟即可开始测量(并继续扫描 CPU 时钟变化).如果在测量期间 CPU 时钟发生变化,则放弃测量并重新开始

just measure the CPU clock by PIT repeatedly when is stable enough start your measurement (and remain scanning for CPU clock change). If CPU clock changes during measurement then throw away the measurement and start again

这篇关于关于使用CPU时钟测量时间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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