-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
gcpriv.h
6153 lines (5205 loc) · 180 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"
#ifdef _MSC_VER
#pragma warning(disable:4293)
#pragma warning(disable:4477)
#endif //_MSC_VER
inline void FATAL_GC_ERROR()
{
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 enable regions by default for only StandAlone GC builds
#if defined (HOST_64BIT) && defined (BUILD_AS_STANDALONE)
#define USE_REGIONS
#endif //HOST_64BIT && BUILD_AS_STANDALONE
#ifdef USE_REGIONS
// Currently this -
// + creates some pins on our own
// + creates some ro segs
// We can add more mechanisms here.
//#define STRESS_REGIONS
#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_REDHAWK
#define HEAP_ANALYZE
#define COLLECTIBLE_CLASS
#endif // !FEATURE_REDHAWK
#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
#ifndef MAX_LONGPATH
#define MAX_LONGPATH 1024
#endif // MAX_LONGPATH
//#define TRACE_GC
//#define SIMPLE_DPRINTF
//#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)
#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;
#ifdef TRACE_GC
#define MIN_CUSTOM_LOG_LEVEL 7
#define SEG_REUSE_LOG_0 (MIN_CUSTOM_LOG_LEVEL)
#define SEG_REUSE_LOG_1 (MIN_CUSTOM_LOG_LEVEL + 1)
#define DT_LOG_0 (MIN_CUSTOM_LOG_LEVEL + 2)
#define BGC_TUNING_LOG (MIN_CUSTOM_LOG_LEVEL + 3)
#define GTC_LOG (MIN_CUSTOM_LOG_LEVEL + 4)
#define GC_TABLE_LOG (MIN_CUSTOM_LOG_LEVEL + 5)
#define JOIN_LOG (MIN_CUSTOM_LOG_LEVEL + 6)
#define SPINLOCK_LOG (MIN_CUSTOM_LOG_LEVEL + 7)
#define SNOOP_LOG (MIN_CUSTOM_LOG_LEVEL + 8)
#define REGIONS_LOG (MIN_CUSTOM_LOG_LEVEL + 9)
// NOTE! This is for HEAP_BALANCE_INSTRUMENTATION
// This particular one is special and needs to be well formatted because we
// do post processing on it with tools\GCLogParser. If you need to add some
// detail to help with investigation that's not 't processed by tooling
// prefix it with TEMP so that line will be written to the results as is in
// the result. I have some already logged with HEAP_BALANCE_TEMP_LOG.
#define HEAP_BALANCE_LOG (MIN_CUSTOM_LOG_LEVEL + 10)
#define HEAP_BALANCE_TEMP_LOG (MIN_CUSTOM_LOG_LEVEL + 11)
#ifdef SIMPLE_DPRINTF
void GCLog (const char *fmt, ... );
#define dprintf(l,x) {if ((l == 1) || (l == GTC_LOG)) {GCLog x;}}
#else //SIMPLE_DPRINTF
// Nobody used the logging mechanism that used to be here. If we find ourselves
// wanting to inspect GC logs on unmodified builds, we can use this define here
// to do so.
//#define dprintf(l, x)
#define dprintf(l,x) STRESS_LOG_VA(l,x);
#endif //SIMPLE_DPRINTF
#else //TRACE_GC
#define dprintf(l,x)
#endif //TRACE_GC
#if !defined(FEATURE_REDHAWK) && !defined(BUILD_AS_STANDALONE)
#undef assert
#define assert _ASSERTE
#undef ASSERT
#define ASSERT _ASSERTE
#endif // FEATURE_REDHAWK
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
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 (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;
#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 Charactaristics |
+==========+====================+=======================================+
| 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,
none = 3,
total_oh_count = 4
};
gc_oh_num gen_to_oh (int gen);
#if defined(TRACE_GC) && defined(BACKGROUND_GC)
static const char * const str_bgc_state[] =
{
"not_in_process",
"mark_handles",
"mark_stack",
"revisit_soh",
"revisit_loh",
"overflow_soh",
"overflow_loh",
"final_marking",
"sweep_soh",
"sweep_loh",
"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 gc_type
{
gc_type_compacting = 0,
gc_type_blocking = 1,
#ifdef BACKGROUND_GC
gc_type_background = 2,
#endif //BACKGROUND_GC
gc_type_max = 3
};
//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;
BOOL allocations_allowed;
#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;
etw_bucket_info() {}
void set (uint16_t _index, uint32_t _count, size_t _size)
{
index = _index;
count = _count;
size = _size;
}
};
#endif //FEATURE_EVENT_TRACE
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* itme, 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, size_t size);
void unlink_item_no_undo_added (unsigned int bn, uint8_t* item, uint8_t* previous_item);
#endif //DOUBLY_LINKED_FL
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
};
#define NUM_GEN_POWER2 (20)
#define BASE_GEN_SIZE (1*512)
// group the frequently used ones together (need intrumentation on accessors)
class generation
{
public:
// Don't move these first two fields without adjusting the references
// from the __asm in jitinterface.cpp.
alloc_context allocation_context;
PTR_heap_segment start_segment;
#ifndef USE_REGIONS
uint8_t* allocation_start;
#endif //!USE_REGIONS
heap_segment* allocation_segment;
uint8_t* allocation_context_start_region;
#ifdef USE_REGIONS
heap_segment* tail_region;
heap_segment* plan_start_segment;
// only max_generation could have ro regions; for other generations
// this will be 0.
heap_segment* tail_ro_region;
#endif //USE_REGIONS
allocator free_list_allocator;
size_t free_list_allocated;
size_t end_seg_allocated;
BOOL allocate_end_seg_p;
size_t condemned_allocated;
size_t sweep_allocated;
size_t free_list_space;
size_t free_obj_space;
size_t allocation_size;
#ifndef USE_REGIONS
uint8_t* plan_allocation_start;
size_t plan_allocation_start_size;
#endif //!USE_REGIONS
// this is the pinned plugs that got allocated into this gen.
size_t pinned_allocated;
size_t pinned_allocation_compact_size;
size_t pinned_allocation_sweep_size;
int gen_num;
#ifdef DOUBLY_LINKED_FL
BOOL set_bgc_mark_bit_p;
uint8_t* last_free_list_allocated;
#endif //DOUBLY_LINKED_FL
#ifdef FREE_USAGE_STATS
size_t gen_free_spaces[NUM_GEN_POWER2];
// these are non pinned plugs only
size_t gen_plugs[NUM_GEN_POWER2];
size_t gen_current_pinned_free_spaces[NUM_GEN_POWER2];
size_t pinned_free_obj_space;
// this is what got allocated into the pinned free spaces.
size_t allocated_in_pinned_free;
size_t allocated_since_last_pin;
#endif //FREE_USAGE_STATS
};
// static data remains the same after it's initialized.
// It's per generation.
// TODO: for gen_time_tuning, we should put the multipliers in static data.
struct static_data
{
size_t min_size;
size_t max_size;
size_t fragmentation_limit;
float fragmentation_burden_limit;
float limit;
float max_limit;
uint64_t time_clock; // time after which to collect generation, in performance counts (see QueryPerformanceCounter)
size_t gc_clock; // nubmer of gcs after which to collect generation
};
// The dynamic data fields are grouped into 3 categories:
//
// calculated logical data (like desired_allocation)
// physical data (like fragmentation)
// const data (sdata), initialized at the beginning
class dynamic_data
{
public:
ptrdiff_t new_allocation;
ptrdiff_t gc_new_allocation; // new allocation at beginning of gc
float surv;
size_t desired_allocation;
// # of bytes taken by objects (ie, not free space) at the beginning
// of the GC.
size_t begin_data_size;
// # of bytes taken by survived objects after mark.
size_t survived_size;
// # of bytes taken by survived pinned plugs after mark.
size_t pinned_survived_size;
size_t artificial_pinned_survived_size;
size_t added_pinned_size;
#ifdef SHORT_PLUGS
size_t padding_size;
#endif //SHORT_PLUGS
#if defined (RESPECT_LARGE_ALIGNMENT) || defined (FEATURE_STRUCTALIGN)
// # of plugs that are not pinned plugs.
size_t num_npinned_plugs;
#endif //RESPECT_LARGE_ALIGNMENT || FEATURE_STRUCTALIGN
//total object size after a GC, ie, doesn't include fragmentation
size_t current_size;
size_t collection_count;
size_t promoted_size;
size_t freach_previous_promotion;
size_t fragmentation; //fragmentation when we don't compact
size_t gc_clock; //gc# when last GC happened
uint64_t time_clock; //time when last gc started
uint64_t previous_time_clock; // time when previous gc started
size_t gc_elapsed_time; // Time it took for the gc to complete
float gc_speed; // speed in bytes/msec for the gc to complete
size_t min_size;
static_data* sdata;
};
struct recorded_generation_info
{
size_t size_before;
size_t fragmentation_before;
size_t size_after;
size_t fragmentation_after;
};
struct last_recorded_gc_info
{
VOLATILE(size_t) index;
size_t total_committed;
size_t promoted;
size_t pinned_objects;
size_t finalize_promoted_objects;
size_t pause_durations[2];
float pause_percentage;
recorded_generation_info gen_info[total_generation_count];
size_t heap_size;
size_t fragmentation;
uint32_t memory_load;
uint8_t condemned_generation;
bool compaction;
bool concurrent;
};
// alignment helpers
//Alignment constant for allocation
#define ALIGNCONST (DATA_ALIGNMENT-1)
inline
size_t Align (size_t nbytes, int alignment=ALIGNCONST)
{
return (nbytes + alignment) & ~alignment;
}
//return alignment constant for small object heap vs large object heap
inline
int get_alignment_constant (BOOL small_object_p)
{
#ifdef FEATURE_STRUCTALIGN
// If any objects on the large object heap require 8-byte alignment,
// the compiler will tell us so. Let's not guess an alignment here.
return ALIGNCONST;
#else // FEATURE_STRUCTALIGN
return small_object_p ? ALIGNCONST : 7;
#endif // FEATURE_STRUCTALIGN
}
struct etw_opt_info
{
size_t desired_allocation;
size_t new_allocation;
int gen_number;
};
// Note, I am not removing the ones that are no longer used
// because the older versions of the runtime still use them
// and ETW interprets them.
enum alloc_wait_reason
{
// When we don't care about firing an event for
// this.
awr_ignored = -1,
// when we detect we are in low memory
awr_low_memory = 0,
// when we detect the ephemeral segment is too full
awr_low_ephemeral = 1,
// we've given out too much budget for gen0.
awr_gen0_alloc = 2,
// we've given out too much budget for loh.
awr_loh_alloc = 3,
// this event is really obsolete - it's for pre-XP
// OSs where low mem notification is not supported.
awr_alloc_loh_low_mem = 4,
// we ran out of VM spaced to reserve on loh.
awr_loh_oos = 5,
// ran out of space when allocating a small object
awr_gen0_oos_bgc = 6,