-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
gcpriv.h
5809 lines (4897 loc) · 223 KB
/
gcpriv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef _DEBUG
#ifdef _MSC_VER
// optimize for speed
#pragma optimize( "t", on )
#endif
#endif
#ifdef __GNUC__
#define inline __attribute__((always_inline)) inline
#else
#define inline __forceinline
#endif // __GNUC__
#include "gc.h"
#include "gcrecord.h"
// The per heap and global fields are separated into the following categories -
//
// Used in GC and needs to be maintained, ie, next GC can be using this field so it needs to have the right value.
// Note that for some fields this doesn't mean the value of the field itself will change (it could remain the same
// throughout the process lifetime (for example, finalize_queue) but you'll need to pay attention to its content
// and make sure it's updated correctly through each GC.
//
// Some fields are marked as "loosely maintained" in their comments - this means they are really only modified during
// a single GC *except* they can be resized during a GC so the reinit-ed value will carry over to later GCs.
// PER_HEAP_FIELD_MAINTAINED
//
// Like PER_HEAP_FIELD_MAINTAINED and also used in the allocator code paths
// PER_HEAP_FIELD_MAINTAINED_ALLOC
//
// Used only during a single GC so we could fill it with an arbitrary value and shouldn't break anything.
// Note that for BGC fields, this means it's initialized at the beginning of that BGC. Ephemeral GCs can happen during
// this single BGC but they do not actually modify these fields.
// PER_HEAP_FIELD_SINGLE_GC
//
// Like PER_HEAP_FIELD_SINGLE_GC and also used in the allocator code paths
// PER_HEAP_FIELD_SINGLE_GC_ALLOC
//
// Only used by the allocator code paths
// PER_HEAP_FIELD_ALLOC
//
// Initialized during the GC init and never changes
// PER_HEAP_FIELD_INIT_ONLY
//
// Used for diagnostics purpose only
// PER_HEAP_FIELD_DIAG_ONLY
//
// Corresponding annotation for global fields
// PER_HEAP_ISOLATED_FIELD_MAINTAINED
// PER_HEAP_ISOLATED_FIELD_MAINTAINED_ALLOC
// PER_HEAP_ISOLATED_FIELD_SINGLE_GC
// PER_HEAP_ISOLATED_FIELD_SINGLE_GC_ALLOC
// PER_HEAP_ISOLATED_FIELD_INIT_ONLY
//
// If a field does not fit any of the above category, such as fgn_maxgen_percent which is only updated by an API,
// it will be marked as PER_HEAP_FIELD/PER_HEAP_ISOLATED_FIELD.
//
// A few notes -
//
// + within the section of a particular category of fields I use the following policy to list them -
// I group the ones that are for the same purpose together, ie, without empty lines inbetween them.
// I list the common ones first, ie, they apply regardless of defines. Then I list the defines in the order of
// #ifdef MULTIPLE_HEAPS
// #ifdef BACKGROUND_GC
// #ifdef USE_REGIONS
// other defines checks
//
// + some of the fields are used by both regions and segments share. When that's the case, the annotation
// is based on regions. So for segments they may or may not apply (segments code is in maintainence mode only).
//
// + some fields are used by the GC and WB but not by the allocator, in which case I will indicate them as such.
#ifdef MULTIPLE_HEAPS
#define PER_HEAP_FIELD
#define PER_HEAP_FIELD_MAINTAINED
#define PER_HEAP_FIELD_MAINTAINED_ALLOC
#define PER_HEAP_FIELD_SINGLE_GC
#define PER_HEAP_FIELD_SINGLE_GC_ALLOC
#define PER_HEAP_FIELD_ALLOC
#define PER_HEAP_FIELD_INIT_ONLY
#define PER_HEAP_FIELD_DIAG_ONLY
#define PER_HEAP_METHOD
#else //MULTIPLE_HEAPS
#define PER_HEAP_FIELD static
#define PER_HEAP_FIELD_MAINTAINED static
#define PER_HEAP_FIELD_MAINTAINED_ALLOC static
#define PER_HEAP_FIELD_SINGLE_GC static
#define PER_HEAP_FIELD_SINGLE_GC_ALLOC static
#define PER_HEAP_FIELD_ALLOC static
#define PER_HEAP_FIELD_INIT_ONLY static
#define PER_HEAP_FIELD_DIAG_ONLY static
#define PER_HEAP_METHOD static
#endif // MULTIPLE_HEAPS
#define PER_HEAP_ISOLATED_FIELD static
#define PER_HEAP_ISOLATED_METHOD static
#define PER_HEAP_ISOLATED_FIELD_MAINTAINED static
#define PER_HEAP_ISOLATED_FIELD_MAINTAINED_ALLOC static
#define PER_HEAP_ISOLATED_FIELD_SINGLE_GC static
#define PER_HEAP_ISOLATED_FIELD_SINGLE_GC_ALLOC static
#define PER_HEAP_ISOLATED_FIELD_INIT_ONLY static
#define PER_HEAP_ISOLATED_FIELD_DIAG_ONLY static
#ifdef _MSC_VER
#pragma warning(disable:4293)
#pragma warning(disable:4477)
#endif //_MSC_VER
inline void FATAL_GC_ERROR()
{
#if defined(TRACE_GC) && defined(SIMPLE_DPRINTF)
flush_gc_log (true);
#endif //TRACE_GC && SIMPLE_DPRINTF
GCToOSInterface::DebugBreak();
_ASSERTE(!"Fatal Error in GC.");
GCToEEInterface::HandleFatalError((unsigned int)COR_E_EXECUTIONENGINE);
}
#ifdef MULTIPLE_HEAPS
// This turns on instrumentation that collects info for heap balancing.
// Define it and make sure you have HEAP_BALANCE_LOG/HEAP_BALANCE_TEMP_LOG
// level logging enabled *only*.
//#define HEAP_BALANCE_INSTRUMENTATION
#endif //MULTIPLE_HEAPS
#ifdef _MSC_VER
#pragma inline_depth(20)
#endif
/* the following section defines the optional features */
// Regions invariants -
//
// + each generation consists of 1+ regions.
// + a region is in a contiguous address range; different regions could have
// gaps inbetween.
// + a region cannot contain more than one generation.
//
// This means any empty regions can be freely used for any generation. For
// Server GC we will balance regions between heaps.
// For now disable regions for StandAlone GC, NativeAOT and MacOS builds
#if defined (HOST_64BIT) && !defined (BUILD_AS_STANDALONE) && !defined(__APPLE__)
#define USE_REGIONS
#endif //HOST_64BIT && BUILD_AS_STANDALONE
//#define SPINLOCK_HISTORY
//#define RECORD_LOH_STATE
#if defined(USE_REGIONS) && defined(MULTIPLE_HEAPS)
// can only change heap count with regions
#define DYNAMIC_HEAP_COUNT
//#define STRESS_DYNAMIC_HEAP_COUNT
#endif //USE_REGIONS && MULTIPLE_HEAPS
#ifdef USE_REGIONS
// Currently this -
// + creates some pins on our own
// + creates some ro segs
// We can add more mechanisms here.
//#define STRESS_REGIONS
//#define COMMITTED_BYTES_SHADOW
#define MARK_PHASE_PREFETCH
#endif //USE_REGIONS
// FEATURE_STRUCTALIGN was added by Midori. In CLR we are not interested
// in supporting custom alignments on LOH. Currently FEATURE_LOH_COMPACTION
// and FEATURE_STRUCTALIGN are mutually exclusive. It shouldn't be much
// work to make FEATURE_STRUCTALIGN not apply to LOH so they can be both
// turned on.
#define FEATURE_LOH_COMPACTION
#ifdef FEATURE_64BIT_ALIGNMENT
// We need the following feature as part of keeping 64-bit types aligned in the GC heap.
#define RESPECT_LARGE_ALIGNMENT //Preserve double alignment of objects during relocation
#endif //FEATURE_64BIT_ALIGNMENT
#define SHORT_PLUGS //used to keep ephemeral plugs short so they fit better into the oldest generation free items
#ifdef SHORT_PLUGS
#define DESIRED_PLUG_LENGTH (1000)
#endif //SHORT_PLUGS
#define FEATURE_PREMORTEM_FINALIZATION
#define GC_HISTORY
#define BACKGROUND_GC //concurrent background GC (requires WRITE_WATCH)
// We need the lower 3 bits in the MT to do our bookkeeping so doubly linked free list is only for 64-bit
#if defined(BACKGROUND_GC) && defined(HOST_64BIT)
#define DOUBLY_LINKED_FL
#endif //HOST_64BIT
#ifndef FEATURE_NATIVEAOT
#define HEAP_ANALYZE
#define COLLECTIBLE_CLASS
#endif // !FEATURE_NATIVEAOT
#ifdef HEAP_ANALYZE
#define initial_internal_roots (1024*16)
#endif // HEAP_ANALYZE
#ifdef SERVER_GC
#define MH_SC_MARK //scalable marking
//#define SNOOP_STATS //diagnostic
#endif //SERVER_GC
//This is used to mark some type volatile only when the scalable marking is used.
#if defined (SERVER_GC) && defined (MH_SC_MARK)
#define SERVER_SC_MARK_VOLATILE(x) VOLATILE(x)
#else //SERVER_GC&&MH_SC_MARK
#define SERVER_SC_MARK_VOLATILE(x) x
#endif //SERVER_GC&&MH_SC_MARK
//#define MULTIPLE_HEAPS //Allow multiple heaps for servers
#define CARD_BUNDLE //enable card bundle feature.(requires WRITE_WATCH)
#define ALLOW_REFERENCES_IN_POH //Allow POH objects to contain references.
#ifdef BACKGROUND_GC
#define BGC_SERVO_TUNING
#endif //BACKGROUND_GC
#if defined(BACKGROUND_GC) || defined(CARD_BUNDLE) || defined(FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP)
#define WRITE_WATCH //Write Watch feature
#endif //BACKGROUND_GC || CARD_BUNDLE
#ifdef WRITE_WATCH
#define array_size 100
#endif //WRITE_WATCH
#define FFIND_DECAY 7 //Number of GC for which fast find will be active
//#define JOIN_STATS //amount of time spent in the join
//#define SYNCHRONIZATION_STATS
//#define SEG_REUSE_STATS
#ifdef SYNCHRONIZATION_STATS
#define BEGIN_TIMING(x) \
int64_t x##_start; \
x##_start = GCToOSInterface::QueryPerformanceCounter()
#define END_TIMING(x) \
int64_t x##_end; \
x##_end = GCToOSInterface::QueryPerformanceCounter(); \
x += x##_end - x##_start
#else //SYNCHRONIZATION_STATS
#define BEGIN_TIMING(x)
#define END_TIMING(x)
#endif //SYNCHRONIZATION_STATS
#ifdef GC_CONFIG_DRIVEN
void GCLogConfig (const char *fmt, ... );
#define cprintf(x) {GCLogConfig x;}
#endif //GC_CONFIG_DRIVEN
// For the bestfit algorithm when we relocate ephemeral generations into an
// existing gen2 segment.
// We recorded sizes from 2^6, 2^7, 2^8...up to 2^30 (1GB). So that's 25 sizes total.
#define MIN_INDEX_POWER2 6
#ifdef SERVER_GC
#ifdef HOST_64BIT
#define MAX_INDEX_POWER2 30
#else
#define MAX_INDEX_POWER2 26
#endif // HOST_64BIT
#else //SERVER_GC
#ifdef HOST_64BIT
#define MAX_INDEX_POWER2 28
#else
#define MAX_INDEX_POWER2 24
#endif // HOST_64BIT
#endif //SERVER_GC
#define MAX_NUM_BUCKETS (MAX_INDEX_POWER2 - MIN_INDEX_POWER2 + 1)
#ifdef USE_REGIONS
// We constrain this as many bookkeeping fields use an int
#define MAX_REGION_SIZE ((size_t)1 << ((sizeof (int32_t) * 8) - 1))
#endif // USE_REGIONS
#define MAX_NUM_FREE_SPACES 200
#define MIN_NUM_FREE_SPACES 5
#ifdef memcpy
#undef memcpy
#endif //memcpy
#ifdef FEATURE_STRUCTALIGN
#define REQD_ALIGN_DCL ,int requiredAlignment
#define REQD_ALIGN_ARG ,requiredAlignment
#define REQD_ALIGN_AND_OFFSET_DCL ,int requiredAlignment,size_t alignmentOffset
#define REQD_ALIGN_AND_OFFSET_DEFAULT_DCL ,int requiredAlignment=DATA_ALIGNMENT,size_t alignmentOffset=0
#define REQD_ALIGN_AND_OFFSET_ARG ,requiredAlignment,alignmentOffset
#else // FEATURE_STRUCTALIGN
#define REQD_ALIGN_DCL
#define REQD_ALIGN_ARG
#define REQD_ALIGN_AND_OFFSET_DCL
#define REQD_ALIGN_AND_OFFSET_DEFAULT_DCL
#define REQD_ALIGN_AND_OFFSET_ARG
#endif // FEATURE_STRUCTALIGN
#ifdef MULTIPLE_HEAPS
#define THREAD_NUMBER_DCL ,int thread
#define THREAD_NUMBER_ARG ,thread
#define THREAD_NUMBER_FROM_CONTEXT int thread = sc->thread_number;
#define THREAD_FROM_HEAP int thread = heap_number;
#define HEAP_FROM_THREAD gc_heap* hpt = gc_heap::g_heaps[thread];
#else
#define THREAD_NUMBER_DCL
#define THREAD_NUMBER_ARG
#define THREAD_NUMBER_FROM_CONTEXT
#define THREAD_FROM_HEAP
#define HEAP_FROM_THREAD gc_heap* hpt = 0;
#endif //MULTIPLE_HEAPS
//These constants are ordered
const int policy_sweep = 0;
const int policy_compact = 1;
const int policy_expand = 2;
#if !defined(FEATURE_NATIVEAOT) && !defined(BUILD_AS_STANDALONE)
#undef assert
#define assert _ASSERTE
#undef ASSERT
#define ASSERT _ASSERTE
#endif // FEATURE_NATIVEAOT
struct GCDebugSpinLock {
VOLATILE(int32_t) lock; // -1 if free, 0 if held
#ifdef _DEBUG
VOLATILE(Thread *) holding_thread; // -1 if no thread holds the lock.
VOLATILE(BOOL) released_by_gc_p; // a GC thread released the lock.
#endif
#if defined (SYNCHRONIZATION_STATS)
// number of times we went into SwitchToThread in enter_spin_lock.
unsigned int num_switch_thread;
// number of times we went into WaitLonger.
unsigned int num_wait_longer;
// number of times we went to calling SwitchToThread in WaitLonger.
unsigned int num_switch_thread_w;
// number of times we went to calling DisablePreemptiveGC in WaitLonger.
unsigned int num_disable_preemptive_w;
#endif
#if defined(DYNAMIC_HEAP_COUNT)
// time in microseconds we wait for the more space lock
uint64_t msl_wait_time;
#endif //DYNAMIC_HEAP_COUNT
GCDebugSpinLock()
: lock(-1)
#ifdef _DEBUG
, holding_thread((Thread*) -1)
#endif
#if defined (SYNCHRONIZATION_STATS)
, num_switch_thread(0), num_wait_longer(0), num_switch_thread_w(0), num_disable_preemptive_w(0)
#endif
#if defined(DYNAMIC_HEAP_COUNT)
, msl_wait_time(0)
#endif //DYNAMIC_HEAP_COUNT
{
}
#if defined (SYNCHRONIZATION_STATS)
void init()
{
num_switch_thread = 0;
num_wait_longer = 0;
num_switch_thread_w = 0;
num_disable_preemptive_w = 0;
}
#endif
};
typedef GCDebugSpinLock GCSpinLock;
class mark;
class heap_segment;
class CObjectHeader;
class sorted_table;
class seg_free_spaces;
class gc_heap;
#define youngest_generation (generation_of (0))
#define large_object_generation (generation_of (loh_generation))
#define pinned_object_generation (generation_of (poh_generation))
#ifdef BACKGROUND_GC
class exclusive_sync;
class recursive_gc_sync;
#endif //BACKGROUND_GC
#ifdef MULTIPLE_HEAPS
// This feature hasn't been enabled for regions yet.
//#ifndef USE_REGIONS
// card marking stealing only makes sense in server GC
// but it works and is easier to debug for workstation GC
// so turn it on for server GC, turn on for workstation GC if necessary
#define FEATURE_CARD_MARKING_STEALING
//#endif //!USE_REGIONS
#endif //MULTIPLE_HEAPS
#ifdef FEATURE_CARD_MARKING_STEALING
class card_marking_enumerator;
#define CARD_MARKING_STEALING_ARG(a) ,a
#define CARD_MARKING_STEALING_ARGS(a,b,c) ,a,b,c
#else // FEATURE_CARD_MARKING_STEALING
#define CARD_MARKING_STEALING_ARG(a)
#define CARD_MARKING_STEALING_ARGS(a,b,c)
#endif // FEATURE_CARD_MARKING_STEALING
// The following 2 modes are of the same format as in clr\src\bcl\system\runtime\gcsettings.cs
// make sure you change that one if you change this one!
enum gc_pause_mode
{
pause_batch = 0, //We are not concerned about pause length
pause_interactive = 1, //We are running an interactive app
pause_low_latency = 2, //short pauses are essential
//avoid long pauses from blocking full GCs unless running out of memory
pause_sustained_low_latency = 3,
pause_no_gc = 4
};
enum gc_loh_compaction_mode
{
loh_compaction_default = 1, // the default mode, don't compact LOH.
loh_compaction_once = 2, // only compact once the next time a blocking full GC happens.
loh_compaction_auto = 4 // GC decides when to compact LOH, to be implemented.
};
enum set_pause_mode_status
{
set_pause_mode_success = 0,
set_pause_mode_no_gc = 1 // NoGCRegion is in progress, can't change pause mode.
};
/*
Latency modes required user to have specific GC knowledge (eg, budget, full blocking GC).
We are trying to move away from them as it makes a lot more sense for users to tell
us what's the most important out of the perf aspects that make sense to them.
In general there are 3 such aspects:
+ memory footprint
+ throughput
+ pause predictibility
Currently the following levels are supported. We may (and will likely) add more
in the future.
+----------+--------------------+---------------------------------------+
| Level | Optimization Goals | Latency Characteristics |
+==========+====================+=======================================+
| 0 | memory footprint | pauses can be long and more frequent |
+----------+--------------------+---------------------------------------+
| 1 | balanced | pauses are more predictable and more |
| | | frequent. the longest pauses are |
| | | shorter than 1. |
+----------+--------------------+---------------------------------------+
*/
enum gc_latency_level
{
latency_level_first = 0,
latency_level_memory_footprint = latency_level_first,
latency_level_balanced = 1,
latency_level_last = latency_level_balanced,
latency_level_default = latency_level_balanced
};
enum gc_tuning_point
{
tuning_deciding_condemned_gen = 0,
tuning_deciding_full_gc = 1,
tuning_deciding_compaction = 2,
tuning_deciding_expansion = 3,
tuning_deciding_promote_ephemeral = 4,
tuning_deciding_short_on_seg = 5
};
enum gc_oh_num
{
soh = 0,
loh = 1,
poh = 2,
unknown = -1,
};
const int total_oh_count = gc_oh_num::poh + 1;
const int recorded_committed_free_bucket = total_oh_count;
const int recorded_committed_bookkeeping_bucket = recorded_committed_free_bucket + 1;
const int recorded_committed_bucket_counts = recorded_committed_bookkeeping_bucket + 1;
gc_oh_num gen_to_oh (int gen);
enum memory_type
{
memory_type_reserved = 0,
memory_type_committed = 1
};
#if defined(TRACE_GC) && defined(BACKGROUND_GC)
static const char * const str_bgc_state[] =
{
"not_in_process",
"bgc_initialized",
"reset_ww",
"mark_handles",
"mark_stack",
"revisit_soh",
"revisit_uoh",
"overflow_soh",
"overflow_uoh",
"final_marking",
"sweep_soh",
"sweep_uoh",
"plan_phase"
};
#endif // defined(TRACE_GC) && defined(BACKGROUND_GC)
enum allocation_state
{
a_state_start = 0,
a_state_can_allocate,
a_state_cant_allocate,
// This could be due to having to wait till a GC is done,
// or having to try a different heap.
a_state_retry_allocate,
a_state_try_fit,
a_state_try_fit_new_seg,
a_state_try_fit_after_cg,
a_state_try_fit_after_bgc,
a_state_try_free_full_seg_in_bgc,
a_state_try_free_after_bgc,
a_state_try_seg_end,
a_state_acquire_seg,
a_state_acquire_seg_after_cg,
a_state_acquire_seg_after_bgc,
a_state_check_and_wait_for_bgc,
a_state_trigger_full_compact_gc,
a_state_trigger_ephemeral_gc,
a_state_trigger_2nd_ephemeral_gc,
a_state_check_retry_seg,
a_state_max
};
enum enter_msl_status
{
msl_entered,
msl_retry_different_heap
};
enum gc_type
{
gc_type_compacting = 0,
gc_type_blocking = 1,
#ifdef BACKGROUND_GC
gc_type_background = 2,
#endif //BACKGROUND_GC
gc_type_max = 3
};
#ifdef DYNAMIC_HEAP_COUNT
enum gc_dynamic_adaptation_mode
{
dynamic_adaptation_default = 0,
dynamic_adaptation_to_application_sizes = 1,
};
#endif //DYNAMIC_HEAP_COUNT
//encapsulates the mechanism for the current gc
class gc_mechanisms
{
public:
VOLATILE(size_t) gc_index; // starts from 1 for the first GC, like dd_collection_count
int condemned_generation;
BOOL promotion;
BOOL compaction;
BOOL loh_compaction;
BOOL heap_expansion;
uint32_t concurrent;
BOOL demotion;
BOOL card_bundles;
int gen0_reduction_count;
BOOL should_lock_elevation;
int elevation_locked_count;
BOOL elevation_reduced;
BOOL minimal_gc;
gc_reason reason;
gc_pause_mode pause_mode;
BOOL found_finalizers;
#ifdef BACKGROUND_GC
BOOL background_p;
bgc_state b_state;
#endif //BACKGROUND_GC
#ifdef STRESS_HEAP
BOOL stress_induced;
#endif // STRESS_HEAP
// These are opportunistically set
uint32_t entry_memory_load;
uint64_t entry_available_physical_mem;
uint32_t exit_memory_load;
void init_mechanisms(); //for each GC
void first_init(); // for the life of the EE
void record (gc_history_global* history);
};
// This is a compact version of gc_mechanism that we use to save in the history.
class gc_mechanisms_store
{
public:
size_t gc_index;
bool promotion;
bool compaction;
bool loh_compaction;
bool heap_expansion;
bool concurrent;
bool demotion;
bool card_bundles;
bool should_lock_elevation;
int condemned_generation : 8;
int gen0_reduction_count : 8;
int elevation_locked_count : 8;
gc_reason reason : 8;
gc_pause_mode pause_mode : 8;
#ifdef BACKGROUND_GC
bgc_state b_state : 8;
#endif //BACKGROUND_GC
bool found_finalizers;
#ifdef BACKGROUND_GC
bool background_p;
#endif //BACKGROUND_GC
#ifdef STRESS_HEAP
bool stress_induced;
#endif // STRESS_HEAP
#ifdef HOST_64BIT
uint32_t entry_memory_load;
#endif // HOST_64BIT
void store (gc_mechanisms* gm)
{
gc_index = gm->gc_index;
condemned_generation = gm->condemned_generation;
promotion = (gm->promotion != 0);
compaction = (gm->compaction != 0);
loh_compaction = (gm->loh_compaction != 0);
heap_expansion = (gm->heap_expansion != 0);
concurrent = (gm->concurrent != 0);
demotion = (gm->demotion != 0);
card_bundles = (gm->card_bundles != 0);
gen0_reduction_count = gm->gen0_reduction_count;
should_lock_elevation = (gm->should_lock_elevation != 0);
elevation_locked_count = gm->elevation_locked_count;
reason = gm->reason;
pause_mode = gm->pause_mode;
found_finalizers = (gm->found_finalizers != 0);
#ifdef BACKGROUND_GC
background_p = (gm->background_p != 0);
b_state = gm->b_state;
#endif //BACKGROUND_GC
#ifdef STRESS_HEAP
stress_induced = (gm->stress_induced != 0);
#endif // STRESS_HEAP
#ifdef HOST_64BIT
entry_memory_load = gm->entry_memory_load;
#endif // HOST_64BIT
}
};
typedef DPTR(class heap_segment) PTR_heap_segment;
typedef DPTR(class gc_heap) PTR_gc_heap;
typedef DPTR(PTR_gc_heap) PTR_PTR_gc_heap;
#ifdef FEATURE_PREMORTEM_FINALIZATION
typedef DPTR(class CFinalize) PTR_CFinalize;
#endif // FEATURE_PREMORTEM_FINALIZATION
//-------------------------------------
//generation free list. It is an array of free lists bucketed by size, starting at sizes lower than (1 << first_bucket_bits)
//and doubling each time. The last bucket (index == num_buckets) is for largest sizes with no limit
#define MAX_SOH_BUCKET_COUNT (13)//Max number of buckets for the SOH generations.
#define MAX_BUCKET_COUNT (20)//Max number of buckets.
class alloc_list
{
#ifdef DOUBLY_LINKED_FL
uint8_t* added_head;
uint8_t* added_tail;
#endif //DOUBLY_LINKED_FL
uint8_t* head;
uint8_t* tail;
size_t damage_count;
public:
#ifdef FL_VERIFICATION
size_t item_count;
#endif //FL_VERIFICATION
#ifdef DOUBLY_LINKED_FL
uint8_t*& added_alloc_list_head () { return added_head;}
uint8_t*& added_alloc_list_tail () { return added_tail;}
#endif //DOUBLY_LINKED_FL
uint8_t*& alloc_list_head () { return head;}
uint8_t*& alloc_list_tail () { return tail;}
size_t& alloc_list_damage_count(){ return damage_count; }
alloc_list()
{
#ifdef DOUBLY_LINKED_FL
added_head = 0;
added_tail = 0;
#endif //DOUBLY_LINKED_FL
head = 0;
tail = 0;
damage_count = 0;
}
};
#ifdef FEATURE_EVENT_TRACE
struct etw_bucket_info
{
uint16_t index;
uint32_t count;
size_t size;
void set (uint16_t _index, uint32_t _count, size_t _size)
{
index = _index;
count = _count;
size = _size;
}
};
#endif //FEATURE_EVENT_TRACE
#ifdef DYNAMIC_HEAP_COUNT
struct min_fl_list_info
{
uint8_t* head;
uint8_t* tail;
#ifdef DOUBLY_LINKED_FL
void thread_item (uint8_t* item);
#endif //DOUBLY_LINKED_FL
void thread_item_no_prev (uint8_t* item);
};
#endif //DYNAMIC_HEAP_COUNT
class allocator
{
int first_bucket_bits;
unsigned int num_buckets;
alloc_list first_bucket;
alloc_list* buckets;
int gen_number;
alloc_list& alloc_list_of (unsigned int bn);
size_t& alloc_list_damage_count_of (unsigned int bn);
void thread_free_item_end (uint8_t* free_item, uint8_t*& head, uint8_t*& tail, int bn);
public:
allocator (unsigned int num_b, int fbb, alloc_list* b, int gen=-1);
allocator()
{
num_buckets = 1;
first_bucket_bits = sizeof(size_t) * 8 - 1;
// for young gens we just set it to 0 since we don't treat
// them differently from each other
gen_number = 0;
}
unsigned int number_of_buckets()
{
return num_buckets;
}
// skip buckets that cannot possibly fit "size" and return the next one
// there is always such bucket since the last one fits everything
unsigned int first_suitable_bucket (size_t size)
{
// sizes taking first_bucket_bits or less are mapped to bucket 0
// others are mapped to buckets 0, 1, 2 respectively
size = (size >> first_bucket_bits) | 1;
DWORD highest_set_bit_index;
#ifdef HOST_64BIT
BitScanReverse64(&highest_set_bit_index, size);
#else
BitScanReverse(&highest_set_bit_index, size);
#endif
return min ((unsigned int)highest_set_bit_index, (num_buckets - 1));
}
size_t first_bucket_size()
{
return ((size_t)1 << (first_bucket_bits + 1));
}
uint8_t*& alloc_list_head_of (unsigned int bn)
{
return alloc_list_of (bn).alloc_list_head();
}
uint8_t*& alloc_list_tail_of (unsigned int bn)
{
return alloc_list_of (bn).alloc_list_tail();
}
#ifdef DOUBLY_LINKED_FL
uint8_t*& added_alloc_list_head_of (unsigned int bn)
{
return alloc_list_of (bn).added_alloc_list_head();
}
uint8_t*& added_alloc_list_tail_of (unsigned int bn)
{
return alloc_list_of (bn).added_alloc_list_tail();
}
#endif //DOUBLY_LINKED_FL
void clear();
BOOL discard_if_no_fit_p()
{
return (num_buckets == 1);
}
// This is when we know there's nothing to repair because this free
// list has never gone through plan phase. Right now it's only used
// by the background ephemeral sweep when we copy the local free list
// to gen0's free list.
//
// We copy head and tail manually (vs together like copy_to_alloc_list)
// since we need to copy tail first because when we get the free items off
// of each bucket we check head first. We also need to copy the
// smaller buckets first so when gen0 allocation needs to thread
// smaller items back that bucket is guaranteed to have been full
// copied.
void copy_with_no_repair (allocator* allocator_to_copy)
{
assert (num_buckets == allocator_to_copy->number_of_buckets());
for (unsigned int i = 0; i < num_buckets; i++)
{
alloc_list* al = &(allocator_to_copy->alloc_list_of (i));
alloc_list_tail_of(i) = al->alloc_list_tail();
#if !defined(TARGET_AMD64) && !defined(TARGET_X86)
// ensure that the write to the tail is seen by
// the allocating thread *before* the write to the head
MemoryBarrier();
#endif
alloc_list_head_of(i) = al->alloc_list_head();
}
}
void unlink_item (unsigned int bn, uint8_t* item, uint8_t* previous_item, BOOL use_undo_p);
void thread_item (uint8_t* item, size_t size);
void thread_item_front (uint8_t* item, size_t size);
#ifdef DOUBLY_LINKED_FL
int thread_item_front_added (uint8_t* itme, size_t size);
void unlink_item_no_undo (uint8_t* item, size_t size);
void unlink_item_no_undo (unsigned int bn, uint8_t* item);
void unlink_item_no_undo_added (unsigned int bn, uint8_t* item, uint8_t* previous_item);
#endif //DOUBLY_LINKED_FL
#ifdef DYNAMIC_HEAP_COUNT
void count_items (gc_heap* this_hp, size_t* fl_items_count, size_t* fl_items_for_oh_count);
void rethread_items (size_t* num_total_fl_items,
size_t* num_total_fl_items_rethread,
gc_heap* current_heap,
min_fl_list_info* min_fl_list,
size_t* free_list_space_per_heap,
int num_heap);
void merge_items (gc_heap* current_heap, int to_num_heaps, int from_num_heaps);
#endif //DYNAMIC_HEAP_COUNT
void copy_to_alloc_list (alloc_list* toalist);
void copy_from_alloc_list (alloc_list* fromalist);
void commit_alloc_list_changes();
#ifdef USE_REGIONS
void thread_sip_fl (heap_segment* region);
#endif //USE_REGIONS
#ifdef FEATURE_EVENT_TRACE
uint16_t count_largest_items (etw_bucket_info* bucket_info,
size_t max_size,
size_t max_item_count,
size_t* recorded_fl_info_size);
#endif //FEATURE_EVENT_TRACE
#ifdef DOUBLY_LINKED_FL
bool is_doubly_linked_p()
{
return (gen_number == max_generation);
}
#endif //DOUBLY_LINKED_FL
};
#define NUM_GEN_POWER2 (20)
#define BASE_GEN_SIZE (1*512)
// A generation is a per heap concept, ie, each heap has its own gen0/1/2/loh/poh.
class generation
{
public:
// A generation's alloc context is only used during a GC. At the end of a GC, the alloc_ptr and alloc_limit
// of the alloc_context of condemned generations and the old generation (if we are doing an ephemeral GC) are
// always reset to 0 - this allows them to be re-initialized correctly in the next GC that needs this
// alloc context.
alloc_context allocation_context;
// This is the first region on the list of regions this generation includes and is maintained across GCs.
// For gen2 this might be an ro region - so far we've always put all ro regions at the beginning of heap#0's
// gen2 region list. So if there are any ro regions, it means start_segment will be ro and tail_ro_region will be
// non NULL. Otherwise all other regions are rw which are considered normal GC regions.
//
// For all generations we are condemning, this region might change (and very likely will). For generations
// we are not condemning this will remain the same during that GC but we might be adding more regions onto
// the list. For example, during a gen1 GC, we might thread some gen1 regions onto the gen2's region list
// so now they are part of gen2.
//
// If we rearrange regions between heaps, we need to make sure this is updated accordingly.
PTR_heap_segment start_segment;
#ifndef USE_REGIONS
uint8_t* allocation_start;
#endif //!USE_REGIONS
// For all the condemned generations, this is re-inited at the beginning of the plan phase. For the other
// SOH generations, this is maintained across GCs because we don't want to always start at the first region
// in allocate_in_older_generation.
//
// If we rearrange regions between heaps for generations we are not condemning, we should just reset this to the
// first region.
heap_segment* allocation_segment;
// This is only used during a GC - it's updated when we update the alloc_ptr of an alloc context.
uint8_t* allocation_context_start_region;
#ifdef USE_REGIONS
// This is the last region on the list of regions this generation includes and is maintained across GCs.
//
// If we rearrange regions between heaps, we need to make sure this is updated accordingly.
heap_segment* tail_region;
// As mentioned above, only max_generation could have ro regions, and only on heap#0; for other generations
// this will always be 0.
heap_segment* tail_ro_region;
#endif //USE_REGIONS
// This is the free list for this generation and maintained across GCs. When we condemn a generation, the free list
// is rebuilt for that generation. For the non condemned generations this is maintained, for example, when we are
// doing a gen1 GC we will be using gen2's free list.
//
// If we rearrange regions between heaps, we'll need to make sure that the items on the free list are threaded
// into the generation for the new heap, if the region that contains these items are moved to that heap. For example,
// if we move region r0 in gen2 from heap#3 to heap#5, all the items in r0 will need to be taken off of the heap#3's
// gen2 FL and threaded into heap#5's gen2 FL. If we merge multiple heaps into one, we could simply thread all heap's
// FL together (for bucketed ones thread the list from each bucket together).
allocator free_list_allocator;
// The following fields are maintained in the older generation we allocate into, and they are only for diagnostics
// except free_list_allocated which is currently used in generation_allocator_efficiency.
//
// If we rearrange regions between heaps, we will no longer have valid values for these fields unless we just merge
// regions from multiple heaps into one, in which case we can simply combine the values from all heaps.
size_t free_list_allocated;
size_t end_seg_allocated;
size_t condemned_allocated;
size_t sweep_allocated;
// This is only used in a single GC's plan phase.
BOOL allocate_end_seg_p;
// When a generation is condemned, these are re-calculated. For older generations these are maintained across GCs as
// younger generation GCs allocate into this generation's FL.
//
// If we rearrange regions between heaps, we need to adjust these values accordingly. free_list_space can be adjusted
// when we adjust the FL. However, since we don't actually maintain free_obj_space per region and walking an entire
// region just to get free_obj_space is not really worth it, we might just have to live with inaccurate value till
// the next GC that condemns this generation which is okay since this is usually a small value anyway.
size_t free_list_space;
size_t free_obj_space;
// This tracks how much is allocated in this generation so we know when the budget is exceeeded. So it's maintained