-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
66 lines (58 loc) · 1.33 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "util.hpp"
/* Measure the time it takes to access a block with virtual address addr. */
CYCLES measure_one_block_access_time(ADDR_PTR addr)
{
CYCLES cycles;
asm volatile("mov %1, %%r8\n\t"
"lfence\n\t"
"rdtsc\n\t"
"mov %%eax, %%edi\n\t"
"mov (%%r8), %%r8\n\t"
"lfence\n\t"
"rdtsc\n\t"
"sub %%edi, %%eax\n\t"
: "=a"(cycles) /*output*/
: "r"(addr)
: "r8", "edi");
return cycles;
}
CYCLES get_highres_time(){
volatile uint32_t time_lo;
volatile uint32_t time_hi;
asm volatile(
"rdtsc\n"
: "=a"(time_lo),"=d"(time_hi));
CYCLES time = (uint64_t)time_lo | (( (uint64_t) time_hi) <<32);
// printf("GOT TIME %Ld\n",time);
return time;
}
// probe straight from paper
CYCLES probe_one_block(ADDR_PTR adrs) {
CYCLES time;
asm __volatile__ (
" mfence \n"
" lfence \n"
" rdtsc \n"
" lfence \n"
" movl %%eax, %%esi \n"
" movl (%1), %%eax \n"
" lfence \n"
" rdtsc \n"
" subl %%esi, %%eax \n"
" clflush 0(%1) \n"
: "=a" (time)
: "c" (adrs)
: "%esi", "%edx");
return time;
}
void wait_for_time(CYCLES time){
CYCLES start_t = get_highres_time();
while(get_highres_time() - start_t < time){
}
}
void flush_one_block(ADDR_PTR addr){
asm volatile(
"clflush 0(%0)"
:
: "r"(addr));
}