From 423f9c44ebcfb67e47ce30bd8e21ab2b69b01b6d Mon Sep 17 00:00:00 2001 From: bthom Date: Thu, 28 Nov 2024 10:46:42 +0100 Subject: [PATCH] Avoid using OS to destroy predictor --- run.sh | 2 +- serializeOff.c | 111 ++++++++++++++++++++++++++++++++++++++++++------- stats.py | 1 + 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/run.sh b/run.sh index 6ad411f..9f7d8a8 100755 --- a/run.sh +++ b/run.sh @@ -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 diff --git a/serializeOff.c b/serializeOff.c index 3038fbd..e4b130c 100644 --- a/serializeOff.c +++ b/serializeOff.c @@ -5,7 +5,7 @@ #include #include -#define N 2 +#define N 1000 #define M 100 #include @@ -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) : @@ -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; @@ -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; } diff --git a/stats.py b/stats.py index 43fdb9c..de1808b 100644 --- a/stats.py +++ b/stats.py @@ -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)