-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmm.cpp
1655 lines (1335 loc) · 49.6 KB
/
mm.cpp
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
//
// PTLsim: Cycle Accurate x86-64 Simulator
// Memory Management
//
// Copyright 2000-2008 Matt T. Yourst <yourst@yourst.com>
//
#include <globals.h>
#ifdef PTLSIM_HYPERVISOR
#include <ptlxen.h>
#else
#include <kernel.h>
#endif
#include <mm.h>
#include <datastore.h>
#include <mm-private.h>
extern ostream logfile;
extern void early_printk(const char* s);
#define ENABLE_MM_LOGGING
static const int mm_event_buffer_size = 16384; // i.e. 256 KB
#ifdef ENABLE_MM_LOGGING
MemoryManagerEvent mm_event_buffer[mm_event_buffer_size];
#endif
MemoryManagerEvent* mm_event_buffer_head = null;
MemoryManagerEvent* mm_event_buffer_tail = null;
MemoryManagerEvent* mm_event_buffer_end = null;
int mm_logging_fd = -1;
bool enable_inline_mm_logging = false;
bool enable_mm_validate = false;
void ptl_mm_set_logging(const char* mm_log_filename, int mm_event_buffer_size, bool set_enable_inline_mm_logging) {
//
// The event buffer itself is allocated at boot time and set up in ptl_mm_init().
// This just changes the output file for the first time.
//
if (mm_logging_fd < 0) {
if (mm_log_filename) {
mm_logging_fd = sys_open(mm_log_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else {
mm_event_buffer_head = null;
mm_event_buffer_tail = null;
mm_event_buffer_end = null;
}
}
//
// Flush any entries that have accumulated since booting
//
ptl_mm_flush_logging();
enable_inline_mm_logging = set_enable_inline_mm_logging;
}
void ptl_mm_set_validate(bool set_enable_mm_validate) {
enable_mm_validate = set_enable_mm_validate;
}
void ptl_mm_flush_logging() {
if likely (!mm_event_buffer_head) return;
if unlikely (mm_logging_fd < 0) return;
int count = mm_event_buffer_tail - mm_event_buffer_head;
int bytes = count * sizeof(MemoryManagerEvent);
assert(sys_write(mm_logging_fd, mm_event_buffer_head, bytes) == bytes);
mm_event_buffer_tail = mm_event_buffer_head;
}
void ptl_mm_add_event(int event, int pool, void* caller, void* address, W32 bytes, int slab = 0) {
if likely (!mm_event_buffer_head) return;
MemoryManagerEvent* e = mm_event_buffer_tail;
e->event = event;
e->pool = pool;
e->caller = LO32(Waddr(caller));
e->address = LO32(Waddr(address));
e->bytes = bytes;
e->slab = slab;
if unlikely (enable_inline_mm_logging) {
cerr << "mm: ", *e, endl;
}
mm_event_buffer_tail++;
if unlikely (mm_event_buffer_tail >= mm_event_buffer_end) ptl_mm_flush_logging();
}
//
// Which pages are mapped to slab cache pages?
//
// We need to know this to correctly free objects
// allocated at arbitrary addresses.
//
// The full 4 GB address space is covered by 1048576
// bits, or 131072 bytes in the bitmap
//
bitvec<PTL_PAGE_POOL_SIZE> page_is_slab_bitmap;
struct AddressSizeSpan {
void* address;
Waddr size;
AddressSizeSpan() { }
AddressSizeSpan(void* address, Waddr size) {
this->address = address;
this->size = size;
}
};
//
// Memory manager validation (for debugging only)
//
bool print_validate_errors = 1;
void* object_of_interest = 0;
bool is_inside_ptlsim(const void* pp) {
#ifdef PTLSIM_HYPERVISOR
Waddr p = Waddr(pp);
if likely (inrange(p, Waddr(PTLSIM_VIRT_BASE + (4096 * PTLSIM_FIRST_READ_ONLY_PAGE)), Waddr(bootinfo.heap_end)-1)) return true;
if likely (!p) return true;
if likely (print_validate_errors) {
cerr << "Error: pointer ", (void*)p, " is not in PTL space from ", bootinfo.heap_start, "-", bootinfo.heap_end, " (called from ", getcaller(), "); object of interest = ", object_of_interest, endl, flush;
logfile << "Error: pointer ", (void*)p, " is not in PTL space from ", bootinfo.heap_start, "-", bootinfo.heap_end, " (called from ", getcaller(), "); object of interest = ", object_of_interest, endl, flush;
}
return false;
#else
return true;
#endif
}
bool is_inside_ptlsim_heap(const void* pp) {
#ifdef PTLSIM_HYPERVISOR
Waddr p = Waddr(pp);
if likely (inrange(p, Waddr(bootinfo.heap_start), Waddr(bootinfo.heap_end)-1)) return true;
if likely (!p) return true;
if likely (print_validate_errors) {
cerr << "Error: pointer ", (void*)p, " is not in PTL heap from ", bootinfo.heap_start, "-", bootinfo.heap_end, " (called from ", getcaller(), "); object of interest = ", object_of_interest, endl, flush;
logfile << "Error: pointer ", (void*)p, " is not in PTL heap from ", bootinfo.heap_start, "-", bootinfo.heap_end, " (called from ", getcaller(), "); object of interest = ", object_of_interest, endl, flush;
}
return false;
#else
return true;
#endif
}
//
// Extent Allocator, suitable for use as a memory allocator
// at both the page level and sub-block level.
//
// This allocator requires no overhead on allocated blocks,
// however, when freeing blocks, the number of bytes to free
// must be explicitly specified.
//
// Blocks can be freed from the middle of an allocated
// region if desired, but the range of the free must not
// overlap an area that is already free.
//
// The parameters CHUNKSIZE, SIZESLOTS and HASHSLOTS must
// be a power of two.
//
// Allocations smaller than one chunk are rounded up to a
// chunk, i.e. CHUNKSIZE is the minimum granularity. The
// chunk size must be at least 32 bytes on 32-bit platforms
// and 64 bytes on 64-bit platforms, to fit the tracking
// structures in the smallest empty block size.
//
// To initialize the allocator, use free() to add a large
// initial pool of storage to it.
//
template <int CHUNKSIZE, int SIZESLOTS, int HASHSLOTS>
struct ExtentAllocator {
struct FreeExtentBase {
selflistlink sizelink;
selflistlink startaddrlink;
selflistlink endaddrlink;
};
struct FreeExtent: public FreeExtentBase {
Waddr size;
byte padding[CHUNKSIZE - (sizeof(FreeExtentBase) + sizeof(Waddr))];
static FreeExtent* sizelink_to_self(selflistlink* link) { return (link) ? (FreeExtent*)(link - 0) : null; }
static FreeExtent* startaddrlink_to_self(selflistlink* link) { return (link) ? (FreeExtent*)(link - 1) : null; }
static FreeExtent* endaddrlink_to_self(selflistlink* link) { return (link) ? (FreeExtent*)(link - 2) : null; }
ostream& print(ostream& os) {
return os << this, ": size ", intstring(size, 7), " = ", intstring(size * CHUNKSIZE, 10),
" bytes (range ", (void*)this, " to ", (void*)(this + size), "); sizelink ", FreeExtentBase::sizelink, ", startaddrlink ", FreeExtentBase::startaddrlink,
", endaddrlink ", FreeExtentBase::endaddrlink;
}
};
int extent_size_to_slot(size_t size) const {
return min(size-1, (size_t)(SIZESLOTS-1));
}
int addr_to_hash_slot(void* addr) const {
W64 key = (Waddr(addr) >> log2(CHUNKSIZE));
return foldbits<log2(HASHSLOTS)>(key);
}
selflistlink* free_extents_by_size[SIZESLOTS];
selflistlink* free_extents_by_startaddr_hash[HASHSLOTS];
selflistlink* free_extents_by_endaddr_hash[HASHSLOTS];
int extent_count;
W64 current_bytes_free;
W64 current_bytes_allocated;
W64 peak_bytes_allocated;
W64 allocs;
W64 frees;
W64 extents_reclaimed;
W64 extent_reclaim_reqs;
W64 chunks_allocated;
void reset() {
extent_count = 0;
foreach (i, SIZESLOTS) {
free_extents_by_size[i] = null;
}
foreach (i, HASHSLOTS) {
free_extents_by_startaddr_hash[i] = null;
}
foreach (i, HASHSLOTS) {
free_extents_by_endaddr_hash[i] = null;
}
current_bytes_free = 0;
current_bytes_allocated = 0;
peak_bytes_allocated = 0;
allocs = 0;
frees = 0;
extents_reclaimed = 0;
extent_reclaim_reqs = 0;
chunks_allocated = 0;
}
FreeExtent* find_extent_by_startaddr(void* addr) const {
int slot = addr_to_hash_slot(addr);
FreeExtent* r = FreeExtent::startaddrlink_to_self(free_extents_by_startaddr_hash[slot]);
while (r) {
if unlikely (r == (FreeExtent*)addr) return r;
r = FreeExtent::startaddrlink_to_self(r->startaddrlink.next);
}
return null;
}
FreeExtent* find_extent_by_endaddr(void* addr) const {
int slot = addr_to_hash_slot(addr);
FreeExtent* r = FreeExtent::endaddrlink_to_self(free_extents_by_endaddr_hash[slot]);
while (r) {
if unlikely ((r + r->size) == (FreeExtent*)addr) return r;
r = FreeExtent::endaddrlink_to_self(r->endaddrlink.next);
}
return null;
}
void alloc_extent(FreeExtent* r) {
bool DEBUG = 0;
if (DEBUG) logfile << "alloc_extent(", r, "): sizelink ", r->sizelink, ", startaddrlink ", r->startaddrlink, ", endaddrlink ", r->endaddrlink, ", extent_count ", extent_count, endl;
current_bytes_free -= (r->size * CHUNKSIZE);
r->sizelink.unlink();
r->startaddrlink.unlink();
r->endaddrlink.unlink();
extent_count--;
assert(extent_count >= 0);
}
FreeExtent* find_extent_in_size_slot(size_t size, int sizeslot) {
bool DEBUG = 0;
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[sizeslot]);
if (r) {
if (DEBUG) cerr << "find_extent_in_size_slot(size ", size, ", slot ", sizeslot, "): r = ", r, endl;
}
while (r) {
if likely (r->size < size) {
if (DEBUG) cerr << " ", r, " too small: only ", r->size, " chunks", endl;
r = FreeExtent::sizelink_to_self(r->sizelink.next);
continue;
}
alloc_extent(r);
if likely (size == r->size) {
if (DEBUG) cerr << " Exact match: ", r, endl;
return r;
}
int remaining_size = r->size - size;
FreeExtent* rsplit = r + size;
if (DEBUG) cerr << "rsplit = ", rsplit, ", size ", size, ", r->size = ", r->size, ", remaining_size = ", remaining_size, endl, flush;
free_extent(rsplit, remaining_size);
return r;
}
return null;
}
FreeExtent* find_free_extent_of_size(size_t size) {
if unlikely (!size) return null;
for (int i = extent_size_to_slot(size); i < SIZESLOTS; i++) {
FreeExtent* r = find_extent_in_size_slot(size, i);
if unlikely (r) return r;
}
return null;
}
void free_extent(FreeExtent* r, size_t size) {
static const bool DEBUG = 0;
if unlikely ((!r) | (!size)) return;
//
// <F1> AAA [now-F] AA <F2>
//
// Need to quickly find F1 and F2
//
if (DEBUG) {
cout << "free_extent(", r, ", ", size, "): from ", r, " to ", (r + size), endl, flush;
cout << " Add to size slot ", extent_size_to_slot(size), " @ ", &free_extents_by_size[extent_size_to_slot(size)], endl, flush;
cout << " Add to startaddr slot ", addr_to_hash_slot(r), " @ ", &free_extents_by_startaddr_hash[addr_to_hash_slot(r)], endl, flush;
cout << " Add to endaddr slot ", addr_to_hash_slot(r + r->size), " @ ", &free_extents_by_endaddr_hash[addr_to_hash_slot(r + r->size)], endl, flush;
}
r->sizelink.reset();
r->startaddrlink.reset();
r->size = 0;
current_bytes_free += (size * CHUNKSIZE);
FreeExtent* right = find_extent_by_startaddr(r + size);
//
// Try to merge with right extent if possible
//
if unlikely (right) {
right->sizelink.unlink();
right->startaddrlink.unlink();
right->endaddrlink.unlink();
size = size + right->size;
if (DEBUG) cout << " Merge with right extent ", right, " of size ", right->size, " to form new extent of total size ", size, endl;
extent_count--;
assert(extent_count >= 0);
}
FreeExtent* left = find_extent_by_endaddr(r);
if likely (left) {
left->sizelink.unlink();
left->startaddrlink.unlink();
left->endaddrlink.unlink();
size = size + left->size;
r -= left->size;
if (DEBUG) cout << " Merge with left extent ", left, " of size ", left->size, " to form new extent of total size ", size, endl;
extent_count--;
assert(extent_count >= 0);
}
r->size = size;
r->sizelink.addto(free_extents_by_size[extent_size_to_slot(size)]);
r->startaddrlink.addto(free_extents_by_startaddr_hash[addr_to_hash_slot(r)]);
r->endaddrlink.addto(free_extents_by_endaddr_hash[addr_to_hash_slot(r + r->size)]);
extent_count++;
assert(extent_count > 0);
}
void* alloc(size_t size) {
bool DEBUG = 0;
if unlikely (enable_mm_validate) fast_validate();
if (DEBUG) cerr << "ExtentAllocator<", CHUNKSIZE, ">::alloc(", size, ")", endl, flush;
size = ceil(size, CHUNKSIZE) >> log2(CHUNKSIZE);
void* addr = (void*)find_free_extent_of_size(size);
if (DEBUG) cerr << "ExtentAllocator<", CHUNKSIZE, ">::alloc(", size, "): found free extent ", addr, endl, flush;
if unlikely (!addr) return null;
allocs++;
current_bytes_allocated += (size * CHUNKSIZE);
peak_bytes_allocated = max(peak_bytes_allocated, current_bytes_allocated);
if unlikely (enable_mm_validate) fast_validate();
return addr;
}
void free(void* p, size_t size) {
if unlikely (enable_mm_validate) fast_validate();
size = ceil(size, CHUNKSIZE) >> log2(CHUNKSIZE);
free_extent((FreeExtent*)p, size);
frees++;
current_bytes_allocated = min(current_bytes_allocated - (size * CHUNKSIZE), 0ULL);
if unlikely (enable_mm_validate) fast_validate();
}
void add_to_free_pool(void* p, size_t size) {
chunks_allocated++;
size = ceil(size, CHUNKSIZE) >> log2(CHUNKSIZE);
free_extent((FreeExtent*)p, size);
}
int reclaim_unused_extents(AddressSizeSpan* ass, int asscount, int sizealign) {
static const int DEBUG = 0;
int minchunks = ceil(sizealign, CHUNKSIZE) >> log2(CHUNKSIZE);
int n = 0;
extent_reclaim_reqs++;
for (int i = extent_size_to_slot(minchunks); i < SIZESLOTS; i++) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
while (r) {
//
// Example:
//
// ..ffffff ffffffff fff.....
// aaaaaa aaaaaaaa aaa.....
// ffffff fff
// -return-
//
Waddr rstart = (Waddr)r;
Waddr rend = rstart + (r->size * CHUNKSIZE);
Waddr first_full_page = ceil(rstart, sizealign);
Waddr last_full_page = floor(rend, sizealign);
Waddr bytes_in_middle = last_full_page - first_full_page;
if (DEBUG) {
cout << " Trying to reclaim extent "; r->print(cout); cout << " (", bytes_in_middle, " bytes in middle)", endl;
}
if likely (!bytes_in_middle) {
r = FreeExtent::sizelink_to_self(r->sizelink.next);
continue;
}
// These are full pages that we can return to the system
if unlikely (n == asscount) return n;
Waddr full_page_bytes = last_full_page - first_full_page;
if (DEBUG) cout << " Adding reclaimed full page extent at ", (void*)first_full_page, " of ", full_page_bytes, " bytes (", full_page_bytes / sizealign, " pages)", endl;
ass[n++] = AddressSizeSpan((void*)first_full_page, full_page_bytes);
Waddr bytes_at_end_of_first_page = ceil(rstart, sizealign) - rstart;
Waddr bytes_at_start_of_last_page = rend - floor(rend, sizealign);
alloc_extent(r);
extents_reclaimed++;
if unlikely (bytes_at_end_of_first_page) {
if (DEBUG) cout << " Adding ", bytes_at_end_of_first_page, " bytes at end of first page @ ", r, " back to free pool", endl;
free(r, bytes_at_end_of_first_page);
}
if unlikely (bytes_at_start_of_last_page) {
void* p = (void*)(rend - bytes_at_start_of_last_page);
if (DEBUG) cout << " Adding ", bytes_at_start_of_last_page, " bytes at start of last page @ ", p, " back to free pool", endl;
free(p, bytes_at_start_of_last_page);
}
//
// Start again since we may have invalidated the next entry,
// or moved some big entries in the current list back into
// one of the smaller lists (smaller than the page size)
//
r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
}
}
return n;
}
bool fast_validate() {
int n = 0;
foreach (i, SIZESLOTS) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
if (!r) continue;
assert(is_inside_ptlsim(r));
while (r) {
object_of_interest = r;
assert(is_inside_ptlsim(r));
assert(is_inside_ptlsim(r->sizelink.prev));
assert(is_inside_ptlsim_heap(r->sizelink.next));
assert(is_inside_ptlsim(r->startaddrlink.prev));
assert(is_inside_ptlsim_heap(r->startaddrlink.next));
assert(is_inside_ptlsim(r->endaddrlink.prev));
assert(is_inside_ptlsim_heap(r->endaddrlink.next));
r = FreeExtent::sizelink_to_self(r->sizelink.next);
}
}
foreach (i, HASHSLOTS) {
selflistlink* link;
link = free_extents_by_startaddr_hash[i];
while (link) {
assert(is_inside_ptlsim(link));
assert(is_inside_ptlsim(link->prev));
assert(is_inside_ptlsim_heap(link->next));
link = link->next;
}
link = free_extents_by_endaddr_hash[i];
while (link) {
assert(is_inside_ptlsim(link));
assert(is_inside_ptlsim(link->prev));
assert(is_inside_ptlsim_heap(link->next));
link = link->next;
}
}
return true;
}
bool full_validate() {
// Collect all regions
FreeExtent** extarray = new FreeExtent*[extent_count];
int n = 0;
foreach (i, SIZESLOTS) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
if (!r) continue;
assert(is_inside_ptlsim_heap(r));
while (r) {
if (n >= extent_count) {
cerr << "ERROR (chunksize ", CHUNKSIZE, "): ", n, " exceeds extent count ", extent_count, endl, flush;
cerr << *this;
return false;
}
extarray[n++] = r;
r = FreeExtent::sizelink_to_self(r->sizelink.next);
}
}
foreach (i, extent_count) {
FreeExtent* r = extarray[i];
Waddr start = (Waddr)r;
Waddr end = start + (r->size * CHUNKSIZE);
foreach (j, extent_count) {
if (j == i) continue;
FreeExtent* rr = extarray[j];
Waddr rrstart = (Waddr)rr;
Waddr rrend = rrstart + (rr->size * CHUNKSIZE);
// ........rrrrrrrrrrr............
// .....ssssssss..................
if (inrange(start, rrstart, rrend-1) | inrange(end, rrstart, rrend-1)) {
cerr << "ERROR (chunksize ", CHUNKSIZE, "): overlap between extent ", r, " (", (r->size * CHUNKSIZE), " bytes; ", (void*)start, " to ", (void*)end, ") "
"and extent ", rr, " (", (rr->size * CHUNKSIZE), " bytes; ", (void*)rrstart, " to ", (void*)rrend, ")", endl, flush;
cerr << *this;
return false;
}
}
}
delete[] extarray;
return true;
}
size_t largest_free_extent_bytes() const {
for (int i = SIZESLOTS-1; i >= 0; i--) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
if likely (!r) continue;
return ((i+1) * CHUNKSIZE);
}
return 0;
}
size_t total_free_bytes() const {
W64 real_free_bytes = 0;
foreach (i, SIZESLOTS) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
if likely (!r) continue;
while (r) {
real_free_bytes += r->size * CHUNKSIZE;
r = FreeExtent::sizelink_to_self(r->sizelink.next);
}
}
if (real_free_bytes != current_bytes_free) {
logfile << "WARNING: real_free_bytes = ", real_free_bytes, " but current_bytes_free ", current_bytes_free, endl;
}
return current_bytes_free;
}
ostream& print(ostream& os) const {
os << "ExtentAllocator<", CHUNKSIZE, ", ", SIZESLOTS, ", ", HASHSLOTS, ">: ", extent_count, " extents:", endl;
os << "Extents by size:", endl;
W64 total_bytes = 0;
foreach (i, SIZESLOTS) {
FreeExtent* r = FreeExtent::sizelink_to_self(free_extents_by_size[i]);
if (!r) continue;
os << " Size slot ", intstring(i, 7), " = ", intstring((i+1) * CHUNKSIZE, 10), " bytes (root @ ", &free_extents_by_size[i], "):", endl;
W64 min_expected_bytes = ((i+1) * CHUNKSIZE);
while (r) {
os << " ";
r->print(os);
os << endl;
W64 bytes = (r->size * CHUNKSIZE);
assert(bytes >= min_expected_bytes);
total_bytes += bytes;
r = FreeExtent::sizelink_to_self(r->sizelink.next);
}
}
os << " Total free bytes: ", total_bytes, endl;
os << endl;
os << "Extents by startaddr hash:", endl;
foreach (i, HASHSLOTS) {
FreeExtent* r = FreeExtent::startaddrlink_to_self(free_extents_by_startaddr_hash[i]);
if (!r) continue;
os << " Hash slot ", intstring(i, 7), ":", endl;
while (r) {
os << " ";
os << r->print(os);
os << endl;
r = FreeExtent::startaddrlink_to_self(r->startaddrlink.next);
}
}
os << endl;
os << "Extents by endaddr hash:", endl;
foreach (i, HASHSLOTS) {
FreeExtent* r = FreeExtent::endaddrlink_to_self(free_extents_by_endaddr_hash[i]);
if (!r) continue;
os << " Hash slot ", intstring(i, 7), ":", endl;
while (r) {
os << " ";
os << r->print(os);
os << endl;
r = FreeExtent::endaddrlink_to_self(r->endaddrlink.next);
}
}
os << endl;
return os;
}
DataStoreNode& capture_stats(DataStoreNode& root) {
root.add("current-bytes-allocated", current_bytes_allocated);
root.add("peak-bytes-allocated", peak_bytes_allocated);
root.add("allocs", allocs);
root.add("frees", frees);
root.add("extents-reclaimed", extents_reclaimed);
root.add("extent-reclaim-reqs", extent_reclaim_reqs);
root.add("chunks-allocated", chunks_allocated);
return root;
}
};
template <int CHUNKSIZE, int SIZESLOTS, int HASHSLOTS>
ostream& operator <<(ostream& os, const ExtentAllocator<CHUNKSIZE, SIZESLOTS, HASHSLOTS>& alloc) {
return alloc.print(os);
}
//
// Slab cache allocator
//
// Minimum object size is 16 bytes (for 256 objects per page)
//
struct SlabAllocator;
ostream& operator <<(ostream& os, const SlabAllocator& slaballoc);
//
// In PTLxen, the hypervisor is running on the bare hardware
// and must handle all page allocation itself.
//
// In userspace PTLsim, we use pagealloc only for its
// statistics counters.
//
ExtentAllocator<4096, 512, 512> pagealloc;
static const W32 SLAB_PAGE_MAGIC = 0x42414c53; // 'SLAB'
struct SlabAllocator {
#ifdef __x86_64__
// 64-bit x86-64: 8 + 8 = 16 bytes
struct FreeObjectHeader: public selflistlink { };
#else
// 32-bit x86: 4 + 4 + 8 = 16 bytes
struct FreeObjectHeader: public selflistlink { W64 pad; };
#endif
static const int GRANULARITY = sizeof(FreeObjectHeader);
// 32 bytes on both x86-64 and 32-bit x86
struct PageHeader {
selflistlink link;
FreeObjectHeader* freelist;
SlabAllocator* allocator;
W32s freecount;
W32 magic;
FreeObjectHeader* getbase() const { return (FreeObjectHeader*)floor(Waddr(this), PAGE_SIZE); }
static PageHeader* headerof(void* p) {
PageHeader* h = (PageHeader*)(floor(Waddr(p), PAGE_SIZE) + PAGE_SIZE - sizeof(PageHeader));
return h;
}
bool validate() {
int n = 0;
selflistlink* link = freelist;
int objsize = allocator->objsize;
while (link) {
assert(inrange(Waddr(link), floor(Waddr(this), 4096), floor(Waddr(this), 4096) + 4095));
assert(((Waddr(link) - floor(Waddr(this), 4096)) % objsize) == 0);
assert(is_inside_ptlsim_heap(link));
assert(is_inside_ptlsim_heap(link->prev));
assert(is_inside_ptlsim_heap(link->next));
n++;
link = link->next;
}
assert(n == freecount);
return true;
}
};
W64 current_objs_allocated;
W64 peak_objs_allocated;
W64 current_bytes_allocated;
W64 peak_bytes_allocated;
W64 current_pages_allocated;
W64 peak_pages_allocated;
W64 allocs;
W64 frees;
W64 page_allocs;
W64 page_frees;
W64 reclaim_reqs;
PageHeader* free_pages;
PageHeader* partial_pages;
PageHeader* full_pages;
W16s free_page_count;
W16 objsize;
W16 max_objects_per_page;
W16 padding;
static const int FREE_PAGE_HI_THRESH = 4;
static const int FREE_PAGE_LO_THRESH = 1;
SlabAllocator() { }
void reset(int objsize) {
free_pages = null;
partial_pages = null;
full_pages = null;
free_page_count = 0;
this->objsize = objsize;
max_objects_per_page = ((PAGE_SIZE - sizeof(PageHeader)) / objsize);
current_objs_allocated = 0;
peak_objs_allocated = 0;
current_bytes_allocated = 0;
peak_bytes_allocated = 0;
current_pages_allocated = 0;
peak_pages_allocated = 0;
allocs = 0;
frees = 0;
page_allocs = 0;
page_frees = 0;
reclaim_reqs = 0;
}
static SlabAllocator* pointer_to_slaballoc(void* p) {
Waddr pfn = (((Waddr)p) - PTL_PAGE_POOL_BASE) >> 12;
if (pfn >= PTL_PAGE_POOL_SIZE) return null; // must be some other kind of page
if (!page_is_slab_bitmap[pfn]) return null;
PageHeader* page = PageHeader::headerof(p);
if unlikely (page->magic != SLAB_PAGE_MAGIC) {
cerr << "Slab corruption: p = ", p, ", h = ", page, ", allocator ", page->allocator, ", magic = 0x", hexstring(page->magic, 32), ", caller ", getcaller(), endl, flush;
assert(false);
}
return page->allocator;
}
FreeObjectHeader* alloc_from_page(PageHeader* page) {
FreeObjectHeader* obj = page->freelist;
if unlikely (!obj) return obj;
obj->unlink();
assert(page->freecount > 0);
page->freecount--;
if unlikely (!page->freelist) {
assert(page->freecount == 0);
page->link.unlink();
page->link.addto((selflistlink*&)full_pages);
}
allocs++;
current_objs_allocated++;
peak_objs_allocated = max(current_objs_allocated, peak_objs_allocated);
current_bytes_allocated += objsize;
peak_bytes_allocated = max(current_bytes_allocated, peak_bytes_allocated);
return obj;
}
PageHeader* alloc_new_page() {
//
// We need the pages in the low 2 GB of the address space so we can use
// page_is_slab_bitmap to find out if it's a slab or genalloc page:
//
byte* rawpage = (byte*)ptl_mm_alloc_private_32bit_page();
if unlikely (!rawpage) return null;
PageHeader* page = PageHeader::headerof(rawpage);
page_allocs++;
current_pages_allocated++;
peak_pages_allocated = max(current_pages_allocated, peak_pages_allocated);
page->magic = SLAB_PAGE_MAGIC;
page->link.reset();
page->freelist = null;
page->freecount = 0;
page->allocator = this;
Waddr pfn = (((Waddr)page) - PTL_PAGE_POOL_BASE) >> 12;
assert(pfn < PTL_PAGE_POOL_SIZE);
page_is_slab_bitmap[pfn] = 1;
FreeObjectHeader* obj = page->getbase();
FreeObjectHeader* prevobj = (FreeObjectHeader*)&page->freelist;
foreach (i, max_objects_per_page) {
prevobj->next = obj;
obj->prev = prevobj;
obj->next = null;
prevobj = obj;
obj = (FreeObjectHeader*)(((byte*)obj) + objsize);
}
page->freecount = max_objects_per_page;
return page;
}
void* alloc() {
if unlikely (enable_mm_validate) validate();
PageHeader* page = (partial_pages) ? partial_pages : free_pages;
if unlikely (!page) {
page = alloc_new_page();
assert(page);
page->link.addto((selflistlink*&)partial_pages);
}
if likely (page == free_pages) {
page->link.unlink();
page->link.addto((selflistlink*&)partial_pages);
free_page_count--;
assert(free_page_count >= 0);
}
FreeObjectHeader* obj = alloc_from_page(page);
assert(obj);
return (void*)obj;
}
void free(void* p) {
if unlikely (enable_mm_validate) validate();
frees++;
if likely (current_objs_allocated > 0) current_objs_allocated--;
current_bytes_allocated -= min((W64)objsize, current_bytes_allocated);
FreeObjectHeader* obj = (FreeObjectHeader*)p;
PageHeader* page = PageHeader::headerof(p);
// assert(((Waddr(p) - Waddr(page->getbase())) % objsize) == 0);
bool page_was_previously_full = (page->freecount == 0);
obj->reset();
obj->addto((selflistlink*&)page->freelist);
assert(page->freecount <= max_objects_per_page);
page->freecount++;
if unlikely (page->freecount == max_objects_per_page) {
page->link.unlink();
page->link.addto((selflistlink*&)free_pages);
free_page_count++;
} else if unlikely (page_was_previously_full) {
page->link.unlink();
page->link.addto((selflistlink*&)partial_pages);
}
if (free_page_count >= FREE_PAGE_HI_THRESH) {
reclaim(FREE_PAGE_LO_THRESH);
}
if unlikely (enable_mm_validate) validate();
}
int reclaim(int limit = 0) {
// Return some of the pages to the main allocator all at once
int n = 0;
reclaim_reqs++;
while (free_page_count > limit) {
PageHeader* page = free_pages;
if (!page) break;
assert(page->freecount == max_objects_per_page);
page->link.unlink();
Waddr pfn = (Waddr(page->getbase()) - PTL_PAGE_POOL_BASE) >> 12;
assert(pfn < PTL_PAGE_POOL_SIZE);
page_is_slab_bitmap[pfn] = 0;
page->magic = 0;
ptl_mm_free_private_page((void*)page->getbase());
page_frees++;
if likely (current_pages_allocated > 0) current_pages_allocated--;
n++;
free_page_count--;
}
return n;
}
ostream& print_page_chain(ostream& os, PageHeader* page) const {
while (page) {
bitvec<4096/16> occupied_slots;
occupied_slots++;
FreeObjectHeader* pagebase = page->getbase();
os << " Page ", page, ": free list size ", page->freecount, ", prev ", page->link.prev, ", next ", page->link.next, ":", endl;
FreeObjectHeader* obj = page->freelist;
int c = 0;
while (obj) {
int slot = (Waddr(obj) - Waddr(pagebase)) / objsize;
os << " Free object ", obj, " in slot ", slot, " (prev ", obj->prev, ", next ", obj->next, ")", endl;
assert(inrange(slot, 0, int(max_objects_per_page)));
occupied_slots[slot] = 0;
obj = (FreeObjectHeader*)obj->next;
c++;
}
if (c != page->freecount) {
os << " WARNING: c = ", c, " vs page->freecount ", page->freecount, endl, flush;
}
foreach (i, max_objects_per_page) {