-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest-bench-steal.cc
257 lines (221 loc) · 7.32 KB
/
test-bench-steal.cc
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <thread>
#include <set>
#include <atomic>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include "bench_common.h"
#if !defined(SAME_THREAD) && !defined(DIFFERENT_THREAD)
#define DIFFERENT_THREAD
#endif
// #define USE_LOCKED_SYNC
// For the thread pool, the offsets at which allocations or frees
// have been performed by other threads have to be synchronized.
//
// If USE_LOCKED_SYNC is set, this is done with a locked queue,
// otherwise with a lock-free CAS-based stack
#ifdef USE_LOCKED_SYNC
#define PUSH_OFFSET(kind, offs) \
do { \
oq_##kind.push(offs); \
} while (0);
#define POP_OFFSET(kind) \
oq_##kind.pop();
#else // NOT USE_LOCKED_SYNC
#define PUSH_OFFSET(kind, offs) \
do { \
s_##kind.push(offs); \
} while (0);
#define POP_OFFSET(kind) \
s_##kind.pop();
#endif
uint16_t prepare_vector(struct exmap_user_interface *interface, unsigned spread, unsigned offset) {
for (unsigned i = 0; i < EXMAP_USER_INTERFACE_PAGES; i++) {
interface->iov[i].page = offset + i * spread;
interface->iov[i].len = 1;
}
return EXMAP_USER_INTERFACE_PAGES;
}
int touch_vector(volatile char *exmap, unsigned spread, unsigned offset) {
for (unsigned i = 0; i < EXMAP_USER_INTERFACE_PAGES; i++) {
exmap[(i * spread + offset) * PAGE_SIZE] ++;
}
return EXMAP_USER_INTERFACE_PAGES;
}
int main() {
std::atomic<uint64_t> readCnt(0);
#ifdef SAME_THREAD
int thread_count = atoi(getenv("THREADS") ?: "1");
#endif
#ifdef DIFFERENT_THREAD
int alloc_count, free_count, thread_count;
if (getenv("ALLOC") && getenv("FREE")) {
alloc_count = atoi(getenv("ALLOC"));
free_count = atoi(getenv("FREE"));
thread_count = alloc_count + free_count;
} else {
thread_count = atoi(getenv("THREADS") ?: "2");
thread_count = thread_count / 2;
if (thread_count < 1)
return -2;
alloc_count = free_count = thread_count;
thread_count = alloc_count + free_count;
}
// queues containing offsets that identify pages
// that have been allocated or freed, meaning they are
// ready to be freed or allocated again, respectively
LockedQueue<unsigned> oq_a, oq_f;
lf_stack<unsigned> s_a, s_f;
#endif
int fd = open("/dev/exmap", O_RDWR);
if (fd < 0) die("open");
const size_t MEMORY_POOL_PAGES = (1024 * 1024 * 1024) >> 12;
const size_t SPREAD = 10;
const size_t MAP_SIZE = MEMORY_POOL_PAGES * SPREAD * PAGE_SIZE;
char *map = (char*) mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) die("mmap");
printf("# MAP: %p-%p\n", map, map + MAP_SIZE);
// must fail!
void *tmp = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
assert(tmp == MAP_FAILED && errno == EBUSY);
struct exmap_ioctl_setup buffer;
buffer.fd = -1; // Not backed by a file
buffer.max_interfaces = thread_count;
buffer.buffer_size = MEMORY_POOL_PAGES;
buffer.flags = 0;
if (ioctl(fd, EXMAP_IOCTL_SETUP, &buffer) < 0) {
perror("ioctl: exmap_setup");
return -1;
/* handle error */
}
std::vector<std::thread> threads;
////////////////////////////////////////////////////////////////
// Alloc/Free Thread
#ifdef SAME_THREAD
for (uint16_t thread_id=0; thread_id < thread_count; thread_id++) {
threads.emplace_back([&, thread_id]() {
auto interface_a = (struct exmap_user_interface *)
mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, EXMAP_OFF_INTERFACE(thread_id));
if (interface_a == MAP_FAILED) die("mmap");
auto interface_f = (struct exmap_user_interface *)
mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, EXMAP_OFF_INTERFACE((thread_id + thread_count)));
if (interface_f == MAP_FAILED) die("mmap");
unsigned base_offset = thread_id * EXMAP_USER_INTERFACE_PAGES * 2;
while(true) {
for (unsigned _offset = 0; _offset < 2; _offset++) {
unsigned offset = base_offset + _offset;
// alloc on one interface
uint16_t nr_pages = prepare_vector(interface_a, 2, offset);
struct exmap_action_params params_alloc = {
.interface = thread_id,
.iov_len = nr_pages,
.opcode = EXMAP_OP_ALLOC,
};
if (ioctl(fd, EXMAP_IOCTL_ACTION, ¶ms_alloc) < 0) {
perror("ioctl: exmap_action");
}
touch_vector(map, 2, offset);
readCnt += nr_pages;
// free on another interface
nr_pages = prepare_vector(interface_f, 2, offset);
uint16_t iface = (thread_id + thread_count);
struct exmap_action_params params_free = {
.interface = iface,
.iov_len = nr_pages,
.opcode = EXMAP_OP_FREE,
};
if (ioctl(fd, EXMAP_IOCTL_ACTION, ¶ms_free) < 0) {
perror("ioctl: exmap_action");
}
} // offset \in {0, 1}
}
});
}
#endif
#ifdef DIFFERENT_THREAD
// Initialize the free queue with every page that we've put into the free buffer.
printf("# %d bundles, %d alloc threads\n", MEMORY_POOL_PAGES / EXMAP_USER_INTERFACE_PAGES, alloc_count);
for (uint16_t i = 0; i < MEMORY_POOL_PAGES / (EXMAP_USER_INTERFACE_PAGES * 2); i++) {
PUSH_OFFSET(f, i * EXMAP_USER_INTERFACE_PAGES);
}
for (uint16_t thread_id=0; thread_id < alloc_count; thread_id++) {
// alloc thread
threads.emplace_back([&, thread_id]() {
auto iface = thread_id;
auto interface = (struct exmap_user_interface *)
mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, EXMAP_OFF_INTERFACE(iface));
if (interface == MAP_FAILED) die("mmap");
while(true) {
// we can only (re-)alloc for a base_offset when the corresponding free has finished
// if the free is still running: use after free
unsigned offset = POP_OFFSET(f);
// alloc on one interface
uint16_t nr_pages = prepare_vector(interface, 1, offset);
struct exmap_action_params params_alloc = {
.interface = iface,
.iov_len = nr_pages,
.opcode = EXMAP_OP_ALLOC,
};
if (ioctl(fd, EXMAP_IOCTL_ACTION, ¶ms_alloc) < 0) {
perror("ioctl: exmap_action");
}
touch_vector((volatile char*) map, 1, offset);
readCnt += nr_pages;
PUSH_OFFSET(a, offset);
}
});
}
for (uint16_t thread_id=0; thread_id < free_count; thread_id++) {
// free thread
threads.emplace_back([&, thread_id]() {
uint16_t iface = thread_id + alloc_count;
auto interface = (struct exmap_user_interface *)
mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, EXMAP_OFF_INTERFACE(iface));
if (interface == MAP_FAILED) die("mmap");
while(true) {
unsigned offset = POP_OFFSET(a);
// free on another interface
uint16_t nr_pages = prepare_vector(interface, 1, offset);
struct exmap_action_params params_free = {
.interface = iface,
.iov_len = nr_pages,
.opcode = EXMAP_OP_FREE,
};
if (ioctl(fd, EXMAP_IOCTL_ACTION, ¶ms_free) < 0) {
perror("ioctl: exmap_action");
}
PUSH_OFFSET(f, offset);
}
});
}
#endif
auto last_shootdowns = readTLBShootdownCount();
int secs = 0;
output_legend();
while (true) {
sleep(1);
auto shootdowns = readTLBShootdownCount();
auto diff = (shootdowns-last_shootdowns);
auto lastReadCnt = (unsigned)readCnt.exchange(0);
output_line(secs++, lastReadCnt, diff);
last_shootdowns= shootdowns;
}
close(fd);
}