These instructions should be into main():
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
This should fix the compilation error.
Then, five iterations for cpuid()
are far too little to give meaningful results.
You can check this answer but using two sequences of different lengths made with only CPUID instructions. You need a longer cycle, but not so long that memory fetches enter into play.
I have ran some tests with TEST defined between 5 and 1000; CPU affinity did not seem to influence results on a quad-core:
#include <stdio.h>
#include <sched.h>
static inline unsigned long long tick() {
unsigned long long d;
asm volatile ("rdtsc" : "=A" (d));
return d;
}
static inline void cpuid(void) {
int i;
for(i=0; i != TEST; i++) {
asm volatile ("cpuid");
}
}
int main()
{
long long bef, aft, dif;
bef=tick();
cpuid();
aft=tick();
dif=(aft-bef)/TEST;
printf("%lld\n", dif);
return 0;
}
gcc -o0 -DTEST=100 -D_GNU_SOURCE -W -Wall -o time time.c && ./time