Linux下的rdtsc定时器不准确吗? [英] Is rdtsc timer inaccurate in linux?

查看:822
本文介绍了Linux下的rdtsc定时器不准确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 __inline__ uint64_t rdtsc() {
    uint32_t low, high;
    __asm__ __volatile__ (
        "xorl %%eax,%%eax \n    cpuid"
        ::: "%rax", "%rbx", "%rcx", "%rdx" );
    __asm__ __volatile__ (
                          "rdtsc" : "=a" (low), "=d" (high));
    return (uint64_t)high << 32 | low;
}



我在程序中使用了上面的rdtsc函数作为计时器:
以下代码导致312-344个时钟周期:

I have used the above rdtsc function as a timer in my program: The following code results in 312-344 clock cycles:

 start = rdtsc();
 stop = rdtsc();

 elapsed_ticks = (unsigned)((stop-start));
 printf("\n%u ticks\n",elapsed_ticks);

每次运行上面的代码时,我得到不同的值。这是为什么?

every time I run the above code I get different values. Why is that?

我在Visual C ++中运行相同的代码,在intrin.h中使用了rdtsc函数。我得到一个恒定的价值18时钟。是的,它是不断的每一个运行!有人可以解释吗?谢谢!

I ran the same code in Visual C++ which uses an rdtsc function in "intrin.h". I was getting a constant value of 18 clocks.Yes, it was constant on every run! Can someone please explain? Thanks!

推荐答案

使用TSC获取可靠的时间戳非常困难。主要的问题是:

It's quite difficult to get reliable timestamps using the TSC. The main problems are:


  • 在旧的多核处理器上,不同内核的速率会有不同的变化,到不同的负载;

  • 在更新的处理器上,速率保持不变,而时钟速度变化,因此轻负载内核的时序可能看起来比它们慢。

  • 无序执行可能意味着当您认为寄存器未被读取。

您的函数正在执行 cpuid 指令并忽略其结果,以及读取TSC,以尝试减轻上一个问题。这是一个序列化指令,它强制按顺序执行。但是,它也是一个慢的指令,所以如果你尝试测量一个非常短的时间,将影响结果。

Your function is executing the cpuid instruction and ignoring its result, as well as reading the TSC, to try to mitigate the last issue. That's a serialising instruction, which forces in-order execution. However, it's also rather a slow instruction, so will affect the result if you try to measure an extremely short time.

如果我从函数中删除该指令,相当于你在VC ++中使用的内在函数:

If I remove that instruction from the function to make it equivalent to the intrinsic you're using in VC++:

inline uint64_t rdtsc() {
    uint32_t low, high;
    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
    return (uint64_t)high << 32 | low;
}

那么我得到更一致的值,但重新引入潜在的指令排序问题。

then I get more consistent values, but reintroduce the potential instruction-ordering issue.

此外,确保使用优化进行编译(例如,如果使用GCC,则 -O3 否则该函数可能不会内联。

Also, make sure you're compiling with optimisation (e.g. -O3 if you're using GCC), otherwise the function may not be inlined.

这篇关于Linux下的rdtsc定时器不准确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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