为什么rdtscp不返回核心id



我有这样的代码

#include <iostream>
#include <sched.h>
unsigned long long rdtscp(unsigned int* aux)
{
    // For IA32
    unsigned long long x;
    asm volatile("rdtscp" : "=c" (*aux), "=A" (x) ::);
    return x;
}
int main()
{
  unsigned int  aux0, aux1 = -1, aux2;
  aux0 = sched_getcpu();
  unsigned long long x = rdtscp(&aux1);
  aux2 = sched_getcpu();
  std::cout << "aux0 = " << aux0 << ", aux1 = " << aux1 << ", aux2 = " << aux2 << ", x = " << x << std::endl;
  return 0;
}

定义了一个函数rdtscp,它使用内联汇编调用rdtscp汇编指令。TSC值看起来很好,但是核心id始终为零,即使sched_getcpu()都返回了正确的核心id并且它不是零。输出看起来像

# g++ test.cpp -o test ; ./test
aux0 = 2, aux1 = 0, aux2 = 2, x = 231368797247511

如何使用rdtscp获得TSC和核心id?

可能您的硬件不支持rdtscp指令。你可以检查这个:

#include <cpuid.h>
#include <stdio.h>
main()
{
    unsigned a, b, c, d;
    if (__get_cpuid(0x80000001, &a, &b, &c, &d))
        puts("unsupported instruction rdtscp" + (d&1<<27 ? 2 : 0));
    else
        puts("unsupported cpuid level");
}

相关内容

  • 没有找到相关文章

最新更新