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
//! Marking phase of the GC.
//!
//! Implements simple marking algorithm that uses a work stealing queue to distribute marking work between worker threads.
//! Marking tasks can be terminated if GC is cancelled which allows us to start STW GC as soon as possible.
use crossbeam_deque::{Injector, Steal, Stealer, Worker};
use rand::distributions::{Distribution, Uniform};
use rand::thread_rng;
use std::mem::size_of;
use std::ptr::null;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use crate::sync::suspendible_thread_set::SuspendibleThreadSetLeaver;
use crate::system::object::HeapObjectHeader;
use crate::system::traits::Visitor;
use super::heap::{heap, Heap};
use super::marking_context::MarkingContext;
use super::thread::Thread;
pub struct Terminator {
const_nworkers: usize,
nworkers: AtomicUsize,
}
impl Terminator {
pub fn new(number_workers: usize) -> Terminator {
Terminator {
const_nworkers: number_workers,
nworkers: AtomicUsize::new(number_workers),
}
}
pub fn try_terminate(&self) -> bool {
if self.const_nworkers == 1 {
return true;
}
if self.decrease_workers() {
// reached 0, no need to wait
return true;
}
thread::sleep(Duration::from_micros(1));
self.zero_or_increase_workers()
}
fn decrease_workers(&self) -> bool {
self.nworkers.fetch_sub(1, Ordering::Relaxed) == 1
}
fn zero_or_increase_workers(&self) -> bool {
let mut nworkers = self.nworkers.load(Ordering::Relaxed);
loop {
if nworkers == 0 {
return true;
}
let result = self.nworkers.compare_exchange(
nworkers,
nworkers + 1,
Ordering::Relaxed,
Ordering::Relaxed,
);
match result {
Ok(_) => {
// Value was successfully increased again, workers didn't terminate in
// time. There is still work left.
return false;
}
Err(prev_nworkers) => {
nworkers = prev_nworkers;
}
}
}
}
}
type Address = usize;
pub struct MarkingTask<'a> {
task_id: usize,
visitor: &'a mut SlotVisitor,
terminator: &'a Terminator,
pub(crate) heap: &'static Heap,
pub(crate) mark_ctx: &'static MarkingContext,
}
impl<'a> MarkingTask<'a> {
pub fn new(
task_id: usize,
terminator: &'a Terminator,
heap: &'static Heap,
mark_ctx: &'static MarkingContext,
) -> MarkingTask<'a> {
MarkingTask {
task_id,
visitor: unsafe { &mut *(mark_ctx.mark_queues().visitor(task_id)) },
terminator,
heap,
mark_ctx,
}
}
pub fn run<const CANCELLABLE: bool>(&mut self) {
let stride = self.heap.options().mark_loop_stride;
loop {
if CANCELLABLE && self.heap.check_cancelled_gc_and_yield(true) {
break;
}
let mut work = 0;
for _ in 0..stride {
if let Some(task) = self.visitor.pop() {
unsafe {
self.do_task(task);
}
work += 1;
} else {
break;
}
}
if work == 0 {
let stsl = SuspendibleThreadSetLeaver::new(CANCELLABLE);
if self.terminator.try_terminate() {
break;
}
drop(stsl);
continue;
}
}
}
unsafe fn do_task(&mut self, task: MarkTask) {
let obj = task.obj();
if task.is_not_chunked() {
if !(*obj).vtable().varsize.is_varsize {
(*obj).visit(self.visitor);
} else if (*obj).vtable().varsize.is_varsize {
self.do_chunked_array_start(obj);
} else {
// primitive array or no heap pointers
}
} else {
self.do_chunked_array(obj, task.chunk() as _, task.pow() as _);
}
}
unsafe fn do_chunked_array_start(&mut self, obj: *mut HeapObjectHeader) {
let len = (*obj).array_length();
((*obj).vtable().trace)(obj.add(1).cast(), self.visitor);
if let Some(iter) = (*obj).vtable().varsize.iterate_range {
if len <= 2048 * 2 {
// a few slices only, process directly
iter(obj.add(1).cast(), 0, len, self.visitor);
} else {
let mut bits = log2i_graceful(len);
// Compensate for non-power-of-two arrays, cover the array in excess:
if len as isize != (1 << bits) {
bits += 1;
}
// Only allow full chunks on the queue. This frees do_chunked_array() from checking from/to
// boundaries against array->length(), touching the array header on every chunk.
//
// To do this, we cut the prefix in full-sized chunks, and submit them on the queue.
// If the array is not divided in chunk sizes, then there would be an irregular tail,
// which we will process separately.
let mut last_idx = 0;
let mut chunk = 1;
let mut pow = bits;
// Handle overflow
if pow >= 31 {
assert_eq!(pow, 31, "sanity");
pow -= 1;
chunk = 2;
last_idx = 1 << pow;
self.visitor
.push(MarkTask::new2(obj, true, false, chunk as _, pow as _));
}
// Split out tasks, as suggested in ShenandoahMarkTask docs. Record the last
// successful right boundary to figure out the irregular tail.
while (1 << pow) > 2048 && (chunk * 2 < MarkTask::chunk_size() as isize) {
pow -= 1;
let left_chunk = chunk * 2 - 1;
let right_chunk = chunk * 2;
let left_chunk_end = left_chunk * (1 << pow);
if left_chunk_end < len as isize {
self.visitor.push(MarkTask::new2(
obj,
true,
false,
left_chunk as _,
pow as _,
));
chunk = right_chunk;
last_idx = left_chunk_end;
} else {
chunk = left_chunk;
}
}
// Process the irregular tail, if present
let from = last_idx;
if from < len as isize {
iter(obj.add(1).cast(), from as _, len, self.visitor);
}
}
}
}
unsafe fn do_chunked_array(
&mut self,
obj: *mut HeapObjectHeader,
mut chunk: i32,
mut pow: i32,
) {
let varsize = &(*obj).vtable().varsize;
while (1 << pow) > 2048 && (chunk * 2 < MarkTask::chunk_size() as i32) {
pow -= 1;
chunk *= 2;
self.visitor.push(MarkTask::new2(
obj,
true,
false,
chunk as usize - 1,
pow as _,
));
}
let chunk_size = 1 << pow;
let from = (chunk - 1) * chunk_size;
let to = chunk * chunk_size;
if let Some(iter) = varsize.iterate_range {
iter(obj.add(1).cast(), from as _, to as _, self.visitor);
}
}
}
const SEGMENT_SIZE: usize = 64;
struct Segment {
data: Vec<MarkTask>,
}
impl Segment {
fn new() -> Segment {
Segment {
data: Vec::with_capacity(SEGMENT_SIZE),
}
}
fn empty() -> Segment {
Segment { data: Vec::new() }
}
fn with(addr: MarkTask) -> Segment {
let mut segment = Segment::new();
segment.data.push(addr);
segment
}
fn has_capacity(&self) -> bool {
self.data.len() < SEGMENT_SIZE
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
fn push(&mut self, addr: MarkTask) {
debug_assert!(self.has_capacity());
self.data.push(addr);
}
fn pop(&mut self) -> Option<MarkTask> {
self.data.pop()
}
fn len(&mut self) -> usize {
self.data.len()
}
}
pub struct MarkQueueSet {
visitors: Vec<*mut SlotVisitor>,
injector: Arc<Injector<MarkTask>>,
}
impl MarkQueueSet {
pub fn new(nworkers: usize) -> MarkQueueSet {
let injector = Arc::new(Injector::new());
let visitors = (0..nworkers)
.map(|i| {
let worker = Worker::new_lifo();
let stealer = worker.stealer();
Box::into_raw(Box::new(SlotVisitor::new(
i,
worker,
stealer,
injector.clone(),
)))
})
.collect::<Vec<_>>();
Self { injector, visitors }
}
pub fn visitors(&self) -> &[*mut SlotVisitor] {
&self.visitors
}
pub fn visitor(&self, id: usize) -> *mut SlotVisitor {
self.visitors[id]
}
pub fn nworkers(&self) -> usize {
self.visitors.len()
}
pub fn injector(&self) -> &Injector<MarkTask> {
&self.injector
}
}
pub struct STWMark {}
impl STWMark {
pub fn new() -> Self {
Self {}
}
pub fn mark(&self) {
let heap = heap();
// push roots
heap.root_set().process();
let mc = heap.marking_context();
let terminator = Terminator::new(mc.mark_queues().nworkers());
// blocking call, waits for marking task to complete.
heap.workers().scoped(|scope| {
for task_id in 0..mc.mark_queues().nworkers() {
let terminator = &terminator;
scope.execute(move || {
let mut task = MarkingTask::new(
task_id,
&terminator,
super::heap::heap(),
super::heap::heap().marking_context(),
);
task.run::<false>();
});
}
});
}
}
use crate::utils::*;
cfg_if::cfg_if! {
if #[cfg(target_pointer_width="64")] {
/// MarkTask
///
/// Encodes both regular oops, and the array oops plus chunking data for parallel array processing.
/// The design goal is to make the regular obj ops very fast, because that would be the prevailing
/// case. On the other hand, it should not block parallel array processing from efficiently dividing
/// the array work.
///
/// The idea is to steal the bits from the 64-bit obj to encode array data, if needed. For the
/// proper divide-and-conquer strategies, we want to encode the "blocking" data. It turns out, the
/// most efficient way to do this is to encode the array block as (chunk * 2^pow), where it is assumed
/// that the block has the size of 2^pow. This requires for pow to have only 5 bits (2^32) to encode
/// all possible arrays.
///
/// |xx-------obj---------|-pow-|--chunk---|
/// 0 49 54 64
///
/// By definition, chunk == 0 means "no chunk", i.e. chunking starts from 1.
///
/// Lower bits of obj are reserved to handle "skip_live" and "strong" properties. Since this encoding
/// stores uncompressed oops, those bits are always available. These bits default to zero for "skip_live"
/// and "weak". This aligns with their frequent values: strong/counted-live references.
///
/// This encoding gives a few interesting benefits:
///
/// a) Encoding/decoding regular oops is very simple, because the upper bits are zero in that task:
///
/// |---------obj---------|00000|0000000000| /// no chunk data
///
/// This helps the most ubiquitous path. The initialization amounts to putting the obj into the word
/// with zero padding. Testing for "chunkedness" is testing for zero with chunk mask.
///
/// b) Splitting tasks for divide-and-conquer is possible. Suppose we have chunk <C, P> that covers
/// interval [ (C-1)*2^P; C*2^P ). We can then split it into two chunks:
/// <2*C - 1, P-1>, that covers interval [ (2*C - 2)*2^(P-1); (2*C - 1)*2^(P-1) )
/// <2*C, P-1>, that covers interval [ (2*C - 1)*2^(P-1); 2*C*2^(P-1) )
///
/// Observe that the union of these two intervals is:
/// [ (2*C - 2)*2^(P-1); 2*C*2^(P-1) )
///
/// ...which is the original interval:
/// [ (C-1)*2^P; C*2^P )
///
/// c) The divide-and-conquer strategy could even start with chunk <1, round-log2-len(arr)>, and split
/// down in the parallel threads, which alleviates the upfront (serial) splitting costs.
///
/// Encoding limitations caused by current bitscales mean:
/// 10 bits for chunk: max 1024 blocks per array
/// 5 bits for power: max 2^32 array
/// 49 bits for obj: max 512 TB of addressable space
///
/// Stealing bits from obj trims down the addressable space. Stealing too few bits for chunk ID limits
/// potential parallelism. Stealing too few bits for pow limits the maximum array size that can be handled.
/// In future, these might be rebalanced to favor one degree of freedom against another. For example,
/// if/when Arrays 2.0 bring 2^64-sized arrays, we might need to steal another bit for power. We could regain
/// some bits back if chunks are counted in ObjArrayMarkingStride units.
///
/// There is also a fallback version that uses plain fields, when we don't have enough space to steal the
/// bits from the native pointer. It is useful to debug the optimized version.
///
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Hash, Eq, Ord)]
pub struct MarkTask {
/// Everything is encoded into this field...
obj: usize,
}
impl MarkTask {
pub const CHUNK_BITS: usize = 10;
pub const POW_BITS: usize = 5;
pub const OOP_BITS: usize = std::mem::size_of::<usize>() * 8 - Self::CHUNK_BITS - Self::POW_BITS;
pub const OOP_SHIFT: usize = 0;
pub const POW_SHIFT: usize = Self::OOP_BITS;
pub const CHUNK_SHIFT: usize = Self::OOP_BITS + Self::POW_BITS;
pub const OOP_EXTRACT_MASK: usize = right_nth_bit(Self::OOP_BITS) - 3;
pub const SKIP_LIVE_EXTRACT_MASK: usize = 1 << 0;
pub const WEAK_EXTRACT_MASK: usize = 1 << 1;
pub const CHUNK_POW_EXTRACT_MASK: usize = !right_nth_bit(Self::OOP_BITS);
pub const CHUNK_RANGE_MASK: usize = right_nth_bit(Self::CHUNK_BITS);
pub const POW_RANGE_MASK: usize = right_nth_bit(Self::POW_BITS);
#[inline]
pub fn decode_oop(val: usize) -> *mut HeapObjectHeader {
(val & Self::OOP_EXTRACT_MASK) as _
}
#[inline]
pub fn decode_not_chunked(val: usize) -> bool {
(val & Self::CHUNK_POW_EXTRACT_MASK) == 0
}
#[inline]
pub fn decode_chunk(val: usize) -> usize {
(val >> Self::CHUNK_SHIFT) & Self::CHUNK_RANGE_MASK
}
#[inline]
pub fn decode_pow(val: usize) -> usize {
(val >> Self::POW_SHIFT) & Self::POW_RANGE_MASK
}
#[inline]
pub fn decode_weak(val: usize) -> bool {
(val & Self::WEAK_EXTRACT_MASK) != 0
}
#[inline]
pub fn decode_cnt_live(val: usize) -> bool {
(val & Self::SKIP_LIVE_EXTRACT_MASK) == 0
}
#[inline]
pub fn encode_oop(obj: *mut HeapObjectHeader, skip_live: bool, weak: bool) -> usize {
let mut encoded = obj as usize;
if skip_live {
encoded |= Self::SKIP_LIVE_EXTRACT_MASK;
}
if weak {
encoded |= Self::WEAK_EXTRACT_MASK;
}
encoded
}
#[inline]
pub fn encode_chunk(chunk: usize) -> usize {
chunk << Self::CHUNK_SHIFT
}
#[inline]
pub fn encode_pow(pow: usize) -> usize {
pow << Self::POW_SHIFT
}
#[inline]
pub fn new(obj: *mut HeapObjectHeader, skip_live: bool, weak: bool) -> Self {
Self {
obj: Self::encode_oop(obj, skip_live, weak)
}
}
#[inline]
pub fn new2(obj: *mut HeapObjectHeader, skip_live: bool, weak: bool, chunk: usize, pow: usize) -> Self {
let enc_oop = Self::encode_oop(obj, skip_live, weak);
let enc_chunk = Self::encode_chunk(chunk);
let enc_pow = Self::encode_pow(pow);
Self {
obj: enc_oop | enc_chunk | enc_pow
}
}
#[inline]
pub fn obj(self) -> *mut HeapObjectHeader {
Self::decode_oop(self.obj)
}
#[inline]
pub fn chunk(self) -> usize {
Self::decode_chunk(self.obj)
}
#[inline]
pub fn pow(self) -> usize {
Self::decode_pow(self.obj)
}
#[inline]
pub fn is_not_chunked(self) -> bool {
Self::decode_not_chunked(self.obj)
}
#[inline]
pub fn is_weak(self) -> bool {
Self::decode_weak(self.obj)
}
#[inline]
pub fn count_liveness(self) -> bool {
Self::decode_cnt_live(self.obj)
}
#[inline]
pub fn max_addressable() -> usize {
nth_bit(Self::OOP_BITS)
}
#[inline]
pub fn chunk_size() -> usize {
nth_bit(Self::CHUNK_BITS)
}
}
} else {
/// MarkTask
///
/// Encodes both regular oops, and the array oops plus chunking data for parallel array processing.
/// The design goal is to make the regular obj ops very fast, because that would be the prevailing
/// case. On the other hand, it should not block parallel array processing from efficiently dividing
/// the array work.
///
/// The idea is to steal the bits from the 64-bit obj to encode array data, if needed. For the
/// proper divide-and-conquer strategies, we want to encode the "blocking" data. It turns out, the
/// most efficient way to do this is to encode the array block as (chunk * 2^pow), where it is assumed
/// that the block has the size of 2^pow. This requires for pow to have only 5 bits (2^32) to encode
/// all possible arrays.
///
/// |xx-------obj---------|-pow-|--chunk---|
/// 0 49 54 64
///
/// By definition, chunk == 0 means "no chunk", i.e. chunking starts from 1.
///
/// Lower bits of obj are reserved to handle "skip_live" and "strong" properties. Since this encoding
/// stores uncompressed oops, those bits are always available. These bits default to zero for "skip_live"
/// and "weak". This aligns with their frequent values: strong/counted-live references.
///
/// This encoding gives a few interesting benefits:
///
/// a) Encoding/decoding regular oops is very simple, because the upper bits are zero in that task:
///
/// |---------obj---------|00000|0000000000| /// no chunk data
///
/// This helps the most ubiquitous path. The initialization amounts to putting the obj into the word
/// with zero padding. Testing for "chunkedness" is testing for zero with chunk mask.
///
/// b) Splitting tasks for divide-and-conquer is possible. Suppose we have chunk <C, P> that covers
/// interval [ (C-1)*2^P; C*2^P ). We can then split it into two chunks:
/// <2*C - 1, P-1>, that covers interval [ (2*C - 2)*2^(P-1); (2*C - 1)*2^(P-1) )
/// <2*C, P-1>, that covers interval [ (2*C - 1)*2^(P-1); 2*C*2^(P-1) )
///
/// Observe that the union of these two intervals is:
/// [ (2*C - 2)*2^(P-1); 2*C*2^(P-1) )
///
/// ...which is the original interval:
/// [ (C-1)*2^P; C*2^P )
///
/// c) The divide-and-conquer strategy could even start with chunk <1, round-log2-len(arr)>, and split
/// down in the parallel threads, which alleviates the upfront (serial) splitting costs.
///
/// Encoding limitations caused by current bitscales mean:
/// 10 bits for chunk: max 1024 blocks per array
/// 5 bits for power: max 2^32 array
/// 49 bits for obj: max 512 TB of addressable space
///
/// Stealing bits from obj trims down the addressable space. Stealing too few bits for chunk ID limits
/// potential parallelism. Stealing too few bits for pow limits the maximum array size that can be handled.
/// In future, these might be rebalanced to favor one degree of freedom against another. For example,
/// if/when Arrays 2.0 bring 2^64-sized arrays, we might need to steal another bit for power. We could regain
/// some bits back if chunks are counted in ObjArrayMarkingStride units.
///
/// There is also a fallback version that uses plain fields, when we don't have enough space to steal the
/// bits from the native pointer. It is useful to debug the optimized version.
///
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Hash, Eq, Ord)]
pub struct MarkTask {
obj: usize,
skip_live: bool,
weak: bool,
chunk: i32,
pow: i32
}
impl MarkTask {
pub const CHUNK_BITS: u8 = 10;
pub const POW_BITS: u8 = 5;
pub const CHUNK_MAX: usize = nth_bit(Self::CHUNK_BITS as usize) - 1;
pub const POW_MAX: usize = nth_bit(Self::POW_BITS as usize) - 1;
#[inline]
pub fn new(obj: *mut HeapObjectHeader, skip_live: bool, weak: bool) -> Self {
Self {
obj: obj as usize,
skip_live,
weak,
chunk: 0,
pow: 0
}
}
#[inline]
pub fn new2(obj: *mut HeapObjectHeader, skip_live: bool, weak: bool, chunk: usize, pow: usize) -> Self {
Self {
obj: obj as usize,
skip_live,
weak,
chunk: chunk as i32,
pow: pow as i32
}
}
#[inline]
pub fn obj(self) -> *mut HeapObjectHeader {
self.obj as *mut HeapObjectHeader
}
#[inline]
pub fn chunk(self) -> usize {
self.chunk as usize
}
#[inline]
pub fn pow(self) -> usize {
self.pow as usize
}
#[inline]
pub fn is_not_chunked(self) -> bool {
self.chunk == 0
}
#[inline]
pub fn is_weak(self) -> bool {
self.weak
}
#[inline]
pub fn count_liveness(self) -> bool {
!self.skip_live
}
#[inline]
pub fn max_addressable(self) -> usize {
size_of::<usize>()
}
#[inline]
pub fn max_chunk(self) -> usize {
Self::CHUNK_MAX
}
#[inline]
pub const fn chunk_size() -> usize {
nth_bit(Self::CHUNK_BITS as usize)
}
}
}
}
pub struct GlobalMark {
visitors: Vec<Arc<SlotVisitor>>,
}
pub struct SlotVisitor {
task_id: usize,
heap: *mut Heap,
mark_ctx: *const MarkingContext,
local: Segment,
marked: usize,
visit_count: usize,
worker: Worker<MarkTask>,
stealer: Stealer<MarkTask>,
injector: Arc<Injector<MarkTask>>,
}
impl SlotVisitor {
pub fn set_mark_ctx(&mut self, mark_ctx: *const MarkingContext) {
self.mark_ctx = mark_ctx;
self.heap = heap();
}
pub fn new(
task_id: usize,
worker: Worker<MarkTask>,
stealer: Stealer<MarkTask>,
injector: Arc<Injector<MarkTask>>,
) -> Self {
Self {
task_id,
heap: heap(),
mark_ctx: null(),
visit_count: 0,
worker,
stealer,
injector,
local: Segment::new(),
marked: 0,
}
}
pub fn visit_count(&self) -> usize {
self.visit_count
}
pub fn mark_ctx(&self) -> &MarkingContext {
unsafe { &*self.mark_ctx }
}
pub fn pop(&mut self) -> Option<MarkTask> {
self.pop_local()
.or_else(|| self.pop_worker())
.or_else(|| self.pop_global())
.or_else(|| self.steal())
}
fn pop_global(&mut self) -> Option<MarkTask> {
loop {
let result = self
.mark_ctx()
.mark_queues()
.injector()
.steal_batch_and_pop(self.worker());
match result {
Steal::Empty => break,
Steal::Success(value) => return Some(value),
Steal::Retry => continue,
}
}
None
}
fn steal(&self) -> Option<MarkTask> {
if self.mark_ctx().mark_queues().nworkers() == 1 {
return None;
}
let mut rng = thread_rng();
let range = Uniform::new(0, self.mark_ctx().mark_queues().nworkers());
for _ in 0..2 * self.mark_ctx().mark_queues().nworkers() {
let mut stealer_id = self.task_id;
while stealer_id == self.task_id {
stealer_id = range.sample(&mut rng);
}
let stealer = unsafe { &(&*self.mark_ctx().mark_queues().visitor(stealer_id)).stealer };
loop {
match stealer.steal_batch_and_pop(self.worker()) {
Steal::Empty => break,
Steal::Success(address) => return Some(address),
Steal::Retry => continue,
}
}
}
None
}
fn pop_local(&mut self) -> Option<MarkTask> {
if self.local.is_empty() {
return None;
}
let obj = self.local.pop().expect("should be non-empty");
Some(obj)
}
fn pop_worker(&mut self) -> Option<MarkTask> {
self.worker.pop()
}
fn worker(&self) -> &Worker<MarkTask> {
&self.worker
}
fn injector(&self) -> &Injector<MarkTask> {
&self.injector
}
fn defensive_push(&mut self) {
self.marked += 1;
if self.marked > 256 {
if self.local.len() > 4 {
let target_len = self.local.len() / 2;
while self.local.len() > target_len {
let val = self.local.pop().unwrap();
self.injector().push(val);
}
}
self.marked = 0;
}
}
pub fn push(&mut self, obj: MarkTask) {
unsafe {
debug_assert!((*self.heap).is_in(obj.obj() as _));
}
self.visit_count += 1;
if self.local.has_capacity() {
self.local.push(obj);
self.defensive_push();
} else {
self.worker().push(obj);
}
}
unsafe fn try_conservative(&mut self, obj: *const u8) {
if !(*self.heap).is_in(obj as _) {
return;
}
let object = (*self.heap).object_start(obj as _);
if object.is_null() {
return;
}
self.visit(object.add(1).cast());
}
}
impl Visitor for SlotVisitor {
unsafe fn visit(&mut self, obj: *const u8) {
unsafe {
let obj = obj.cast::<HeapObjectHeader>().sub(1);
if self.mark_ctx().mark(obj) {
if (*obj).should_trace() {
self.push(MarkTask::new(obj as _, false, false));
}
}
}
}
unsafe fn visit_conservative(&mut self, ptrs: *const *const u8, len: usize) {
unsafe {
for i in 0..len {
let ptr = ptrs.add(i).read();
self.try_conservative(ptr);
}
}
}
fn visit_count(&self) -> usize {
self.visit_count
}
}