Skip to content

Instantly share code, notes, and snippets.

@bradley-evans
Created February 3, 2020 20:02
Show Gist options
  • Save bradley-evans/14538ded615ec7b0f9b1a0dda61fe9bf to your computer and use it in GitHub Desktop.
Save bradley-evans/14538ded615ec7b0f9b1a0dda61fe9bf to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define CACHE_MISS 0x100
int loop(int n, int a) {
int b = 0;
for(int i=0; i<n; i++) {
b = a + b;
}
return b;
}
int main() {
int a = 1;
int n = 200;
int count = 0;
printf("Configuring event monitor ...\n");
/*
// initialize counters here
// should start tracking cache misses with 0x100
__asm__ volatile("csrw mhpmevent3, %0"
:
: "r"(CACHE_MISS)
);
*/
printf("Executing loop ...\n");
loop(n, a);
printf("Reading event counter ...\n");
// read counters here
__asm__ volatile("csrr %0, hpmcounter3"
: "=r"(count)
);
printf("Cache misses: %d\n", count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment