Skip to content

Commit

Permalink
Avoid using OS to destroy predictor
Browse files Browse the repository at this point in the history
  • Loading branch information
threonorm committed Nov 28, 2024
1 parent 0791691 commit 423f9c4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ objdump -D $1 > $1.dump

rm log$1
touch log$1
for i in {1..100}; do
for i in {1..1}; do
./$1 >> log$1
done
cat log$1 | python stats.py
111 changes: 96 additions & 15 deletions serializeOff.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <sys/syscall.h>
#include <unistd.h>

#define N 2
#define N 1000
#define M 100
#include <time.h>

Expand All @@ -23,11 +23,11 @@ int __attribute__ ((noinline)) withspecKind1(int* a,volatile int** c){
int v;
if (**c) {
__asm__ volatile(
"serialize\n"
// "lfence\n"
// "rdtscp\n"
// "addq %%rax, %%rdi\n"
// "subq %%rax, %%rdi\n"
// "serialize\n"
// "lfence\n"
"rdtscp\n"
"addq %%rax, %%rdi\n"
"subq %%rax, %%rdi\n"
"movq (%%rdi), %%rax\n"
: "=a" (v)
:
Expand Down Expand Up @@ -59,6 +59,85 @@ int __attribute__ ((noinline)) withspecKind2(int* a,volatile int** c){
}
#pragma GCC pop_options

void test_withspecKind3() {
uint64_t start=0;
uint64_t end=0;
uint64_t total_elapsed=0;
uint64_t mask = ((uint64_t) &cc1) ^ ((uint64_t) &cc2);
uint64_t lastime = 0;
uint64_t acc;

// Train the branch predictor
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Remove everything of interest from the cache
__asm__ volatile (
"clflush (%0)\n"
"clflush (%1)\n"
"clflush (%2)\n"
"clflush (%3)\n"
"clflush (%4)\n"
"lfence\n"
"sfence\n"
"mfence\n"
"serialize\n" :: "r"(&a),"r"(&c1),"r"(&c2),"r"(&cc2),"r"(&cc1)
);


lastime = (j == M-1)*(-1);
acc = (mask & lastime) ^ ((uint64_t) &cc1);
withspecKind1(&a, (int**) acc);
// Start timer
__asm__ volatile (
"serialize\n"
"lfence\n"
"sfence\n"
"mfence\n"
"rdtscp\n"
: "=A" (start)
);
// Load a
__asm__ volatile (
"mov (%0), %%rdx"
:
: "r" (&a)
: "rdx"
);
// Stop timer
__asm__ volatile (
"lfence\n"
"rdtscp\n"
: "=A" (end)
);
total_elapsed += (lastime & (end-start));


}
__asm__ volatile (
"clflush (%0)\n"
"clflush (%1)\n"
"clflush (%2)\n"
"clflush (%3)\n"
"clflush (%4)\n"
"lfence\n"
"sfence\n"
"mfence\n"
"serialize\n" :: "r"(&a),"r"(&c1),"r"(&c2),"r"(&cc2),"r"(&cc1)
);


printf("{ \"kind1\": ");
printf(" %llu ", total_elapsed);
printf(", \"kind2\": 0");
printf(", \"refInCache\": ");
ref(1);
printf(", \"refOutCache\": ");
ref(0);
printf("}\n");
total_elapsed = 0;
}
}

void test_withspecKind1() {
uint64_t start=0;
uint64_t end=0;
Expand Down Expand Up @@ -246,14 +325,16 @@ void ref(int incache){


int main() {
printf("{ \"kind1\": ");
test_withspecKind1();
printf(", \"kind2\": ");
test_withspecKind2();
printf(", \"refInCache\": ");
ref(1);
printf(", \"refOutCache\": ");
ref(0);
printf("}\n");
// printf("{ \"kind3\": ");
test_withspecKind3();
// printf(", \"kind2\": ");
// test_withspecKind2();
// printf(", \"kind3\": ");
// test_withspecKind3();
// printf(", \"refInCache\": ");
// ref(1);
// printf(", \"refOutCache\": ");
// ref(0);
// printf("}\n");
return 0;
}
1 change: 1 addition & 0 deletions stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def read_json_lines():
print(f"Error: {e}")
return data, filtered_counts


def create_histograms(data, filtered_counts):
"""Create histograms for each field in the data using the same bins."""
n_fields = len(data)
Expand Down

0 comments on commit 423f9c4

Please sign in to comment.