-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvthread_pallocator.hh
191 lines (156 loc) · 4.96 KB
/
kvthread_pallocator.hh
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Modification of Masstree
* VLSC Laboratory
* Copyright (c) 2018-2019 Ecole Polytechnique Federale de Lausanne
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#pragma once
#include "incll_configs.hh"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/stat.h>
#include "incll_extflush.hh"
#define nvm_free_addr ((void**)mmappedData)[0]
typedef uint64_t mrcu_epoch_type;
extern volatile mrcu_epoch_type globalepoch;
extern volatile mrcu_epoch_type failedepoch;
extern volatile mrcu_epoch_type currexec;
//nvm layout
//|thread 1 |thread 2 |
//|loc_size pool data|loc_size pool data|
class PDataAllocator{
private:
static constexpr const char *pdata_filename = PDATA_FILENAME;
static constexpr const size_t pm_size = DATA_BUF_SIZE;
static constexpr const size_t cl_size = 64;
static constexpr const size_t mapping_length = pm_size + cl_size;
static constexpr const intptr_t data_addr = DATA_REGION_ADDR;
static constexpr const size_t skip_to_ti = cl_size;
static constexpr const size_t ti_size = 8192;
static constexpr const size_t tis_size = ti_size * DATA_MAX_THREAD;
static constexpr const size_t skip_to_data = (2 << 20);
void *mmappedData;
void *mmappedDataEnd;
int fd;
pthread_mutex_t nvm_lock;
void init_exist(){
if(!exists){
nvm_free_addr = (char*)mmappedData + skip_to_data;
}else{
failedepoch = read_failed_epoch();
currexec = globalepoch = failedepoch + 1;
printf("fe:%lu ge:%lu, currexec:%lu\n",
failedepoch, globalepoch, currexec);
}
}
void access_pages(){
const int page_size = 4096;
char* tmp = (char*)mmappedData;
void* acc_val = nullptr;
for(size_t i=0;i<mapping_length;i+=page_size){
if(i + page_size >= mapping_length) break;
tmp += page_size;
acc_val = ((void**)tmp)[0];
(void)(acc_val);
}
}
public:
bool exists;
void init(){
mkdir(PDATA_DIRNAME, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
pthread_mutex_init(&nvm_lock, NULL);
exists = access( pdata_filename, F_OK ) != -1;
#ifdef USE_OPTANE
fd = open(pdata_filename, O_RDWR | O_CREAT | O_DIRECT, S_IRWXU | S_IRWXG | S_IRWXO);
#else
fd = open(pdata_filename, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
#endif
assert(fd != -1);
if(!exists){
int val = 0;
lseek(fd, mapping_length, SEEK_SET);
assert(write(fd, (void*)&val, sizeof(val))==sizeof(val));
lseek(fd, 0, SEEK_SET);
}
//Execute mmap
#ifdef USE_OPTANE
mmappedData = mmap((void*)data_addr, mapping_length,
PROT_READ | PROT_WRITE, MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0);
#else
mmappedData = mmap((void*)data_addr, mapping_length,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#endif
fallocate(fd, 0, 0, mapping_length);
assert(mmappedData!=MAP_FAILED);
assert(mmappedData == (void *)DATA_REGION_ADDR);
if(!exists){
memset(mmappedData, 0, mapping_length);
}else{
access_pages();
}
mmappedDataEnd = (void*)((char*)mmappedData + mapping_length);
init_exist();
printf("%s data region in %s - Mapped to:%p. Cur free addr:%p\n",
(exists) ? "Found":"Created", pdata_filename, mmappedData, nvm_free_addr);
}
void destroy(){
unlink();
assert(remove(pdata_filename)==0);
}
void* allocate_ti(int pindex){
return (void*)((char*)mmappedData + skip_to_ti + pindex * ti_size);
}
void* malloc_nvm(size_t sz){
void* tmp;
pthread_mutex_lock(&nvm_lock);
tmp = nvm_free_addr;
nvm_free_addr = (void*)((char*)nvm_free_addr + sz);
assert(tmp && nvm_free_addr < mmappedDataEnd);
pthread_mutex_unlock(&nvm_lock);
return tmp;
}
void* malloc_a(size_t sz){
void *ptr = malloc(sz);
return ptr;
}
void unlink(){
pthread_mutex_destroy(&nvm_lock);
munmap(mmappedData, mapping_length);
close(fd);
}
void *get_cur_nvm_addr(){
return nvm_free_addr;
}
void sync_cur_nvm_addr(){
char *beg = (char*)nvm_free_addr;
sync_range(beg, beg+sizeof(void*));
}
void block_malloc_nvm(){
pthread_mutex_lock(&nvm_lock);
}
void write_failed_epoch(mrcu_epoch_type e){
void *epoch_addr = (void*)((char*)mmappedData + pm_size);
*(mrcu_epoch_type*)epoch_addr = e;
}
mrcu_epoch_type read_failed_epoch(){
void *epoch_addr = (void*)((char*)mmappedData + pm_size);
return *(mrcu_epoch_type*)epoch_addr;
}
size_t get_mem_usage(){
char* nvm_current = (char*)nvm_free_addr;
char* nvm_begin = (char*)mmappedData + skip_to_data;
size_t mem_usage = (size_t)(nvm_current - nvm_begin);
return mem_usage;
}
};