Panic的小屋

国破山河在,城春草木深。
随笔 - 152, 评论 - 1764, 引用 - 25, 文章 - 0

导航

公告

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿(336)

随笔分类

随笔档案

文章档案

相册

国外好站推荐

工具网页

我的其他网页

我的网友

户外运动

美女的空间

搜索

最新评论

阅读排行榜

评论排行榜

CPU时间戳rdtsc的问题

Posted on 2005-04-08 21:12 Panic 阅读(8894) 评论(8)  编辑 收藏
CPU时间戳rdtsc的问题
作者:Panic 2005年4月8日

首先从一段代码说起: Sleep指定机器周期数的代码
这个代码使用了一个指令:rdtsc
 //CPU时间戳,用来获取机器周期数。
inline unsigned __int64 GetCycleCount()
{
        __asm _emit 0x0F
        __asm _emit 0x31
}
本以为这个是只要支持的CPU都能有效运行的。没想到在笔记本上出了问题。
在笔记本上,这个指令的计算会无故出错。
为什么呢?不确切的解答是:
笔记本电脑用的CPU内置了低功耗模式,这一模式的方法一般是降低运行频率。
当CPU进入低功耗模式的时候,工作频率和CPU的标称频率就不相同了。于是所有基于这个指令的数值运算全部出错。
目前还没有对这个问题做深入研究,希望有兴趣、有能力或者已经知道的朋友给予解答,并探讨解决方案。

Feedback

# 还有我上次说的SMP的机器也会有问题,呵呵,这个东东在严肃的时候少用为妙

2005-04-08 22:29 by Diviner
rt

# re: CPU时间戳rdtsc的问题

2005-04-08 23:11 by 乾坤一笑
low-cost这些玩意儿是很头痛的,我最近也发现一个low-cost的问题。我的推测是:在维持功率不变的情况下,low-cost会上拉电压,从而电流急剧下降。至于会不会影响到频率,偶到没有测过。一般low-cost模式需要外加驱动电路的。(这里说的不是CPU的low-cost)

# re: CPU时间戳rdtsc的问题

2005-04-09 06:42 by Coder JOzu
rdtsc不过是个特定CPU支持的指令而已,和机器的低功耗没有什么关系,目前硬件的发展还没有到达根据供电多少实现一部分硬件功能的水平。目前这个指令只是用在intel x86架构兼容的CPU上,而且对于这个指令,还要根据CPU的主频来确定所耗时间,你要根据代码运行在什么系统上来决定是不是用这个指令,windows平台上有一个函数叫IsProcessorFeaturePresent用来确定是不是当前CPU支持这个指令,建议你调用GetCycleCount之前先确保CPU支持这个指令,在linux平台上可以用CPUID指令获得CPU是不是支持这个指令。

# 我翻了一下INTEL资料,好像没提到这个问题。

2005-04-09 08:57 by Diviner
RDTSC—Read Time-Stamp Counter
Description
This instruction loads the current value of the processor’s time-stamp counter into the
EDX:EAX registers. The time-stamp counter is contained in a 64-bit MSR. The high-order 32
bits of the MSR are loaded into the EDX register, and the low-order 32 bits are loaded into the
EAX register. The processor increments the time-stamp counter MSR every clock cycle and
resets it to 0 whenever the processor is reset.
The time stamp disable (TSD) flag in register CR4 restricts the use of the RDTSC instruction.
When the TSD flag is clear, the RDTSC instruction can be executed at any privilege level; when
the flag is set, the instruction can only be executed at privilege level 0. The time-stamp counter
can also be read with the RDMSR instruction, when executing at privilege level 0.
The RDTSC instruction is not a serializing instruction. Thus, it does not necessarily wait until
all previous instructions have been executed before reading the counter. Similarly, subsequent
instructions may begin execution before the read operation is performed.
This instruction was introduced into the Intel Architecture in the Pentium® processor.
Operation
IF (CR4.TSD = 0) OR ((CR4.TSD = 1) AND (CPL=0))
THEN
EDX:EAX ¬ TimeStampCounter;
ELSE (* CR4 is 1 and CPL is 1, 2, or 3 *)
#GP(0)
FI;
Flags Affected
None.
Protected Mode Exceptions
#GP(0) If the TSD flag in register CR4 is set and the CPL is greater than 0.
Real-Address Mode Exceptions
#GP If the TSD flag in register CR4 is set.
Virtual-8086 Mode Exceptions
#GP(0) If the TSD flag in register CR4 is set.

# re: CPU时间戳rdtsc的问题

2005-04-11 10:00 by 周星星
“没想到在笔记本上出了问题。”
--- 问题的表现是什么?

# to 七猫

2005-04-12 04:38 by Coder JOzu
可以参考intel手册第三卷的15.8

# re: CPU时间戳rdtsc的问题

2007-08-04 11:36 by windam
这个指令是不是用的时候应该考虑到线程切换?
是否应该关中断?= =
还是说操作系统在上下文切换的时候会保存这个指令用到的某个寄存器的值?

# re: CPU时间戳rdtsc的问题

2009-06-09 20:28 by iashao
CPU上的SpeedStep会影响TSC
(I)  TSC基于当前的处理器时钟,也就是如果处理器因为SpeedStep变慢了,那么TSC的数值代表的时间也随之加长了。Pentium M处理器 (family [06H], models [09H, 0DH]); Pentium 4以及志强处理器中(family [0FH], models [00H, 01H, or 02H]);和P6家族的处理器,TSC增加的频率都是会随着处理器的时钟变化而变化的
(II) TSC基于Marked frequency, 也就是默认的处理器时钟。在Pentium 4和志强处理器(family [0FH], models [03H and higher]); Core Solo and Core Duo处理器 (family [06H], model[0EH]); 志强处理器5100系列和Core 2 Duo处理器(family [06H], model [0FH]),TSC都不会随着SpeedStep的频率变化而变化。
标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]