我正在尝试计算 CPUID 指令运行所需的时间。
来源:
#include <stdio.h>
#include <sched.h>
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
static inline unsigned long long tick()
{
unsigned long long d;
asm volatile ("rdtsc" : "=A" (d));
return d;
}
void cpuid(void)
{
int i;
for(i=0; i != 5; i++)
{
asm volatile ("cpuid");
}
}
int main()
{
long long bef;
long long aft;
long long dif;
bef=tick();
cpuid();
aft=tick();
dif=aft-bef;
printf("%d\n", bef);
printf("%d\n", aft);
printF("%d\n", dif);
return 0;
}
现在我正在使用以下内容进行编译
gcc -D_GNU_SOURCE -o test test.c
我在不是文件的代码上遇到错误! 例如:
test.c:6:1: error: expected identifier or '(' before 'do'
test.c:6:1: error: expected identifier or '(' before 'while'
test.c:7:1: error: expected identifier or '(' before '__extension__'
test.c:8:1: warning: data definition has no type or storage class [enable by def...
test.c:8:1: error: intializer element is not constant
“def...”实际上并不是它的输出,因为我的终端窗口很小。我在 ESXi 中工作。
任何帮助都会很棒!!!
对于 future 的读者
用户@Iwillnotexist Idonotexist 说使用以下函数以获得完整的 x86 和 x64 支持是正确的。
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
最佳答案
您有函数外的代码。那是不允许的。将以下内容移至 main
:
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
关于c - 在 C 中设置 CPU 亲和性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23203322/