You can try to use the tsc register from the core is executing your code, whenever there's a cache miss you'll get a significantly higher access time so you can infer a cache miss happened.
unsigned long long int rdtsc(void){
unsigned long long int x;
unsigned a, d;
__asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
return ((unsigned long long)a) | (((unsigned long long)d) << 32);;
}
So you might want to read tsc register before and after you execute the instruction you want to measure access to cache, like the following graph.
You can check the image on the following link to give you an idea on how this works.
https://adriancolyer.files.wordpress.com/2018/01/meltdown-fig-4.jpeg?w=480
long long int start_time = 0;
long long int total_time = 0;
for (i = 0; i < N-1; i++){
start_time = rdtsc();
a[i] = (a[i] + a[i+1])/2;
total_time = rdtsc() - start_time;
}
Hope this helps you.