-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem.h
461 lines (389 loc) · 11.9 KB
/
mem.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#ifndef MEM_H
#define MEM_H
#include <cstdlib>
#include <string>
#include <mutex>
#include <cstdio>
#include <array>
#include <sys/mman.h>
#include "json11/json11.hpp"
#include "util/arch.h"
#include "util/locks.h"
#include "util/linklist.h"
#include "literals.h"
namespace mem {
constexpr size_t kNrCorePerNode = 8;
enum MemAllocType {
GenericMemory,
EpochQueueItem,
EpochQueuePromise,
Txn,
Promise,
Epoch,
ContentionManagerPool,
EntityPool,
VhandlePool,
RegionPool,
Coroutine,
NumMemTypes,
};
const std::string kMemAllocTypeLabel[] = {
"generic",
"epoch queue item",
"epoch queue promise",
"txn input and state",
"promise",
"epoch",
"^pool:contention manager",
"^pool:row entity",
"^pool:vhandle",
"^pool:region",
"coroutine",
};
struct PoolStatistics {
long long used;
long long watermark;
};
class WeakPool {
protected:
friend class ParallelPool;
friend class ParallelRegion;
friend void PrintMemStats();
void *data;
size_t len;
void * head;
size_t capacity;
MemAllocType alloc_type;
bool need_unmap;
PoolStatistics stats;
public:
WeakPool() : data(nullptr), len(0), head(nullptr), capacity(0), need_unmap(false) {}
WeakPool(MemAllocType alloc_type, size_t chunk_size, size_t cap, int numa_node = -1);
WeakPool(MemAllocType alloc_type, size_t chunk_size, size_t cap, void *data);
WeakPool(const WeakPool &rhs) = delete;
WeakPool(WeakPool &&rhs) {
data = rhs.data;
capacity = rhs.capacity;
len = rhs.len;
alloc_type = rhs.alloc_type;
head = rhs.head;
stats = rhs.stats;
need_unmap = rhs.need_unmap;
rhs.data = nullptr;
rhs.capacity = 0;
rhs.head = nullptr;
rhs.need_unmap = false;
}
~WeakPool();
WeakPool &operator=(WeakPool &&rhs) {
if (this != &rhs) {
this->~WeakPool();
new (this) WeakPool(std::move(rhs));
}
return *this;
}
void *Alloc();
void Free(void *ptr);
size_t total_capacity() const { return capacity; }
void *data_ptr() const { return data; }
bool is_full() const { return head == nullptr; }
bool is_empty() const { return stats.used == 0; }
void Register();
};
// This checks ownership of the pointer
class BasicPool : public WeakPool {
bool suppress_warning = false;
public:
using WeakPool::WeakPool;
long CheckPointer(void *ptr);
void *Alloc();
void Free(void *ptr);
void set_suppress_warning(bool suppress_warning) {
this->suppress_warning = suppress_warning;
}
};
// Thread-Safe version
class Pool : public BasicPool {
util::SpinLock lock;
public:
using BasicPool::BasicPool;
Pool &operator=(Pool &&rhs) {
auto &o = (WeakPool &)(*this);
o = (WeakPool &&) rhs;
return (*this);
}
void *Alloc() {
auto _ = util::Guard(lock);
return BasicPool::Alloc();
}
void Free(void *ptr) {
auto _ = util::Guard(lock);
BasicPool::Free(ptr);
}
};
static_assert(sizeof(BasicPool) <= CACHE_LINE_SIZE);
void InitTotalNumberOfCores(int nr_cores);
// Before we implement a region allocator, we need to implement a Slab
// allocator. Slab allocator is to make memory from different pools shared at
// the page granularity to reduce memory fragmentation. This is extremely useful
// for the partitioned skewed workload, where one core allocate all the memory.
void InitSlab(size_t mem);
// SlabPool can take care of chunks <= 512_K or chunks <= 16_M. For chunks larger
// than 512_K, SlabPool will ask for memory from the large metaslabs. These are
// 64_M in page size.
class Slab;
class SlabPool {
friend class ParallelRegion;
util::GenericListNode<Slab> empty;
util::GenericListNode<Slab> half_full;
MemAllocType alloc_type;
unsigned int numa_node;
unsigned int nr_empty;
unsigned int nr_buffer;
unsigned int chunk_size;
PoolStatistics stats;
public:
SlabPool(MemAllocType alloc_type, unsigned int chunk_size,
unsigned int nr_buffer, int numa_node);
void *Alloc();
void Free(void *ptr);
void Register();
static constexpr size_t kSlabPageSize = 2_M;
static constexpr size_t kLargeSlabPageSize = 64_M;
static size_t PageSize(bool large_slab) {
return large_slab ? kLargeSlabPageSize : kSlabPageSize;
}
static size_t PageSize(size_t chunk_size) {
return PageSize(chunk_size >= 512_K);
}
bool is_large_slab() const { return chunk_size >= 512_K; }
size_t metaslab_page_size() const { return PageSize(is_large_slab()); }
private:
Slab *RefillSlab();
void ReturnSlab();
};
class ParallelAllocationPolicy {
protected:
static thread_local int g_affinity;
public:
static int g_nr_cores;
static int g_core_shifting;
static std::mutex *g_core_locks;
static constexpr int kMaxNrPools = 64;
// Affinity can override the current thread id. However, this has to be
// exclusive among different cores. That's why we need the maximum number of
// cores upfront.
//
// SetCurrentAffinity() will acquire a lock before setting the affinity for
// the current thread. This is essential to keep the ParallelPool safe.
static void SetCurrentAffinity(int aff);
static int CurrentAffinity();
};
template <typename PoolType>
class ParallelAllocator : public ParallelAllocationPolicy {
protected:
struct ConsolidateFreeList {
uint64_t dice = 0;
uint64_t bitmap = 0;
std::array<uintptr_t, kMaxNrPools> heads = {};
};
std::array<PoolType *, kMaxNrPools> pools;
std::array<uintptr_t *, kMaxNrPools> free_lists;
std::array<uintptr_t *, kMaxNrPools> free_tails;
std::array<ConsolidateFreeList *, kMaxNrPools> csld_free_lists;
size_t chunk_size;
size_t total_cap;
MemAllocType alloc_type;
static const size_t kHeaderSize = sizeof(PoolType)
+ 2 * kMaxNrPools * sizeof(uintptr_t)
+ sizeof(ConsolidateFreeList);
public:
ParallelAllocator() : total_cap(0) {
pools.fill(nullptr);
free_lists.fill(nullptr);
free_tails.fill(nullptr);
csld_free_lists.fill(nullptr);
}
ParallelAllocator(const ParallelAllocator<PoolType>& rhs) = delete;
ParallelAllocator(ParallelAllocator<PoolType> &&rhs) {
pools = rhs.pools;
free_lists = rhs.free_lists;
free_tails = rhs.free_tails;
csld_free_lists = rhs.csld_free_lists;
chunk_size = rhs.chunk_size;
total_cap = rhs.total_cap;
alloc_type = rhs.alloc_type;
rhs.total_cap = -1;
rhs.pools.fill(nullptr);
rhs.free_lists.fill(nullptr);
rhs.free_tails.fill(nullptr);
rhs.csld_free_lists.fill(nullptr);
}
size_t capacity() const { return total_cap; }
PoolType *get_pool(int idx) const { return pools[idx]; }
void Register() {
for (auto i = 0; i < g_nr_cores; i++) pools[i]->Register();
}
void *Alloc() {
auto cur = CurrentAffinity();
auto csld = csld_free_lists[cur];
auto &dice = csld->dice;
if (csld->bitmap != 0) {
auto n = csld->bitmap >> dice;
dice = (n == 0) ? __builtin_ctzll(csld->bitmap) : __builtin_ctzll(n) + dice;
auto &head = csld->heads[dice];
auto p = (void *) head;
head = *(uintptr_t *) head;
if (head == 0) csld->bitmap &= ~(1 << dice);
__builtin_prefetch((void *) head);
return p;
}
return pools[cur]->Alloc();
}
void Free(void *ptr, int alloc_core) {
auto cur = CurrentAffinity();
if (alloc_core < 0 || alloc_core >= kMaxNrPools) {
fprintf(stderr, "alloc_core error, is %d\n", alloc_core);
std::abort();
}
// Trying to free to an extra pool. Then you must be on that core to free to
// this pool!
if (alloc_core >= g_nr_cores && cur != alloc_core) {
fprintf(stderr, "alloc_core is not current core, is %d\n", alloc_core);
std::abort();
}
if (cur == alloc_core) {
pools[cur]->Free(ptr);
} else {
if (free_lists[cur][alloc_core] == 0)
free_tails[cur][alloc_core] = (uintptr_t) ptr;
*(uintptr_t *) ptr = free_lists[cur][alloc_core];
free_lists[cur][alloc_core] = (uintptr_t) ptr;
}
}
void Quiescence() {
auto cur = CurrentAffinity();
// We do not want to free them back to the pool right now, because the
// objects in the list are cold now.
auto csld = csld_free_lists[cur];
for (int i = 0; i < g_nr_cores; i++) {
uintptr_t tail = free_tails[i][cur];
if (tail) {
*(uintptr_t *) tail = csld->heads[i];
csld->heads[i] = free_lists[i][cur];
free_lists[i][cur] = free_tails[i][cur] = 0;
csld->bitmap |= 1 << i;
}
}
}
};
class ParallelPool : public ParallelAllocator<BasicPool> {
public:
ParallelPool() : ParallelAllocator() {}
ParallelPool(MemAllocType alloc_type, size_t chunk_size, size_t total_cap);
~ParallelPool();
ParallelPool(ParallelPool &&rhs) : ParallelAllocator(std::move(rhs)) {}
ParallelPool &operator=(ParallelPool &&rhs) {
if (&rhs != this) {
this->~ParallelPool();
new (this) ParallelPool(std::move(rhs));
}
return *this;
}
// You can add a dedicate pool.
void AddExtraBasicPool(int core, size_t cap = 0, int node = -1);
};
class ParallelSlabPool : public ParallelAllocator<SlabPool> {
public:
ParallelSlabPool() : ParallelAllocator() {}
ParallelSlabPool(MemAllocType alloc_type, size_t chunk_size, unsigned int buffer);
~ParallelSlabPool();
ParallelSlabPool(ParallelSlabPool &&rhs) : ParallelAllocator(std::move(rhs)) {}
ParallelSlabPool &operator=(ParallelSlabPool &&rhs) {
if (&rhs != this) {
this->~ParallelSlabPool();
new (this) ParallelSlabPool(std::move(rhs));
}
return *this;
}
};
class ParallelRegion {
static const int kMaxPools = 20;
// static const int kMaxPools = 12;
ParallelSlabPool pools[32];
size_t proposed_caps[32];
public:
ParallelRegion();
ParallelRegion(const ParallelRegion &) = delete;
static int SizeToClass(size_t sz) {
int idx = 64 - __builtin_clzl(sz - 1) - 5;
if (__builtin_expect(idx >= kMaxPools, 0)) {
fprintf(stderr, "Requested invalid size class %d %lu\n", idx, sz);
return -1;
}
return idx < 0 ? 0 : idx;
}
void ApplyFromConf(json11::Json conf);
void set_pool_capacity(size_t sz, size_t cap) {
int k = SizeToClass(sz);
if (k < 0) std::abort();
proposed_caps[k] = cap;
}
void InitPools();
void *Alloc(size_t sz);
void Free(void *ptr, int alloc_core, size_t sz);
void Quiescence();
void PrintUsageEachClass();
};
ParallelRegion &GetDataRegion();
class Brk {
std::atomic_size_t offset;
size_t limit;
uint8_t *data;
bool thread_safe;
public:
Brk() : offset(0), limit(0), data(nullptr), thread_safe(false) {}
Brk(void *p, size_t limit) : offset(0), limit(limit), data((uint8_t *) p), thread_safe(false) {}
~Brk() {}
Brk(Brk &&rhs) {
data = rhs.data;
limit = rhs.limit;
offset.store(rhs.offset.load(std::memory_order_relaxed), std::memory_order_relaxed);
thread_safe = rhs.thread_safe;
rhs.offset = 0;
rhs.limit = 0;
rhs.data = nullptr;
}
Brk &operator =(Brk &&rhs) {
if (this != &rhs) {
this->~Brk();
new (this) Brk(std::move(rhs));
}
return *this;
}
void set_thread_safe(bool safe) {
thread_safe = safe;
}
// This is a special New() function. It avoids memory allocation.
static Brk *New(void *buf, size_t sz) {
auto *p = (uint8_t *) buf;
auto hdr_size = util::Align(sizeof(Brk), 16);
return new (p) Brk(p + hdr_size, sz - hdr_size);
}
bool Check(size_t s) { return offset + s <= limit; }
void Reset() { offset = 0; }
void *Alloc(size_t s);
uint8_t *ptr() const { return data; }
size_t current_size() const { return offset; }
};
#define NewStackBrk(sz) mem::Brk::New(alloca(sz), sz)
#define INIT_ROUTINE_BRK(sz) go::RoutineScopedData _______(NewStackBrk(sz));
void *AllocFromRoutine(size_t sz);
PoolStatistics GetMemStats(MemAllocType alloc_type);
void PrintMemStats();
void *AllocMemory(mem::MemAllocType alloc_type, size_t length,
int numa_node = -1, bool on_demand = false);
long TotalMemoryAllocated();
}
std::string MemTypeToString(mem::MemAllocType alloc_type);
#endif /* MEM_H */