-
Notifications
You must be signed in to change notification settings - Fork 63
/
cacheutils.h
111 lines (90 loc) · 2.17 KB
/
cacheutils.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef CACHEUTILS_H
#define CACHEUTILS_H
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#ifndef HIDEMINMAX
#define MAX(X,Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X,Y) (((X) < (Y)) ? (X) : (Y))
#endif
uint64_t rdtsc_nofence() {
uint64_t a, d;
asm volatile ("rdtsc" : "=a" (a), "=d" (d));
a = (d<<32) | a;
return a;
}
uint64_t rdtsc() {
uint64_t a, d;
asm volatile ("mfence");
asm volatile ("rdtsc" : "=a" (a), "=d" (d));
a = (d<<32) | a;
asm volatile ("mfence");
return a;
}
void maccess(void* p)
{
asm volatile ("movq (%0), %%rax\n"
:
: "c" (p)
: "rax");
}
void flush(void* p) {
asm volatile ("clflush 0(%0)\n"
:
: "c" (p)
: "rax");
}
void prefetch(void* p)
{
asm volatile ("prefetcht1 %0" : : "m" (p));
}
void longnop()
{
asm volatile ("nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n");
}
typedef struct map_handle_s {
int fd;
size_t range;
void* mapping;
} map_handle_t;
void* map_file(const char* filename, map_handle_t** handle) {
if (filename == NULL || handle == NULL) {
return NULL;
}
*handle = calloc(1, sizeof(map_handle_t));
if (*handle == NULL) {
return NULL;
}
(*handle)->fd = open(filename, O_RDONLY);
if ((*handle)->fd == -1) {
return NULL;
}
struct stat filestat;
if (fstat((*handle)->fd, &filestat) == -1) {
close((*handle)->fd);
return NULL;
}
(*handle)->range = filestat.st_size;
(*handle)->mapping = mmap(0, (*handle)->range, PROT_READ, MAP_SHARED, (*handle)->fd, 0);
return (*handle)->mapping;
}
void unmap_file(map_handle_t* handle) {
if (handle == NULL) {
return;
}
munmap(handle->mapping, handle->range);
close(handle->fd);
free(handle);
}
#endif