首页 > 解决方案 > 使用 rdtsc 对英特尔进行汇编程序基准测试给出了奇怪的答案,为什么?

问题描述

前段时间,我问了一个关于堆栈溢出的问题,并展示了如何在 C++ 中执行 rdtsc 操作码。我最近使用 rdtsc 创建了一个基准函数,如下所示:

inline unsigned long long rdtsc() {
  unsigned int lo, hi;
  asm volatile (
     "cpuid \n"
     "rdtsc" 
   : "=a"(lo), "=d"(hi) /* outputs */
   : "a"(0)             /* inputs */
   : "%ebx", "%ecx");     /* clobbers*/
  return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
}

typedef uint64_t (*FuncOneInt)(uint32_t n);
/**
     time a function that takes an integer parameter and returns a 64 bit number
     Since this is capable of timing in clock cycles, we won't have to do it a
     huge number of times and divide, we can literally count clocks.
     Don't forget that everything takes time including getting into and out of the
     function.  You may want to time an empty function.  The time to do the computation
     can be compute by taking the time of the function you want minus the empty one.
 */
void clockBench(const char* msg, uint32_t n, FuncOneInt f) {
    uint64_t t0 = rdtsc();
    uint64_t r = f(n);
    uint64_t t1 = rdtsc();
    std::cout << msg << "n=" << n << "\telapsed=" << (t1-t0) << '\n';
}

因此,我假设如果我对一个函数进行基准测试,我将(大致)拥有它执行所需的时钟周期数。我还假设如果我想减去进入或退出函数所需的时钟周期数,我应该对一个空函数进行基准测试,然后在里面编写一个包含所需代码的函数。

这是一个示例:

uint64_t empty(uint32_t n) {
    return 0;
}

uint64_t sum1Ton(uint32_t n) {
    uint64_t s = 0;
    for (int i = 1; i <= n; i++)
        s += i;
    return s;
}

代码是使用编译的

g++ -g -O2

我可以理解是否由于中断或其他条件而出现错误,但鉴于这些例程很短,并且 n 被选择得很小,我假设我可以看到实数。但令我惊讶的是,这是连续两次运行的输出

empty n=100 elapsed=438
Sum 1 to n=100  elapsed=887

empty n=100 elapsed=357
Sum 1 to n=100  elapsed=347

始终如一的空函数表明它需要的方式比它应该的要多。

毕竟,进出函数只涉及几条指令。真正的工作是在循环中完成的。不要介意差异巨大的事实。在第二次运行中,空函数声称需要 357 个时钟周期,而总和需要更少,这很荒谬。

怎么了?

标签: assemblyx86intelmicrobenchmarkrdtsc

解决方案


始终如一的空函数表明它需要的方式比它应该的要多。

你有cpuid时间间隔内cpuid根据 Agner Fog 的测试,在 Intel Sandybridge 系列 CPU 上需要 100 到 250 个核心时钟周期(取决于您忽略设置的输入)。(https://agner.org/optimize/)。

但是您不是在测量核心时钟周期,而是在测量 RDTSC 参考周期,这可能要短得多。(例如,我的 Skylake i7-6700k 在 800MHz 时空闲,但参考时钟频率为 4008MHz。)请参阅获取 CPU 周期数?因为我尝试在rdtsc.

首先预热 CPU,或者pause在另一个内核上运行一个繁忙的循环以使其保持在最大值(假设它是台式机/笔记本电脑双核或四核,所有核心频率都锁定在一起。)


不要介意差异巨大的事实。在第二次运行中,空函数声称需要 357 个时钟周期,而总和需要更少,这很荒谬。

效果也是一致的吗?

也许您的 CPU 在打印第三行消息期间/之后加速到全速,从而使最后一个定时区域运行得更快?(为什么这个延迟循环在没有睡眠的几次迭代后开始运行得更快?)。

IDK 之前 eax 和 ecx 中不同的垃圾会有多大的影响cpuid。将其替换为lfence以消除它并使用低得多的开销方式来序列化rdtsc.


推荐阅读