forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cc
1403 lines (1254 loc) · 38.5 KB
/
memory.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
/// \cond internal
//
// Seastar memory allocator
//
// This is a share-nothing allocator (memory allocated on one cpu must
// be freed on the same cpu).
//
// Inspired by gperftools' tcmalloc.
//
// Memory map:
//
// 0x0000'sccc'vvvv'vvvv
//
// 0000 - required by architecture (only 48 bits of address space)
// s - chosen to satisfy system allocator (1-7)
// ccc - cpu number (0-12 bits allocated vary according to system)
// v - virtual address within cpu (32-44 bits, according to how much ccc
// leaves us
//
// Each page has a page structure that describes it. Within a cpu's
// memory pool, the page array starts at offset 0, describing all pages
// within that pool. Page 0 does not describe a valid page.
//
// Each pool can contain at most 2^32 pages (or 44 address bits), so we can
// use a 32-bit integer to identify a page.
//
// Runs of pages are organized into spans. Free spans are organized into lists,
// by size. When spans are broken up or coalesced, they may move into new lists.
#include "memory.hh"
#ifndef DEFAULT_ALLOCATOR
#include "bitops.hh"
#include "align.hh"
#include "posix.hh"
#include "shared_ptr.hh"
#include <new>
#include <cstdint>
#include <algorithm>
#include <limits>
#include <cassert>
#include <atomic>
#include <mutex>
#include <experimental/optional>
#include <functional>
#include <cstring>
#include <boost/intrusive/list.hpp>
#include <sys/mman.h>
#ifdef HAVE_NUMA
#include <numaif.h>
#endif
namespace memory {
static constexpr unsigned cpu_id_shift = 36; // FIXME: make dynamic
static constexpr unsigned max_cpus = 256;
static constexpr size_t cache_line_size = 64;
using pageidx = uint32_t;
struct page;
class page_list;
static std::atomic<bool> live_cpus[max_cpus];
static thread_local uint64_t g_allocs;
static thread_local uint64_t g_frees;
static thread_local uint64_t g_cross_cpu_frees;
static thread_local uint64_t g_reclaims;
using std::experimental::optional;
using allocate_system_memory_fn
= std::function<mmap_area (optional<void*> where, size_t how_much)>;
namespace bi = boost::intrusive;
inline
unsigned object_cpu_id(const void* ptr) {
return (reinterpret_cast<uintptr_t>(ptr) >> cpu_id_shift) & 0xff;
}
class page_list_link {
uint32_t _prev;
uint32_t _next;
friend class page_list;
};
static char* mem_base() {
static char* known;
static std::once_flag flag;
std::call_once(flag, [] {
size_t alloc = size_t(1) << 44;
auto r = ::mmap(NULL, 2 * alloc,
PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
-1, 0);
if (r == MAP_FAILED) {
abort();
}
::madvise(r, 2 * alloc, MADV_DONTDUMP);
auto cr = reinterpret_cast<char*>(r);
known = align_up(cr, alloc);
::munmap(cr, known - cr);
::munmap(known + alloc, cr + 2 * alloc - (known + alloc));
});
return known;
}
class small_pool;
struct free_object {
free_object* next;
};
struct page {
bool free;
uint8_t offset_in_span;
uint16_t nr_small_alloc;
uint32_t span_size; // in pages, if we're the head or the tail
page_list_link link;
small_pool* pool; // if used in a small_pool
free_object* freelist;
};
class page_list {
uint32_t _front = 0;
uint32_t _back = 0;
public:
page& front(page* ary) { return ary[_front]; }
page& back(page* ary) { return ary[_back]; }
bool empty() const { return !_front; }
void erase(page* ary, page& span) {
if (span.link._next) {
ary[span.link._next].link._prev = span.link._prev;
} else {
_back = span.link._prev;
}
if (span.link._prev) {
ary[span.link._prev].link._next = span.link._next;
} else {
_front = span.link._next;
}
}
void push_front(page* ary, page& span) {
auto idx = &span - ary;
if (_front) {
ary[_front].link._prev = idx;
} else {
_back = idx;
}
span.link._next = _front;
span.link._prev = 0;
_front = idx;
}
void pop_front(page* ary) {
if (ary[_front].link._next) {
ary[ary[_front].link._next].link._prev = 0;
} else {
_back = 0;
}
_front = ary[_front].link._next;
}
page* find(uint32_t n_pages, page* ary) {
auto n = _front;
while (n && ary[n].span_size < n_pages) {
n = ary[n].link._next;
}
if (!n) {
return nullptr;
}
return &ary[n];
}
};
class small_pool {
unsigned _object_size;
unsigned _span_size;
free_object* _free = nullptr;
size_t _free_count = 0;
unsigned _min_free;
unsigned _max_free;
unsigned _spans_in_use = 0;
page_list _span_list;
static constexpr unsigned idx_frac_bits = 2;
private:
size_t span_bytes() const { return _span_size * page_size; }
public:
explicit small_pool(unsigned object_size) noexcept;
~small_pool();
void* allocate();
void deallocate(void* object);
unsigned object_size() const { return _object_size; }
static constexpr unsigned size_to_idx(unsigned size);
static constexpr unsigned idx_to_size(unsigned idx);
private:
void add_more_objects();
void trim_free_list();
float waste();
};
// index 0b0001'1100 -> size (1 << 4) + 0b11 << (4 - 2)
constexpr unsigned
small_pool::idx_to_size(unsigned idx) {
return (((1 << idx_frac_bits) | (idx & ((1 << idx_frac_bits) - 1)))
<< (idx >> idx_frac_bits))
>> idx_frac_bits;
}
static constexpr unsigned log2ceil(unsigned n) {
return std::numeric_limits<unsigned>::digits - count_leading_zeros(n-1);
}
static constexpr unsigned log2floor(unsigned n) {
return std::numeric_limits<unsigned>::digits - count_leading_zeros(n) - 1;
}
constexpr unsigned
small_pool::size_to_idx(unsigned size) {
return ((log2floor(size) << idx_frac_bits) - ((1 << idx_frac_bits) - 1))
+ ((size - 1) >> (log2floor(size) - idx_frac_bits));
}
class small_pool_array {
public:
static constexpr unsigned nr_small_pools = small_pool::size_to_idx(4 * page_size) + 1;
private:
union u {
small_pool a[nr_small_pools];
u() {
for (unsigned i = 0; i < nr_small_pools; ++i) {
new (&a[i]) small_pool(small_pool::idx_to_size(i));
}
}
~u() {
// cannot really call destructor, since other
// objects may be freed after we are gone.
}
} _u;
public:
small_pool& operator[](unsigned idx) { return _u.a[idx]; }
};
static constexpr size_t max_small_allocation
= small_pool::idx_to_size(small_pool_array::nr_small_pools - 1);
struct cross_cpu_free_item {
cross_cpu_free_item* next;
};
struct cpu_pages {
static constexpr unsigned min_free_pages = 20000000 / page_size;
char* memory;
page* pages;
uint32_t nr_pages;
uint32_t nr_free_pages;
uint32_t current_min_free_pages = 0;
unsigned cpu_id = -1U;
std::function<void (std::function<void ()>)> reclaim_hook;
std::vector<reclaimer*> reclaimers;
static constexpr unsigned nr_span_lists = 32;
union pla {
pla() {
for (auto&& e : free_spans) {
new (&e) page_list;
}
}
~pla() {
// no destructor -- might be freeing after we die
}
page_list free_spans[nr_span_lists]; // contains spans with span_size >= 2^idx
} fsu;
small_pool_array small_pools;
alignas(cache_line_size) std::atomic<cross_cpu_free_item*> xcpu_freelist;
alignas(cache_line_size) std::vector<physical_address> virt_to_phys_map;
static std::atomic<unsigned> cpu_id_gen;
static cpu_pages* all_cpus[max_cpus];
char* mem() { return memory; }
void link(page_list& list, page* span);
void unlink(page_list& list, page* span);
struct trim {
unsigned offset;
unsigned nr_pages;
};
template <typename Trimmer>
void* allocate_large_and_trim(unsigned nr_pages, Trimmer trimmer);
void* allocate_large(unsigned nr_pages);
void* allocate_large_aligned(unsigned align_pages, unsigned nr_pages);
page* find_and_unlink_span(unsigned nr_pages);
page* find_and_unlink_span_reclaiming(unsigned n_pages);
void free_large(void* ptr);
void free_span(pageidx start, uint32_t nr_pages);
void free_span_no_merge(pageidx start, uint32_t nr_pages);
void* allocate_small(unsigned size);
void free(void* ptr);
void free(void* ptr, size_t size);
void shrink(void* ptr, size_t new_size);
void free_cross_cpu(unsigned cpu_id, void* ptr);
bool drain_cross_cpu_freelist();
size_t object_size(void* ptr);
page* to_page(void* p) {
return &pages[(reinterpret_cast<char*>(p) - mem()) / page_size];
}
bool is_initialized() const;
bool initialize();
reclaiming_result run_reclaimers(reclaimer_scope);
void schedule_reclaim();
void set_reclaim_hook(std::function<void (std::function<void ()>)> hook);
void resize(size_t new_size, allocate_system_memory_fn alloc_sys_mem);
void do_resize(size_t new_size, allocate_system_memory_fn alloc_sys_mem);
void replace_memory_backing(allocate_system_memory_fn alloc_sys_mem);
void init_virt_to_phys_map();
memory::memory_layout memory_layout();
translation translate(const void* addr, size_t size);
~cpu_pages();
};
constexpr unsigned cpu_pages::min_free_pages;
static thread_local cpu_pages cpu_mem;
std::atomic<unsigned> cpu_pages::cpu_id_gen;
cpu_pages* cpu_pages::all_cpus[max_cpus];
// Free spans are store in the largest index i such that nr_pages >= 1 << i.
static inline
unsigned index_of(unsigned pages) {
return std::numeric_limits<unsigned>::digits - count_leading_zeros(pages) - 1;
}
// Smallest index i such that all spans stored in the index are >= pages.
static inline
unsigned index_of_conservative(unsigned pages) {
if (pages == 1) {
return 0;
}
return std::numeric_limits<unsigned>::digits - count_leading_zeros(pages - 1);
}
void
cpu_pages::unlink(page_list& list, page* span) {
list.erase(pages, *span);
}
void
cpu_pages::link(page_list& list, page* span) {
list.push_front(pages, *span);
}
void cpu_pages::free_span_no_merge(uint32_t span_start, uint32_t nr_pages) {
assert(nr_pages);
nr_free_pages += nr_pages;
auto span = &pages[span_start];
auto span_end = &pages[span_start + nr_pages - 1];
span->free = span_end->free = true;
span->span_size = span_end->span_size = nr_pages;
auto idx = index_of(nr_pages);
link(fsu.free_spans[idx], span);
}
void cpu_pages::free_span(uint32_t span_start, uint32_t nr_pages) {
page* before = &pages[span_start - 1];
if (before->free) {
auto b_size = before->span_size;
assert(b_size);
span_start -= b_size;
nr_pages += b_size;
nr_free_pages -= b_size;
unlink(fsu.free_spans[index_of(b_size)], before - (b_size - 1));
}
page* after = &pages[span_start + nr_pages];
if (after->free) {
auto a_size = after->span_size;
assert(a_size);
nr_pages += a_size;
nr_free_pages -= a_size;
unlink(fsu.free_spans[index_of(a_size)], after);
}
free_span_no_merge(span_start, nr_pages);
}
page*
cpu_pages::find_and_unlink_span(unsigned n_pages) {
auto idx = index_of_conservative(n_pages);
auto orig_idx = idx;
if (n_pages >= (2u << idx)) {
throw std::bad_alloc();
}
while (idx < nr_span_lists && fsu.free_spans[idx].empty()) {
++idx;
}
if (idx == nr_span_lists) {
if (initialize()) {
return find_and_unlink_span(n_pages);
}
// Can smaller list possibly hold object?
idx = index_of(n_pages);
if (idx == orig_idx) { // was exact power of two
return nullptr;
}
}
auto& list = fsu.free_spans[idx];
page* span = list.find(n_pages, pages);
if (!span) {
return nullptr;
}
unlink(list, span);
return span;
}
page*
cpu_pages::find_and_unlink_span_reclaiming(unsigned n_pages) {
while (true) {
auto span = find_and_unlink_span(n_pages);
if (span) {
return span;
}
if (run_reclaimers(reclaimer_scope::sync) == reclaiming_result::reclaimed_nothing) {
return nullptr;
}
}
}
template <typename Trimmer>
void*
cpu_pages::allocate_large_and_trim(unsigned n_pages, Trimmer trimmer) {
page* span = find_and_unlink_span_reclaiming(n_pages);
if (!span) {
return nullptr;
}
auto span_size = span->span_size;
auto span_idx = span - pages;
nr_free_pages -= span->span_size;
trim t = trimmer(span_idx, nr_pages);
if (t.offset) {
free_span_no_merge(span_idx, t.offset);
span_idx += t.offset;
span_size -= t.offset;
span = &pages[span_idx];
}
if (t.nr_pages < span_size) {
free_span_no_merge(span_idx + t.nr_pages, span_size - t.nr_pages);
span_size = t.nr_pages;
}
auto span_end = &pages[span_idx + t.nr_pages - 1];
span->free = span_end->free = false;
span->span_size = span_end->span_size = t.nr_pages;
span->pool = nullptr;
if (nr_free_pages < current_min_free_pages) {
drain_cross_cpu_freelist();
run_reclaimers(reclaimer_scope::sync);
if (nr_free_pages < current_min_free_pages) {
schedule_reclaim();
}
}
return mem() + span_idx * page_size;
}
void*
cpu_pages::allocate_large(unsigned n_pages) {
return allocate_large_and_trim(n_pages, [n_pages] (unsigned idx, unsigned n) {
return trim{0, std::min(n, n_pages)};
});
}
void*
cpu_pages::allocate_large_aligned(unsigned align_pages, unsigned n_pages) {
return allocate_large_and_trim(n_pages + align_pages - 1, [=] (unsigned idx, unsigned n) {
return trim{align_up(idx, align_pages) - idx, n_pages};
});
}
void*
cpu_pages::allocate_small(unsigned size) {
auto idx = small_pool::size_to_idx(size);
auto& pool = small_pools[idx];
assert(size <= pool.object_size());
return pool.allocate();
}
void cpu_pages::free_large(void* ptr) {
pageidx idx = (reinterpret_cast<char*>(ptr) - mem()) / page_size;
page* span = &pages[idx];
free_span(idx, span->span_size);
}
size_t cpu_pages::object_size(void* ptr) {
pageidx idx = (reinterpret_cast<char*>(ptr) - mem()) / page_size;
page* span = &pages[idx];
if (span->pool) {
return span->pool->object_size();
} else {
return size_t(span->span_size) * page_size;
}
}
void cpu_pages::free_cross_cpu(unsigned cpu_id, void* ptr) {
if (!live_cpus[cpu_id].load(std::memory_order_relaxed)) {
// Thread was destroyed; leak object
// should only happen for boost unit-tests.
return;
}
auto p = reinterpret_cast<cross_cpu_free_item*>(ptr);
auto& list = all_cpus[cpu_id]->xcpu_freelist;
auto old = list.load(std::memory_order_relaxed);
do {
p->next = old;
} while (!list.compare_exchange_weak(old, p, std::memory_order_release, std::memory_order_relaxed));
++g_cross_cpu_frees;
}
bool cpu_pages::drain_cross_cpu_freelist() {
if (!xcpu_freelist.load(std::memory_order_relaxed)) {
return false;
}
auto p = xcpu_freelist.exchange(nullptr, std::memory_order_acquire);
while (p) {
auto n = p->next;
free(p);
p = n;
}
return true;
}
void cpu_pages::free(void* ptr) {
auto obj_cpu = object_cpu_id(ptr);
if (obj_cpu != cpu_id) {
return free_cross_cpu(obj_cpu, ptr);
}
page* span = to_page(ptr);
if (span->pool) {
span->pool->deallocate(ptr);
} else {
free_large(ptr);
}
}
void cpu_pages::free(void* ptr, size_t size) {
// match action on allocate() so hit the right pool
if (size <= sizeof(free_object)) {
size = sizeof(free_object);
}
auto obj_cpu = object_cpu_id(ptr);
if (obj_cpu != cpu_id) {
return free_cross_cpu(obj_cpu, ptr);
}
if (size <= max_small_allocation) {
auto pool = &small_pools[small_pool::size_to_idx(size)];
pool->deallocate(ptr);
} else {
free_large(ptr);
}
}
void cpu_pages::shrink(void* ptr, size_t new_size) {
auto obj_cpu = object_cpu_id(ptr);
assert(obj_cpu == cpu_id);
page* span = to_page(ptr);
if (span->pool) {
return;
}
size_t new_size_pages = align_up(new_size, page_size) / page_size;
auto old_size_pages = span->span_size;
assert(old_size_pages >= new_size_pages);
if (new_size_pages == old_size_pages) {
return;
}
span->span_size = new_size_pages;
span[new_size_pages - 1].free = false;
span[new_size_pages - 1].span_size = new_size_pages;
pageidx idx = span - pages;
free_span(idx + new_size_pages, old_size_pages - new_size_pages);
}
cpu_pages::~cpu_pages() {
live_cpus[cpu_id].store(false, std::memory_order_relaxed);
}
bool cpu_pages::is_initialized() const {
return bool(nr_pages);
}
bool cpu_pages::initialize() {
if (is_initialized()) {
return false;
}
cpu_id = cpu_id_gen.fetch_add(1, std::memory_order_relaxed);
assert(cpu_id < max_cpus);
all_cpus[cpu_id] = this;
auto base = mem_base() + (size_t(cpu_id) << cpu_id_shift);
auto size = 32 << 20; // Small size for bootstrap
auto r = ::mmap(base, size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
-1, 0);
if (r == MAP_FAILED) {
abort();
}
::madvise(base, size, MADV_HUGEPAGE);
pages = reinterpret_cast<page*>(base);
memory = base;
nr_pages = size / page_size;
// we reserve the end page so we don't have to special case
// the last span.
auto reserved = align_up(sizeof(page) * (nr_pages + 1), page_size) / page_size;
for (pageidx i = 0; i < reserved; ++i) {
pages[i].free = false;
}
pages[nr_pages].free = false;
free_span_no_merge(reserved, nr_pages - reserved);
live_cpus[cpu_id].store(true, std::memory_order_relaxed);
return true;
}
mmap_area
allocate_anonymous_memory(optional<void*> where, size_t how_much) {
return mmap_anonymous(where.value_or(nullptr),
how_much,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | (where ? MAP_FIXED : 0));
}
mmap_area
allocate_hugetlbfs_memory(file_desc& fd, optional<void*> where, size_t how_much) {
auto pos = fd.size();
fd.truncate(pos + how_much);
auto ret = fd.map(
how_much,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE | (where ? MAP_FIXED : 0),
pos,
where.value_or(nullptr));
return ret;
}
void cpu_pages::replace_memory_backing(allocate_system_memory_fn alloc_sys_mem) {
// We would like to use ::mremap() to atomically replace the old anonymous
// memory with hugetlbfs backed memory, but mremap() does not support hugetlbfs
// (for no reason at all). So we must copy the anonymous memory to some other
// place, map hugetlbfs in place, and copy it back, without modifying it during
// the operation.
auto bytes = nr_pages * page_size;
auto old_mem = mem();
auto relocated_old_mem = mmap_anonymous(nullptr, bytes, PROT_READ|PROT_WRITE, MAP_PRIVATE);
std::memcpy(relocated_old_mem.get(), old_mem, bytes);
alloc_sys_mem({old_mem}, bytes).release();
std::memcpy(old_mem, relocated_old_mem.get(), bytes);
}
void cpu_pages::init_virt_to_phys_map() {
auto nr_entries = nr_pages / (huge_page_size / page_size);
virt_to_phys_map.resize(nr_entries);
auto fd = file_desc::open("/proc/self/pagemap", O_RDONLY | O_CLOEXEC);
for (size_t i = 0; i != nr_entries; ++i) {
uint64_t entry = 0;
auto phys = std::numeric_limits<physical_address>::max();
auto pfn = reinterpret_cast<uintptr_t>(mem() + i * huge_page_size) / page_size;
fd.pread(&entry, 8, pfn * 8);
assert(entry & 0x8000'0000'0000'0000);
phys = (entry & 0x007f'ffff'ffff'ffff) << page_bits;
virt_to_phys_map[i] = phys;
}
}
translation
cpu_pages::translate(const void* addr, size_t size) {
auto a = reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(mem());
auto pfn = a / huge_page_size;
if (pfn >= virt_to_phys_map.size()) {
return {};
}
auto phys = virt_to_phys_map[pfn];
if (phys == std::numeric_limits<physical_address>::max()) {
return {};
}
auto translation_size = align_up(a + 1, huge_page_size) - a;
size = std::min(size, translation_size);
phys += a & (huge_page_size - 1);
return translation{phys, size};
}
void cpu_pages::do_resize(size_t new_size, allocate_system_memory_fn alloc_sys_mem) {
auto new_pages = new_size / page_size;
if (new_pages <= nr_pages) {
return;
}
auto old_size = nr_pages * page_size;
auto mmap_start = memory + old_size;
auto mmap_size = new_size - old_size;
auto mem = alloc_sys_mem({mmap_start}, mmap_size);
mem.release();
::madvise(mmap_start, mmap_size, MADV_HUGEPAGE);
// one past last page structure is a sentinel
auto new_page_array_pages = align_up(sizeof(page[new_pages + 1]), page_size) / page_size;
auto new_page_array
= reinterpret_cast<page*>(allocate_large(new_page_array_pages));
if (!new_page_array) {
throw std::bad_alloc();
}
std::copy(pages, pages + nr_pages, new_page_array);
// mark new one-past-last page as taken to avoid boundary conditions
new_page_array[new_pages].free = false;
auto old_pages = reinterpret_cast<char*>(pages);
auto old_nr_pages = nr_pages;
auto old_pages_size = align_up(sizeof(page[nr_pages + 1]), page_size);
pages = new_page_array;
nr_pages = new_pages;
auto old_pages_start = (old_pages - memory) / page_size;
if (old_pages_start == 0) {
// keep page 0 allocated
old_pages_start = 1;
old_pages_size -= page_size;
}
free_span(old_pages_start, old_pages_size / page_size);
free_span(old_nr_pages, new_pages - old_nr_pages);
}
void cpu_pages::resize(size_t new_size, allocate_system_memory_fn alloc_memory) {
new_size = align_down(new_size, huge_page_size);
while (nr_pages * page_size < new_size) {
// don't reallocate all at once, since there might not
// be enough free memory available to relocate the pages array
auto tmp_size = std::min(new_size, 4 * nr_pages * page_size);
do_resize(tmp_size, alloc_memory);
}
}
reclaiming_result cpu_pages::run_reclaimers(reclaimer_scope scope) {
auto target = std::max(nr_free_pages + 1, min_free_pages);
reclaiming_result result = reclaiming_result::reclaimed_nothing;
while (nr_free_pages < target) {
bool made_progress = false;
++g_reclaims;
for (auto&& r : reclaimers) {
if (r->scope() >= scope) {
made_progress |= r->do_reclaim() == reclaiming_result::reclaimed_something;
}
}
if (!made_progress) {
return result;
}
result = reclaiming_result::reclaimed_something;
}
return result;
}
void cpu_pages::schedule_reclaim() {
current_min_free_pages = 0;
reclaim_hook([this] {
if (nr_free_pages < min_free_pages) {
try {
run_reclaimers(reclaimer_scope::async);
} catch (...) {
current_min_free_pages = min_free_pages;
throw;
}
}
current_min_free_pages = min_free_pages;
});
}
memory::memory_layout cpu_pages::memory_layout() {
assert(is_initialized());
return {
reinterpret_cast<uintptr_t>(memory),
reinterpret_cast<uintptr_t>(memory) + nr_pages * page_size
};
}
void cpu_pages::set_reclaim_hook(std::function<void (std::function<void ()>)> hook) {
reclaim_hook = hook;
current_min_free_pages = min_free_pages;
}
small_pool::small_pool(unsigned object_size) noexcept
: _object_size(object_size), _span_size(1) {
while (_object_size > span_bytes()
|| (_span_size < 32 && waste() > 0.05)
|| (span_bytes() / object_size < 32)) {
_span_size *= 2;
}
_max_free = std::max<unsigned>(100, span_bytes() * 2 / _object_size);
_min_free = _max_free / 2;
}
small_pool::~small_pool() {
_min_free = _max_free = 0;
trim_free_list();
}
// Should not throw in case of running out of memory to avoid infinite recursion,
// becaue throwing std::bad_alloc requires allocation. __cxa_allocate_exception
// falls back to the emergency pool in case malloc() returns nullptr.
void*
small_pool::allocate() {
if (!_free) {
add_more_objects();
}
if (!_free) {
return nullptr;
}
auto* obj = _free;
_free = _free->next;
--_free_count;
return obj;
}
void
small_pool::deallocate(void* object) {
auto o = reinterpret_cast<free_object*>(object);
o->next = _free;
_free = o;
++_free_count;
if (_free_count >= _max_free) {
trim_free_list();
}
}
void
small_pool::add_more_objects() {
auto goal = (_min_free + _max_free) / 2;
while (!_span_list.empty() && _free_count < goal) {
page& span = _span_list.front(cpu_mem.pages);
_span_list.pop_front(cpu_mem.pages);
while (span.freelist) {
auto obj = span.freelist;
span.freelist = span.freelist->next;
obj->next = _free;
_free = obj;
++_free_count;
++span.nr_small_alloc;
}
}
while (_free_count < goal) {
auto data = reinterpret_cast<char*>(cpu_mem.allocate_large(_span_size));
if (!data) {
return;
}
++_spans_in_use;
auto span = cpu_mem.to_page(data);
for (unsigned i = 0; i < _span_size; ++i) {
span[i].offset_in_span = i;
span[i].pool = this;
}
span->nr_small_alloc = 0;
span->freelist = nullptr;
for (unsigned offset = 0; offset <= span_bytes() - _object_size; offset += _object_size) {
auto h = reinterpret_cast<free_object*>(data + offset);
h->next = _free;
_free = h;
++_free_count;
++span->nr_small_alloc;
}
}
}
void
small_pool::trim_free_list() {
auto goal = (_min_free + _max_free) / 2;
while (_free && _free_count > goal) {
auto obj = _free;
_free = _free->next;
--_free_count;
page* span = cpu_mem.to_page(obj);
span -= span->offset_in_span;
if (!span->freelist) {
new (&span->link) page_list_link();
_span_list.push_front(cpu_mem.pages, *span);
}
obj->next = span->freelist;
span->freelist = obj;
if (--span->nr_small_alloc == 0) {
_span_list.erase(cpu_mem.pages, *span);
cpu_mem.free_span(span - cpu_mem.pages, span->span_size);
--_spans_in_use;
}
}
}
float small_pool::waste() {
return (span_bytes() % _object_size) / (1.0 * span_bytes());
}
void
abort_on_underflow(size_t size) {
if (std::make_signed_t<size_t>(size) < 0) {
// probably a logic error, stop hard
abort();
}
}
void* allocate_large(size_t size) {
abort_on_underflow(size);
unsigned size_in_pages = (size + page_size - 1) >> page_bits;
assert((size_t(size_in_pages) << page_bits) >= size);
return cpu_mem.allocate_large(size_in_pages);
}
void* allocate_large_aligned(size_t align, size_t size) {
abort_on_underflow(size);
unsigned size_in_pages = (size + page_size - 1) >> page_bits;
unsigned align_in_pages = std::max(align, page_size) >> page_bits;
return cpu_mem.allocate_large_aligned(align_in_pages, size_in_pages);
}
void free_large(void* ptr) {
return cpu_mem.free_large(ptr);
}
size_t object_size(void* ptr) {
return cpu_pages::all_cpus[object_cpu_id(ptr)]->object_size(ptr);
}
void* allocate(size_t size) {
++g_allocs;
if (size <= sizeof(free_object)) {
size = sizeof(free_object);
}
if (size <= max_small_allocation) {
return cpu_mem.allocate_small(size);
} else {
return allocate_large(size);
}
}
void* allocate_aligned(size_t align, size_t size) {
++g_allocs;
size = std::max(size, align);
if (size <= sizeof(free_object)) {
size = sizeof(free_object);
}
if (size <= max_small_allocation && align <= page_size) {
// Our small allocator only guarantees alignment for power-of-two
// allocations which are not larger than a page.
size = 1 << log2ceil(size);
return cpu_mem.allocate_small(size);
} else {
return allocate_large_aligned(align, size);
}
}
void free(void* obj) {
++g_frees;
cpu_mem.free(obj);
}
void free(void* obj, size_t size) {
++g_frees;
cpu_mem.free(obj, size);
}
void shrink(void* obj, size_t new_size) {
++g_frees;
++g_allocs; // keep them balanced
cpu_mem.shrink(obj, new_size);
}
void set_reclaim_hook(std::function<void (std::function<void ()>)> hook) {
cpu_mem.set_reclaim_hook(hook);
}
reclaimer::reclaimer(reclaim_fn reclaim, reclaimer_scope scope)
: _reclaim(std::move(reclaim))
, _scope(scope) {
cpu_mem.reclaimers.push_back(this);
}
reclaimer::~reclaimer() {
auto& r = cpu_mem.reclaimers;
r.erase(std::find(r.begin(), r.end(), this));
}
void configure(std::vector<resource::memory> m,