0
你能告訴我如何使用clflush()指令嗎?我已經編寫了下面的簡單代碼來測量從緩存中讀取變量的執行時間和從緩存中逐出後的執行時間之間的差異。但是我沒有找到確鑿的結果。什麼是使用clflush()驅逐緩存的正確方法?CLFLUSH()的問題?
#include <stdio.h>
#include <stdint.h>
#include"cpucycles.c"
#define REPEAT 1900000
inline void clflush(volatile void *p)
{
asm volatile ("clflush (%0)" :: "r"(p));
}
inline uint64_t rdtsc()
{
unsigned long a, d;
asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");
return a | ((uint64_t)d << 32);
}
volatile int i;
inline void test()
{
uint64_t start, end,clock;
volatile int j;
long int rep;
int k;
clock=0;
for(rep=0;rep<REPEAT;rep++){
start = rdtsc();
j = i+1;
end = rdtsc();
clock=clock+(end-start);
k=j;
}
printf("took %lu ticks\n", clock);
}
inline void testflush()
{
uint64_t start, end,clock;
volatile int j;
int k;
long int rep;
clock=0;
for(rep=0;rep<REPEAT;rep++){
start = rdtsc();
j = i+1;
end = rdtsc();
clflush(&i);
clock=clock+(end-start);
k=j;
}
printf("took %lu ticks\n", clock);
}
int main(int ac, char **av)
{
i=5;
printf("------------------------------------------\n");
test();
printf("------------------------------------------\n");
testflush();
printf("------------------------------------------\n");
test();
return 0;
}
[如何使用clflush?]的可能重複(http://stackoverflow.com/questions/39448276/how-to-use-clflush) – Olaf