rsgc 1.1.0

Concurrent GC library for Rust
Documentation
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
use core::fmt;
use std::intrinsics::unlikely;
use std::{mem::size_of, ptr::null_mut, time::Instant};

use crate::utils::env::{get_total_memory, read_float_from_env};
use crate::heap::heap::heap;
use crate::sync::mutex::Mutex;
use crate::system::object::HeapObjectHeader;
use crate::{utils::env::read_uint_from_env, formatted_size};

use super::marking_context::MarkingContext;
use super::virtual_memory::{PlatformVirtualMemory, VirtualMemory, VirtualMemoryImpl};
use super::GCHeuristic;
use super::{align_down, bitmap::HeapBitmap, free_list::FreeList, virtual_memory};

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum RegionState {
    Regular,
    HumongousStart,
    HumongousCont,
    EmptyUncommited,
    EmptyCommited,
    Trash,
}

/// HeapRegion is a header for a region of memory that is used for allocation.
///
/// It is stored in side-table instead of being stored in the region itself.
///
/// # Region types
///
/// ## Regular
///
/// Regular region is a free-list region, this means allocation in it can happen only from free-list.
///
/// ## Humongous
///
/// Humongous region is used for large objects, there might be object larger than region, then there is a few contiguous
/// regions after first humongous region.
///
/// ## Empty
///
/// Empty unallocated region.
pub struct HeapRegion {
    typ: RegionState,
    index: usize,
    used: usize,
    pub visited: bool,
    pub(crate) object_start_bitmap: HeapBitmap<16>,
    /// Start of region memory
    begin: *mut u8,
    /// End of region memory
    end: *mut u8,
    pub(crate) free_list: FreeList,
    pub(crate) largest_free_list_entry: usize,
    pub(crate) last_sweep_free: usize,
    pub empty_time: Instant,
    pub to_sweep: bool,
}

#[derive(Debug)]
pub struct HeapArguments {
    /// With automatic region sizing, the regions would be at most
    /// this large.
    pub max_region_size: usize,
    /// With automatic region sizing, the regions would be at least
    /// this large.
    pub min_region_size: usize,
    /// With automatic region sizing, this is the approximate number
    /// of regions that would be used, within min/max region size
    /// limits."
    pub target_num_regions: usize,
    /// Humongous objects are allocated in separate regions.
    /// This setting defines how large the object should be to be
    /// deemed humongous. Value is in  percents of heap region size.
    /// This also caps the maximum TLAB size.
    pub humongous_threshold: usize,
    /// Static heap region size. Set zero to enable automatic sizing.
    pub region_size: usize,
    /// Enable Elastic TLABs. Elastic TLABs are TLABs that have dynamic size, from [min_tlab_size](HeapArguments::min_tlab_size) to 
    /// [tlab_size](HeapArguments::tlab_size). Elastic TLABs are enabled by default.
    pub elastic_tlab: bool,
    /// Minimum TLAB size.
    pub min_tlab_size: usize,
    /// TLAB size specified by user, if set to 0 then determined automatically (1/8 of a region usually).
    pub tlab_size: usize,
    pub tlab_waste_target_percent: usize,
    pub tlab_refill_waste_fraction: usize,
    pub tlab_waste_increment: usize,
    /// Maximum heap size in bytes. 
    pub max_heap_size: usize,
    /// Minimum heap size in bytes.
    pub min_heap_size: usize,
    /// Initial heap size in bytes. It is number of bytes that are commited at the start of the program.
    pub initial_heap_size: usize,
    /// Minimum free bytes in the heap before GC is triggered.
    pub min_free_threshold: usize,
    /// Allocation threshold in bytes. If allocation threshold is reached, GC is triggered.
    pub allocation_threshold: usize,
    /// Interval between *guaranteed* GC cycles in milliseconds.
    pub guaranteed_gc_interval: usize,
    /// How much controller thread will sleep before checking if GC is needed.
    pub control_interval_min: usize,
    /// Maximum interval for controller thread to sleep.
    pub control_interval_max: usize,
    /// How much to adjust the interval for sleeping between GC checks.
    pub control_interval_adjust_period: usize,
    /// Enable uncommit of unused regions.
    pub uncommit: bool,
    /// Uncommit region only if it was unused for this many milliseconds.
    pub uncommit_delay: usize,
    /// How many regions to process at once during parallel region
    /// iteration. Affects heaps with lots of regions.
    pub parallel_region_stride: usize,
    /// How many threads to use for parallel GC.
    pub parallel_gc_threads: usize,
    /// Minimum percentage of real memory used for maximum heap
    /// size on systems with small physical memory size
    pub min_ram_percentage: f64,
    /// Maximum percentage of real memory used for maximum heap size.
    pub max_ram_percentage: f64,
    /// Percentage of real memory used for initial heap size
    pub initial_ram_percentage: f64,
    /// Enable parallel mark.
    pub parallel_mark: bool,
    /// Enable parallel sweep.
    pub parallel_sweep: bool,
    /// How many items to process during one marking iteration before 
    /// checking for cancellation, yielding, etc. Larger values improve
    /// marking performance at expense of responsiveness. (default: 1000)
    pub mark_loop_stride: usize,
    /// Unused for now. How much times SATB buffers can be flushed before triggering STW.
    pub max_satb_buffer_flushes: usize,
    /// Number of entries in an SATB log buffer. Default is 1024.
    pub max_satb_buffer_size: usize,
    /// Deprecated, CMS failure always triggers Full GC. 
    /// How many Degenarate GC cycles can be triggered before triggering Full GC.
    pub full_gc_threshold: usize,
    /// Always do full GC cycle.
    pub always_full: bool,
    /// The decay factor (alpha) used for values in the weighted
    /// moving average of cycle time and allocation rate. 
    /// Larger values give more weight to recent values.
    /// 
    /// Default: 0.5
    pub adaptive_decay_factor: f64,
    /// The number of cycles some heuristics take to collect in order 
    /// to learn application and GC performance. 
    pub learning_steps: usize,

    /// The cycle may shortcut when enough garbage can be reclaimed 
    /// from the immediate garbage (completely garbage regions).
    /// In percents of total garbage found. Setting this threshold 
    /// to 100 effectively disables the shortcut.
    pub immediate_threshold: usize,
    /// The number of times per second to update the allocation rate moving average 
    pub adaptive_sample_frequency_hz: usize,
    /// The size of the moving window over which the average allocation rate is maintained. 
    /// The total number of samples is the product of this number and the sample frequency.
    pub adaptive_sample_frequency_size_seconds: usize,
    /// The number of standard deviations used to determine an initial
    /// margin of error for the average cycle time and average 
    /// llocation rate. Increasing this value will cause the 
    /// heuristic to initiate more concurrent cycles.
    pub adaptive_initial_confidence: f64,
    /// If the most recently sampled allocation rate is more than 
    /// this many standard deviations away from the moving average, 
    /// then a cycle is initiated. This value controls how sensitive 
    /// the heuristic is to allocation spikes. Decreasing this number 
    /// ncreases the sensitivity. 
    pub adaptive_initial_spike_threshold: f64,
    /// How much of heap should some heuristics reserve for absorbing 
    /// the allocation spikes. Larger value wastes more memory in 
    /// non-emergency cases, but provides more safety in emergency 
    /// cases. In percents of max heap size.
    pub alloc_spike_factor: usize,

    /// GC heuristics to use. This fine-tunes the GC mode selected,
    /// by choosing when to start the GC, how much to process on each
    /// cycle, and what other features to automatically enable.
    /// Possible values are:
    /// - adaptive - adapt to maintain the given amount of free heap at all times, even during the GC cycle;
    /// - static -  trigger GC when free heap falls below the threshold;
    /// - aggressive - run GC continuously;
    /// - compact - run GC more frequently and with deeper targets to free up more memory.
    pub heuristics: GCHeuristic,
    pub init_free_threshold: usize,
    /// Pace application allocations to give GC chance to start
    /// and complete before allocation failure is reached.
    pub pacing: bool,
    /// Max delay for pacing application allocations. Larger values
    /// provide more resilience against out of memory, at expense at
    /// hiding the GC latencies in the allocation path. Time is in
    /// milliseconds. Setting it to arbitrarily large value makes
    /// GC effectively stall the threads indefinitely instead of going
    /// to degenerated or Full GC.
    pub pacing_max_delay: usize,
    /// How much of free space to take as non-taxable allocations the GC cycle.
    /// Larger value makes the pacing milder at the beginning of the cycle.
    /// Lower value makes the pacing less uniform during the cyclent. In percent of free space
    pub pacing_cycle_slack: usize,
    pub pacing_idle_slack: usize,
    /// Additional pacing tax surcharge to help unclutter the heap.
    /// Larger values makes the pacing more aggressive. Lower values
    /// risk GC cycles finish with less memory than were available at the beginning of it.
    pub pacing_surcharge: f64,
    
    pub parallel_root_mark_tasks: bool,
    /// Size of heap (bytes) per GC thread used in calculating the number of GC threads.
    pub heap_size_per_gc_thread: usize,
}

impl HeapArguments {
    pub fn from_env() -> Self {
        let mut this = Self::default();

        match read_uint_from_env("GC_MIN_REGION_SIZE") {
            Some(size) => this.min_region_size = size,
            None => (),
        }

        match read_uint_from_env("GC_MAX_REGION_SIZE") {
            Some(size) => this.max_region_size = size,
            None => (),
        }

        match read_uint_from_env("GC_REGION_SIZE") {
            Some(size) => this.region_size = size,
            None => (),
        }

        match read_uint_from_env("GC_ELASTIC_TLAB") {
            Some(x) => this.elastic_tlab = x > 0,
            None => (),
        }

        match read_uint_from_env("GC_TARGET_NUM_REGIONS") {
            Some(x) => this.target_num_regions = x,
            None => (),
        }

        match read_uint_from_env("GC_HUMONGOUS_THRESHOLD") {
            Some(x) => this.humongous_threshold = x,
            None => (),
        }

        match read_uint_from_env("GC_MIN_TLAB_SIZE") {
            Some(x) => this.min_tlab_size = x,
            None => (),
        }

        match read_uint_from_env("GC_TLAB_SIZE") {
            Some(x) => {
                this.tlab_size = x;
            }
            None => (),
        }

        match read_uint_from_env("GC_TLAB_WASTE_TARGET_PERCENT") {
            Some(x) => this.tlab_waste_target_percent = x,
            None => (),
        }

        match read_uint_from_env("GC_TLAB_REFILL_WASTE_FRACTION") {
            Some(x) => this.tlab_refill_waste_fraction = x,
            None => (),
        }

        match read_uint_from_env("GC_TLAB_WASTE_INCREMENT") {
            Some(x) => this.tlab_waste_increment = x,
            None => (),
        }

        match read_uint_from_env("GC_MAX_HEAP_SIZE") {
            Some(x) => this.max_heap_size = x,
            None => (),
        }

        match read_uint_from_env("GC_INITIAL_HEAP_SIZE") {
            Some(x) => this.initial_heap_size = x,
            None => (),
        }

        match read_uint_from_env("GC_MIN_HEAP_SIZE") {
            Some(x) => this.min_heap_size = x,
            None => (),
        }

        match read_uint_from_env("GC_MIN_FREE_THRESHOLD") {
            Some(x) => this.min_free_threshold = x,
            None => (),
        }

        match read_uint_from_env("GC_ALLOCATION_THRESHOLD") {
            Some(x) => this.allocation_threshold = x,
            None => (),
        }

        match read_uint_from_env("GC_GUARANTEED_GC_INTERVAL") {
            Some(x) => this.guaranteed_gc_interval = x,
            None => (),
        }

        match read_uint_from_env("GC_CONTROL_INTERVAL_MIN") {
            Some(x) => this.control_interval_min = x,
            None => (),
        }

        match read_uint_from_env("GC_CONTROL_INTERVAL_MAX") {
            Some(x) => this.control_interval_max = x,
            None => (),
        }

        match read_uint_from_env("GC_CONTROL_INTERVAL_ADJUST_PERIOD") {
            Some(x) => this.control_interval_adjust_period = x,
            None => (),
        }

        match read_uint_from_env("GC_UNCOMMIT") {
            Some(x) => this.uncommit = x > 0,
            None => (),
        }

        match read_uint_from_env("GC_UNCOMMIT_DELAY") {
            Some(x) => this.uncommit_delay = x,
            None => (),
        }

        match read_uint_from_env("GC_PARALLEL_REGION_STRIDE") {
            Some(x) => this.parallel_region_stride = x,
            None => (),
        }

        match read_uint_from_env("GC_PARALLEL_THREADS") {
            Some(x) => this.parallel_gc_threads = x,
            None => (),
        }

        match read_uint_from_env("GC_PARALLEL_MARK") {
            Some(x) => this.parallel_mark = x > 0,
            None => (),
        }

        match read_uint_from_env("GC_PARALLEL_SWEEP") {
            Some(x) => this.parallel_sweep = x > 0,
            None => (),
        }

        match read_float_from_env("GC_MIN_RAM_PERCENTAGE") {
            Some(x) => this.min_ram_percentage = x,
            None => (),
        }

        match read_float_from_env("GC_MAX_RAM_PERCENTAGE") {
            Some(x) => this.max_ram_percentage = x,
            None => (),
        }

        match read_float_from_env("GC_INITIAL_RAM_PERCENTAGE") {
            Some(x) => this.initial_ram_percentage = x,
            None => (),
        }

        match read_uint_from_env("GC_MARK_LOOP_STRIDE") {
            Some(x) => this.mark_loop_stride = x,
            None => (),
        }

        match read_uint_from_env("GC_MAX_SATB_BUFFER_FLUSHES") {
            Some(x) => this.max_satb_buffer_flushes = x,
            None => (),
        }

        match read_uint_from_env("GC_MAX_SATB_BUFFER_SIZE") {
            Some(x) => this.max_satb_buffer_size = x,
            None => (),
        }

        match read_uint_from_env("GC_FULL_GC_THRESHOLD") {
            Some(x) => this.full_gc_threshold = x,
            None => (),
        }

        match read_uint_from_env("GC_ALWAYS_FULL") {
            Some(x) => this.always_full = x > 0,
            None => (),
        }

        match read_float_from_env("GC_ADAPTIVE_DECAY_FACTOR") {
            Some(x) => this.adaptive_decay_factor = x,
            None => (),
        }

        match read_uint_from_env("GC_PARALLEL_ROOT_MARKS") {
            Some(x) => this.parallel_root_mark_tasks = x != 0,
            None => (),
        }

        match read_uint_from_env("GC_LEARNING_STEPS") {
            Some(x) => if x == 0 {
                this.learning_steps = 1;
            } else {
                this.learning_steps = x;
            }
            None => ()
        }

        match read_uint_from_env("GC_HEAP_SIZE_PER_GC_THREAD") {
            Some(x) => this.heap_size_per_gc_thread = x,
            None => (),
        }

        this.heuristics = match std::env::var("GC_HEURISTICS") {
            Ok(x) => match x.to_lowercase().as_str() {
                "adaptive" => GCHeuristic::Adaptive,
                "static" => GCHeuristic::Static,
                _ => todo!("unsupported or unknown heuristic: {}", x),
            },
            _ => GCHeuristic::Adaptive,
        };

        this
    }

    pub fn set_heap_size(&mut self) {
        let phys_mem = get_total_memory();

        if self.max_heap_size == 96 * 1024 * 1024 {
            let mut reasonable_max = ((phys_mem as f64 * self.max_ram_percentage) / 100.0) as usize;
            let reasonable_min = ((phys_mem as f64 * self.min_ram_percentage) / 100.0) as usize;

            if reasonable_min < self.max_heap_size {
                reasonable_max = reasonable_min;
            } else {
                reasonable_max = reasonable_max.max(self.max_heap_size);
            }

            if self.initial_heap_size != 0 {
                reasonable_max = reasonable_max.max(self.initial_heap_size);
            } else if self.min_heap_size != 0 {
                reasonable_max = reasonable_max.max(self.min_heap_size);
            }

            log::info!(target: "gc", " Maximum heap size {}", formatted_size(reasonable_max));
            self.max_heap_size = reasonable_max;
        }

        if self.initial_heap_size == 0 || self.min_heap_size == 0 {
            let mut reasonable_minimum = 5 * 1024 * 1024; // 5MB

            reasonable_minimum = reasonable_minimum.min(self.max_heap_size);

            if self.initial_heap_size == 0 {
                let mut reasonable_initial =
                    ((phys_mem as f64 * self.initial_ram_percentage) / 100.0) as usize;
                reasonable_initial = reasonable_initial
                    .max(reasonable_minimum)
                    .max(self.min_heap_size);
                reasonable_initial = reasonable_initial.min(self.max_heap_size);

                log::info!(target: "gc", " Initial heap size {}", formatted_size(reasonable_initial));
                self.initial_heap_size = reasonable_initial;
            }

            if self.min_heap_size == 0 {
                log::info!(target: "gc", " Minimum heap size {}", formatted_size(reasonable_minimum));
                self.min_heap_size = reasonable_minimum;
            }
        }
    }
}

impl Default for HeapArguments {
    fn default() -> Self {
        Self {
            parallel_root_mark_tasks: true,
            always_full: false,
            min_region_size: virtual_memory::page_size(),
            max_region_size: 32 * 1024 * 1024,
            target_num_regions: 2048,
            humongous_threshold: 100,
            region_size: 0,
            elastic_tlab: false,
            min_free_threshold: 10,
            allocation_threshold: 0,
            guaranteed_gc_interval: 5 * 60 * 1000,
            uncommit: true,
            uncommit_delay: 5 * 60 * 1000,
            control_interval_min: 1,
            control_interval_max: 10,
            control_interval_adjust_period: 1000,
            min_tlab_size: 2 * 1024,
            max_heap_size: 96 * 1024 * 1024,
            min_heap_size: 0,
            min_ram_percentage: 50.0,
            max_ram_percentage: 25.0,
            initial_ram_percentage: 1.5625,
            initial_heap_size: 0,
            parallel_mark: true,
            parallel_sweep: true,
            mark_loop_stride: 1000,
            tlab_refill_waste_fraction: 64,
            tlab_size: 0,
            tlab_waste_increment: 4,
            tlab_waste_target_percent: 1,
            parallel_region_stride: 1024,
            parallel_gc_threads: 2,

            max_satb_buffer_flushes: 5,
            max_satb_buffer_size: 1024,
            full_gc_threshold: 3,
            adaptive_decay_factor: 0.5,
            adaptive_initial_confidence: 1.8,
            adaptive_initial_spike_threshold: 1.8,
            adaptive_sample_frequency_hz: 10,
            adaptive_sample_frequency_size_seconds: 10,
            alloc_spike_factor: 5,
            learning_steps: 5,
            immediate_threshold: 90,
            heuristics: GCHeuristic::Adaptive,
            init_free_threshold: 70,
            pacing: true,
            pacing_cycle_slack: 10,
            pacing_idle_slack: 2,
            pacing_max_delay: 10,
            pacing_surcharge: 1.1,
            heap_size_per_gc_thread: 64 * 1024 * 1024,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Default, Debug)]
pub struct HeapOptions {
    pub region_size_bytes: usize,
    pub region_size_words: usize,
    pub region_size_bytes_shift: usize,
    pub region_size_bytes_mask: usize,
    pub region_size_words_shift: usize,
    pub region_size_words_mask: usize,
    pub region_payload_size_bytes: usize,
    pub region_payload_size_words: usize,
    pub region_count: usize,
    pub humongous_threshold_words: usize,
    pub humongous_threshold_bytes: usize,
    pub max_heap_size: usize,
    pub max_tlab_size: usize,
    pub tlab_size: usize,
    pub min_tlab_size: usize,
    pub region_size_log2: usize,
    pub elastic_tlab: bool,
    pub min_free_threshold: usize,
    pub allocation_threshold: usize,
    pub guaranteed_gc_interval: usize,
    pub control_interval_min: usize,
    pub control_interval_max: usize,
    pub control_interval_adjust_period: usize,
    pub uncommit: bool,
    pub uncommit_delay: usize,
    pub target_refills: usize,
    pub parallel_gc_threads: usize,
    pub parallel_region_stride: usize,
    pub parallel_mark: bool,
    pub parallel_sweep: bool,
    pub mark_loop_stride: usize,
    pub max_satb_buffer_flushes: usize,
    pub max_satb_buffer_size: usize,
    pub full_gc_threshold: usize,
    pub always_full: bool,
    pub adaptive_decay_factor: f64,
    pub learning_steps: usize,
    pub immediate_threshold: usize,
    pub adaptive_sample_frequency_hz: usize,
    pub adaptive_sample_frequency_size_seconds: usize,
    pub adaptive_initial_confidence: f64,
    pub adaptive_initial_spike_threshold: f64,
    pub alloc_spike_factor: usize,
    pub init_free_threshold: usize,
    /// Pace application allocations to give GC chance to start
    /// and complete before allocation failure is reached.
    pub pacing: bool,
    /// Max delay for pacing application allocations. Larger values
    /// provide more resilience against out of memory, at expense at
    /// hiding the GC latencies in the allocation path. Time is in
    /// milliseconds. Setting it to arbitrarily large value makes
    /// GC effectively stall the threads indefinitely instead of going
    /// to degenerated or Full GC.
    pub pacing_max_delay: usize,
    /// How much of free space to take as non-taxable allocations the GC cycle.
    /// Larger value makes the pacing milder at the beginning of the cycle.
    /// Lower value makes the pacing less uniform during the cyclent. In percent of free space
    pub pacing_cycle_slack: usize,
    pub pacing_idle_slack: usize,
    /// Additional pacing tax surcharge to help unclutter the heap.
    /// Larger values makes the pacing more aggressive. Lower values
    /// risk GC cycles finish with less memory than were available at the beginning of it.
    pub pacing_surcharge: f64,

    pub parallel_root_mark_tasks: bool,

    /// Size of heap (bytes) per GC thread used in calculating the number of GC threads.
    pub heap_size_per_gc_thread: usize,
}

impl HeapOptions {
    pub const fn required_regions(&self, size: usize) -> usize {
        (size + self.region_size_bytes - 1) >> self.region_size_bytes_shift
    }
}

impl fmt::Display for HeapOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RegionOptions")
            .field("region_size_bytes", &formatted_size(self.region_size_bytes))
            .field("region_size_words", &self.region_size_words)
            .field("region_size_bytes_shift", &self.region_size_bytes_shift)
            .field("region_size_bytes_mask", &self.region_size_bytes_mask)
            .field("region_size_words_shift", &self.region_size_words_shift)
            .field("region_size_words_mask", &self.region_size_words_mask)
            .field("region_count", &self.region_count)
            .field("humongous_threshold_words", &self.humongous_threshold_words)
            .field(
                "humongous_threshold_bytes",
                &formatted_size(self.humongous_threshold_bytes),
            )
            .field("max_heap_size", &formatted_size(self.max_heap_size))
            .field("region_size_log2", &self.region_size_log2)
            .finish()
    }
}

impl HeapRegion {
    pub fn end(&self) -> usize {
        self.end as _
    }

    pub unsafe fn new(
        loc: usize,
        index: usize,
        start: usize,
        opts: &HeapOptions,
        is_commited: bool,
    ) -> *mut Self {
        let p = loc as *mut Self;
        p.write(Self {
            visited: false,
            last_sweep_free: 0,
            typ: if is_commited {
                RegionState::EmptyCommited
            } else {
                RegionState::EmptyUncommited
            },
            begin: start as *mut u8,
            index,
            used: 0,
            to_sweep: false,
            object_start_bitmap: HeapBitmap::new(start, opts.region_size_bytes),
            end: (start + opts.region_size_bytes) as *mut u8,
            free_list: FreeList::new(opts),
            largest_free_list_entry: 0,
            empty_time: Instant::now(),
        });

        (*p).recycle();

        p
    }
    pub fn empty_time(&self) -> Instant {
        self.empty_time
    }

    pub fn allocate(&mut self, size: usize) -> *mut u8 {
        unsafe {
            self.make_regular_allocation();
            let (result, sz) = self.free_list.allocate(size);

            if result.is_null() {
                return null_mut();
            }

            if sz > size {
                let remainder = result.add(size);
                let remainder_size = result.add(sz) as usize - remainder as usize;
                self.free_list.add(remainder, remainder_size);
                self.used += sz - remainder_size;
            } else {
                self.used += sz;
            }

            result
        }
    }

    pub fn do_commit(&mut self) {
        PlatformVirtualMemory::commit(self.bottom() as _, self.size());
        heap().increase_commited(self.size());
        self.typ = RegionState::EmptyCommited;
    }

    pub fn do_decommit(&mut self) {
        PlatformVirtualMemory::decommit(self.bottom() as _, self.size());
        PlatformVirtualMemory::dontneed(self.bottom() as _, self.size());

        heap().decrease_commited(self.size());
        self.free_list.clear();
        self.typ = RegionState::EmptyUncommited;
    }

    pub fn make_regular_allocation(&mut self) {
        if self.typ == RegionState::EmptyUncommited {
            self.do_commit();
        }
        if self.typ == RegionState::EmptyCommited {
            unsafe {
                self.free_list.add(self.bottom() as _, self.size());
            }
        }
        self.typ = RegionState::Regular;
    }

    pub fn used(&self) -> usize {
        self.used
    }

    pub fn free(&self) -> usize {
        if self.typ == RegionState::EmptyUncommited || self.typ == RegionState::EmptyCommited {
            return self.size();
        }
        self.free_list.free()
    }
    pub fn peek_free(&self) -> usize {
        self.free_list.peek_free()
    }

    pub fn index(&self) -> usize {
        self.index
    }

    pub fn bottom(&self) -> usize {
        self.begin as usize
    }

    pub fn size(&self) -> usize {
        self.end as usize - self.begin as usize
    }

    pub fn state(&self) -> RegionState {
        self.typ
    }

    pub fn set_state(&mut self, state: RegionState) {
        self.typ = state
    }

    pub fn set_used(&mut self, bytes: usize) {
        self.used = bytes;
    }

    pub fn contains(&self, ptr: *mut u8) -> bool {
        ptr >= self.begin && ptr < self.end
    }

    pub fn is_empty(&self) -> bool {
        self.typ == RegionState::EmptyUncommited || self.typ == RegionState::EmptyCommited
    }

    pub fn is_empty_commited(&self) -> bool {
        self.typ == RegionState::EmptyCommited
    }

    pub fn is_alloc_allowed(&self) -> bool {
        self.typ == RegionState::Regular || self.is_empty()
    }

    pub fn is_trash(&self) -> bool {
        self.typ == RegionState::Trash
    }

    pub fn make_humonogous_start(&mut self) {
        if self.typ == RegionState::EmptyUncommited {
            self.do_commit();
        }
        self.typ = RegionState::HumongousStart;
        self.free_list.clear();
    }

    pub fn make_humonogous_cont(&mut self) {
        if self.typ == RegionState::EmptyUncommited {
            self.do_commit();
        }
        self.typ = RegionState::HumongousCont;
        self.free_list.clear();
    }

    pub fn recycle(&mut self) {
        self.free_list.clear();
        if self.typ == RegionState::Trash {
            self.make_empty();
        }
        self.object_start_bitmap.clear();
        self.used = 0;
        self.largest_free_list_entry = self.size();
    }

    pub fn largest_free_list_entry(&self) -> usize {
        self.largest_free_list_entry
    }

    pub fn set_largest_free_list_entry(&mut self, size: usize) {
        self.largest_free_list_entry = size;
    }

    pub fn make_trash(&mut self) {
        self.typ = RegionState::Trash;
    }

    pub fn make_empty(&mut self) {
        self.typ = RegionState::EmptyCommited;
        self.empty_time = Instant::now();
    }

    pub fn is_humongous_start(&self) -> bool {
        self.typ == RegionState::HumongousStart
    }

    pub fn is_humongous_cont(&self) -> bool {
        self.typ == RegionState::HumongousCont
    }

    pub fn is_regular(&self) -> bool {
        self.typ == RegionState::Regular
    }


    pub const MIN_REGION_SIZE: usize = 4 * 1024;
    pub const MIN_NUM_PAGES: usize = 10;
    pub const MAX_REGION_SIZE: usize = 32 * 1024 * 1024;

    /// Setups heap region sizes and thresholds based on input parameters.
    ///
    /// # Notes
    /// - `humongous_threshold` is a percentage of memory in region that could be used for large object, for example:
    /// if you have humongous_threshold set to 50 and your region size is 4KB then largest object that will be allocated
    /// using free-list is 2KB, objects larger than that get a separate humongous region(s).
    pub fn setup_sizes(args: &HeapArguments) -> HeapOptions {
        let mut opts = HeapOptions::default();

        opts.parallel_mark = args.parallel_mark;
        opts.parallel_sweep = args.parallel_sweep;
        opts.target_refills = 100 / (2 * args.tlab_waste_target_percent);
        opts.target_refills = opts.target_refills.max(2);
        opts.tlab_size = args.tlab_size;
        opts.uncommit = args.uncommit;
        opts.parallel_gc_threads = args.parallel_gc_threads;
        opts.parallel_region_stride = args.parallel_region_stride;
        opts.allocation_threshold = args.allocation_threshold;
        opts.min_free_threshold = args.min_free_threshold;
        opts.control_interval_adjust_period = args.control_interval_adjust_period;
        opts.control_interval_max = args.control_interval_max;
        opts.control_interval_min = args.control_interval_min;
        opts.uncommit_delay = args.uncommit_delay;
        opts.guaranteed_gc_interval = args.guaranteed_gc_interval;
        opts.mark_loop_stride = args.mark_loop_stride.max(32);
        opts.max_satb_buffer_flushes = args.max_satb_buffer_flushes;
        opts.max_satb_buffer_size = args.max_satb_buffer_size;
        opts.full_gc_threshold = args.full_gc_threshold;
        opts.always_full = args.always_full;
        opts.adaptive_decay_factor = args.adaptive_decay_factor;
        opts.learning_steps = args.learning_steps;
        opts.immediate_threshold = args.immediate_threshold;
        opts.adaptive_sample_frequency_hz = args.adaptive_sample_frequency_hz;
        opts.adaptive_sample_frequency_size_seconds = args.adaptive_sample_frequency_size_seconds;
        opts.adaptive_initial_confidence = args.adaptive_initial_confidence;
        opts.adaptive_initial_spike_threshold = args.adaptive_initial_spike_threshold;
        opts.alloc_spike_factor = args.alloc_spike_factor;
        opts.init_free_threshold = args.init_free_threshold;
        opts.pacing = args.pacing;
        opts.pacing_cycle_slack = args.pacing_cycle_slack;
        opts.pacing_idle_slack = args.pacing_idle_slack;
        opts.pacing_max_delay = args.pacing_max_delay;
        opts.pacing_surcharge = args.pacing_surcharge;
        opts.parallel_root_mark_tasks = args.parallel_root_mark_tasks;

        let min_region_size = if args.min_region_size < Self::MIN_REGION_SIZE {
            Self::MIN_REGION_SIZE
        } else {
            args.min_region_size
        };

        let target_num_regions = if args.target_num_regions == 0 {
            2048
        } else {
            args.target_num_regions
        };
        let max_region_size =
            if args.max_region_size == 0 || args.max_region_size < args.min_region_size {
                Self::MAX_REGION_SIZE
            } else {
                args.max_region_size
            };

        let mut max_heap_size = args.max_heap_size;

        if min_region_size > max_heap_size / Self::MIN_NUM_PAGES {
            panic!("Max heap size ({}) is too low to afford the minimum number of regions ({}) of minimum region size ({})",
                formatted_size(max_heap_size) ,Self::MIN_NUM_PAGES,formatted_size(min_region_size)
            );
        }

        let mut region_size = if args.region_size != 0 {
            args.region_size
        } else {
            let mut region_size = max_heap_size / target_num_regions;
            region_size = region_size.max(min_region_size);
            region_size = max_region_size.min(region_size);
            region_size
        };

        region_size = super::align_usize(region_size, super::virtual_memory::page_size());
        max_heap_size = super::align_usize(max_heap_size, region_size);

        let region_size_log = (region_size as f64).log2() as usize;
        region_size = 1 << region_size_log;
        max_heap_size = super::align_usize(max_heap_size, region_size);
        opts.region_count = max_heap_size / region_size;
        opts.region_size_bytes = region_size;
        opts.region_size_words = region_size / size_of::<usize>();
        opts.region_size_bytes_mask = region_size - 1;
        opts.region_size_words_mask = region_size - 1;
        opts.region_size_bytes_shift = region_size_log;
        opts.region_size_words_shift = opts.region_size_bytes_shift - 3;
        opts.region_size_log2 = region_size_log;
        let humongous_threshold = if args.humongous_threshold == 0 {
            100
        } else {
            args.humongous_threshold
        };
        opts.humongous_threshold_words = opts.region_size_words * humongous_threshold / 100;
        opts.humongous_threshold_words = align_down(opts.humongous_threshold_words, 8);
        opts.humongous_threshold_bytes = opts.humongous_threshold_words * size_of::<usize>();
        opts.max_heap_size = max_heap_size;

        // The rationale for trimming TLAB sizes has to do with the size of regions
        // and wasteful memory usage. The worst case realizes when "answer" is "region size", which means
        // it could prematurely retire an entire region. Having smaller TLABs does not fix that completely, but reduces the probability of too
        // wasteful region usage. With current divisor we will waste no more than 1/8 of region size in the worst
        // case.
        //
        // One example of problem that happens when TLAB size is a region size:
        // Program has 8 mutators and heap size of 256M with 32M regions, if all 8 mutators were to allocate
        // at the same time nothing incredibly bad would not happen, each of threads would get 32M regions.
        // But once program has to start another thread it would end up in OOMing since no free memory is left.
        opts.max_tlab_size = (opts.region_size_bytes / 8).min(opts.humongous_threshold_bytes);
        opts.min_tlab_size = if args.min_tlab_size < 2 * 1024 {
            2 * 1024
        } else {
            args.min_tlab_size
        };
        opts.max_tlab_size = opts.min_tlab_size.max(opts.max_tlab_size);
        opts.elastic_tlab = args.elastic_tlab;
        if opts.tlab_size != 0 {
            opts.tlab_size = opts.tlab_size.min(opts.humongous_threshold_bytes);
        }

        if opts.parallel_region_stride == 0 && opts.parallel_gc_threads != 0
            || opts.parallel_region_stride == 1024
        {
            opts.parallel_region_stride = opts.region_count / opts.parallel_gc_threads;
        }

        log::info!(target: "gc", "Region sizes setup complete");
        log::info!(target: "gc", "- Max heap size: {}", formatted_size(opts.max_heap_size));
        log::info!(target: "gc", "- Region count: {}", opts.region_count);
        log::info!(target: "gc", "- Region size: {}", formatted_size(opts.region_size_bytes));
        log::info!(target: "gc", "- Humongous threshold: {}", formatted_size(opts.humongous_threshold_bytes));
        log::info!(target: "gc", "- Max TLAB size: {}", formatted_size(opts.max_tlab_size));
        log::info!(target: "gc", "- TLAB size: {}", formatted_size(opts.tlab_size));
        opts
    }
}