forked from dotnet/corert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcpriv.h
4355 lines (3628 loc) · 119 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.
// See the LICENSE file in the project root for more information.
// optimize for speed
#ifndef _DEBUG
#ifdef _MSC_VER
#pragma optimize( "t", on )
#endif
#endif
#define inline __forceinline
#include "gc.h"
//#define DT_LOG
#include "gcrecord.h"
#ifdef _MSC_VER
#pragma warning(disable:4293)
#pragma warning(disable:4477)
#endif //_MSC_VER
inline void FATAL_GC_ERROR()
{
#ifndef DACCESS_COMPILE
GCToOSInterface::DebugBreak();
#endif // DACCESS_COMPILE
_ASSERTE(!"Fatal Error in GC.");
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
}
#ifdef _MSC_VER
#pragma inline_depth(20)
#endif
/* the following section defines the optional features */
// 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 //used to keep "double" objects aligned 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
#ifndef FEATURE_REDHAWK
#define HEAP_ANALYZE
#define COLLECTIBLE_CLASS
#endif // !FEATURE_REDHAWK
#ifdef HEAP_ANALYZE
#define initial_internal_roots (1024*16)
#endif // HEAP_ANALYZE
#define MARK_LIST //used sorted list to speed up plan phase
#define BACKGROUND_GC //concurrent background GC (requires WRITE_WATCH)
#ifdef SERVER_GC
#define MH_SC_MARK //scalable marking
//#define SNOOP_STATS //diagnostic
#define PARALLEL_MARK_LIST_SORT //do the sorting and merging of the multiple mark lists in server gc in parallel
#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 INTERIOR_POINTERS //Allow interior pointers in the code manager
#define CARD_BUNDLE //enable card bundle feature.(requires WRITE_WATCH)
// If this is defined we use a map for segments in order to find the heap for
// a segment fast. But it does use more memory as we have to cover the whole
// heap range and for each entry we allocate a struct of 5 ptr-size words
// (3 for WKS as there's only one heap).
#define SEG_MAPPING_TABLE
// If allocating the heap mapping table for the available VA consumes too
// much memory, you can enable this to allocate only the portion that
// corresponds to rw segments and grow it when needed in grow_brick_card_table.
// However in heap_of you will need to always compare the address with
// g_lowest/highest before you can look at the heap mapping table.
#define GROWABLE_SEG_MAPPING_TABLE
#ifdef BACKGROUND_GC
#define MARK_ARRAY //Mark bit in an array
#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 SHORT_PLUGS //keep plug short
#define FFIND_OBJECT //faster find_object, slower allocation
#define FFIND_DECAY 7 //Number of GC for which fast find will be active
//#define NO_WRITE_BARRIER //no write barrier, use Write Watch feature
//#define DEBUG_WRITE_WATCH //Additional debug for write watch
//#define STRESS_PINNING //Stress pinning by pinning randomly
//#define TRACE_GC //debug trace gc operation
//#define SIMPLE_DPRINTF
//#define TIME_GC //time allocation and garbage collection
//#define TIME_WRITE_WATCH //time GetWriteWatch and ResetWriteWatch calls
//#define COUNT_CYCLES //Use cycle counter for timing
//#define JOIN_STATS //amount of time spent in the join
//also, see TIME_SUSPEND in switches.h.
//#define SYNCHRONIZATION_STATS
//#define SEG_REUSE_STATS
#if defined (SYNCHRONIZATION_STATS) || defined (STAGE_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
#define BEGIN_TIMING(x)
#define END_TIMING(x)
#define BEGIN_TIMING_CYCLES(x)
#define END_TIMING_CYCLES(x)
#endif //SYNCHRONIZATION_STATS || STAGE_STATS
/* End of optional features */
#ifdef GC_CONFIG_DRIVEN
void GCLogConfig (const char *fmt, ... );
#define cprintf(x) {GCLogConfig x;}
#endif //GC_CONFIG_DRIVEN
#ifdef _DEBUG
#define TRACE_GC
#endif
#define NUMBERGENERATIONS 4 //Max number of generations
// 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 BIT64
#define MAX_INDEX_POWER2 30
#else
#define MAX_INDEX_POWER2 26
#endif // BIT64
#else //SERVER_GC
#ifdef BIT64
#define MAX_INDEX_POWER2 28
#else
#define MAX_INDEX_POWER2 24
#endif // BIT64
#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
//Please leave these definitions intact.
#define CLREvent CLREventStatic
// hosted api
#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
extern int print_level;
extern BOOL trace_gc;
extern int gc_trace_fac;
class hlet
{
static hlet* bindings;
int prev_val;
int* pval;
hlet* prev_let;
public:
hlet (int& place, int value)
{
prev_val = place;
pval = &place;
place = value;
prev_let = bindings;
bindings = this;
}
~hlet ()
{
*pval = prev_val;
bindings = prev_let;
}
};
#define let(p,v) hlet __x = hlet (p, v);
#else //TRACE_GC
#define gc_count -1
#define let(s,v)
#endif //TRACE_GC
#ifdef TRACE_GC
#define SEG_REUSE_LOG_0 7
#define SEG_REUSE_LOG_1 (SEG_REUSE_LOG_0 + 1)
#define DT_LOG_0 (SEG_REUSE_LOG_1 + 1)
#define BGC_LOG (DT_LOG_0 + 1)
#define GTC_LOG (DT_LOG_0 + 2)
#define GC_TABLE_LOG (DT_LOG_0 + 3)
#define JOIN_LOG (DT_LOG_0 + 4)
#define SPINLOCK_LOG (DT_LOG_0 + 5)
#define SNOOP_LOG (DT_LOG_0 + 6)
#ifndef DACCESS_COMPILE
#ifdef SIMPLE_DPRINTF
//#define dprintf(l,x) {if (trace_gc && ((l<=print_level)||gc_heap::settings.concurrent)) {printf ("\n");printf x ; fflush(stdout);}}
void GCLog (const char *fmt, ... );
//#define dprintf(l,x) {if (trace_gc && (l<=print_level)) {GCLog x;}}
//#define dprintf(l,x) {if ((l==SEG_REUSE_LOG_0) || (l==SEG_REUSE_LOG_1) || (trace_gc && (l<=3))) {GCLog x;}}
//#define dprintf(l,x) {if (l == DT_LOG_0) {GCLog x;}}
//#define dprintf(l,x) {if (trace_gc && ((l <= 2) || (l == BGC_LOG) || (l==GTC_LOG))) {GCLog x;}}
//#define dprintf(l,x) {if ((l == 1) || (l == 2222)) {GCLog x;}}
#define dprintf(l,x) {if ((l <= 1) || (l == GTC_LOG)) {GCLog x;}}
//#define dprintf(l,x) {if ((l==GTC_LOG) || (l <= 1)) {GCLog x;}}
//#define dprintf(l,x) {if (trace_gc && ((l <= print_level) || (l==GTC_LOG))) {GCLog x;}}
//#define dprintf(l,x) {if (l==GTC_LOG) {printf ("\n");printf x ; fflush(stdout);}}
#else //SIMPLE_DPRINTF
// The GCTrace output goes to stdout by default but can get sent to the stress log or the logfile if the
// reg key GCTraceFacility is set. THe stress log can only take a format string and 4 numbers or
// string literals.
#define dprintf(l,x) {if (trace_gc && (l<=print_level)) { \
if ( !gc_trace_fac) {printf ("\n");printf x ; fflush(stdout);} \
else if ( gc_trace_fac == 2) {LogSpewAlways x;LogSpewAlways ("\n");} \
else if ( gc_trace_fac == 1) {STRESS_LOG_VA(x);}}}
#endif //SIMPLE_DPRINTF
#else //DACCESS_COMPILE
#define dprintf(l,x)
#endif //DACCESS_COMPILE
#else //TRACE_GC
#define dprintf(l,x)
#endif //TRACE_GC
#ifndef FEATURE_REDHAWK
#undef assert
#define assert _ASSERTE
#undef ASSERT
#define ASSERT _ASSERTE
#endif // FEATURE_REDHAWK
#ifdef _DEBUG
struct GCDebugSpinLock {
VOLATILE(int32_t) lock; // -1 if free, 0 if held
VOLATILE(Thread *) holding_thread; // -1 if no thread holds the lock.
VOLATILE(BOOL) released_by_gc_p; // a GC thread released the lock.
GCDebugSpinLock()
: lock(-1), holding_thread((Thread*) -1)
{
}
};
typedef GCDebugSpinLock GCSpinLock;
#elif defined (SYNCHRONIZATION_STATS)
struct GCSpinLockInstru {
VOLATILE(int32_t) lock;
// 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;
GCSpinLockInstru()
: lock(-1), num_switch_thread(0), num_wait_longer(0), num_switch_thread_w(0), num_disable_preemptive_w(0)
{
}
void init()
{
num_switch_thread = 0;
num_wait_longer = 0;
num_switch_thread_w = 0;
num_disable_preemptive_w = 0;
}
};
typedef GCSpinLockInstru GCSpinLock;
#else
struct GCDebugSpinLock {
VOLATILE(int32_t) lock; // -1 if free, 0 if held
GCDebugSpinLock()
: lock(-1)
{
}
};
typedef GCDebugSpinLock GCSpinLock;
#endif
class mark;
class heap_segment;
class CObjectHeader;
class l_heap;
class sorted_table;
class c_synchronize;
class seg_free_spaces;
class gc_heap;
#ifdef BACKGROUND_GC
class exclusive_sync;
class recursive_gc_sync;
#endif //BACKGROUND_GC
// 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.
};
enum gc_tuning_point
{
tuning_deciding_condemned_gen,
tuning_deciding_full_gc,
tuning_deciding_compaction,
tuning_deciding_expansion,
tuning_deciding_promote_ephemeral
};
#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,
a_state_try_fit,
a_state_try_fit_new_seg,
a_state_try_fit_new_seg_after_cg,
a_state_try_fit_no_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
};
#define v_high_memory_load_th 97
//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
uint32_t entry_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 BIT64
uint32_t entry_memory_load;
#endif // BIT64
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 BIT64
entry_memory_load = gm->entry_memory_load;
#endif // BIT64
}
};
#ifdef GC_STATS
// GC specific statistics, tracking counts and timings for GCs occuring in the system.
// This writes the statistics to a file every 60 seconds, if a file is specified in
// COMPlus_GcMixLog
struct GCStatistics
: public StatisticsBase
{
// initialized to the contents of COMPlus_GcMixLog, or NULL, if not present
static TCHAR* logFileName;
static FILE* logFile;
// number of times we executed a background GC, a foreground GC, or a
// non-concurrent GC
int cntBGC, cntFGC, cntNGC;
// min, max, and total time spent performing BGCs, FGCs, NGCs
// (BGC time includes everything between the moment the BGC starts until
// it completes, i.e. the times of all FGCs occuring concurrently)
MinMaxTot bgc, fgc, ngc;
// number of times we executed a compacting GC (sweeping counts can be derived)
int cntCompactNGC, cntCompactFGC;
// count of reasons
int cntReasons[reason_max];
// count of condemned generation, by NGC and FGC:
int cntNGCGen[max_generation+1];
int cntFGCGen[max_generation];
///////////////////////////////////////////////////////////////////////////////////////////////
// Internal mechanism:
virtual void Initialize();
virtual void DisplayAndUpdate();
// Public API
static BOOL Enabled()
{ return logFileName != NULL; }
void AddGCStats(const gc_mechanisms& settings, size_t timeInMSec);
};
extern GCStatistics g_GCStatistics;
extern GCStatistics g_LastGCStatistics;
#endif // GC_STATS
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 first_bucket_size
//and doubling each time. The last bucket (index == num_buckets) is for largest sizes with no limit
#define MAX_BUCKET_COUNT (13)//Max number of buckets for the small generations.
class alloc_list
{
uint8_t* head;
uint8_t* tail;
size_t damage_count;
public:
#ifdef FL_VERIFICATION
size_t item_count;
#endif //FL_VERIFICATION
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()
{
head = 0;
tail = 0;
damage_count = 0;
}
};
class allocator
{
size_t num_buckets;
size_t frst_bucket_size;
alloc_list first_bucket;
alloc_list* buckets;
alloc_list& alloc_list_of (unsigned int bn);
size_t& alloc_list_damage_count_of (unsigned int bn);
public:
allocator (unsigned int num_b, size_t fbs, alloc_list* b);
allocator()
{
num_buckets = 1;
frst_bucket_size = SIZE_T_MAX;
}
unsigned int number_of_buckets() {return (unsigned int)num_buckets;}
size_t first_bucket_size() {return frst_bucket_size;}
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();
}
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();
alloc_list_head_of(i) = al->alloc_list_head();
}
}
void unlink_item (unsigned int bucket_number, 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);
void thread_free_item (uint8_t* free_item, uint8_t*& head, uint8_t*& tail);
void copy_to_alloc_list (alloc_list* toalist);
void copy_from_alloc_list (alloc_list* fromalist);
void commit_alloc_list_changes();
};
#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;
heap_segment* allocation_segment;
PTR_heap_segment start_segment;
uint8_t* allocation_context_start_region;
uint8_t* allocation_start;
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 free_list_space;
size_t free_obj_space;
size_t allocation_size;
uint8_t* plan_allocation_start;
size_t plan_allocation_start_size;
// 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 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
};
// The dynamic data fields are grouped into 3 categories:
//
// calculated logical data (like desired_allocation)
// physical data (like fragmentation)
// const data (like min_gc_size), 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
size_t time_clock; //time when last 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
// min_size is always the same as min_gc_size..
size_t min_gc_size;
size_t max_size;
size_t min_size;
size_t default_new_allocation;
size_t fragmentation_limit;
float fragmentation_burden_limit;
float limit;
float max_limit;
};
#define ro_in_entry 0x1
#ifdef SEG_MAPPING_TABLE
// Note that I am storing both h0 and seg0, even though in Server GC you can get to
// the heap* from the segment info. This is because heap_of needs to be really fast
// and we would not want yet another indirection.
struct seg_mapping
{
// if an address is > boundary it belongs to h1; else h0.
// since we init h0 and h1 to 0, if we get 0 it means that
// address doesn't exist on managed segments. And heap_of
// would just return heap0 which is what it does now.
uint8_t* boundary;
#ifdef MULTIPLE_HEAPS
gc_heap* h0;
gc_heap* h1;
#endif //MULTIPLE_HEAPS
// You could have an address that's inbetween 2 segments and
// this would return a seg, the caller then will use
// in_range_for_segment to determine if it's on that seg.
heap_segment* seg0; // this is what the seg for h0 is.
heap_segment* seg1; // this is what the seg for h1 is.
// Note that when frozen objects are used we mask seg1
// with 0x1 to indicate that there is a ro segment for
// this entry.
};
#endif //SEG_MAPPING_TABLE
// 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;
};
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,
// ran out of space when allocating a large object
awr_loh_oos_bgc = 7,
// waiting for BGC to let FGC happen
awr_fgc_wait_for_bgc = 8,
// wait for bgc to finish to get loh seg.
awr_get_loh_seg = 9,
// we don't allow loh allocation during bgc planning.
awr_loh_alloc_during_plan = 10,
// we don't allow too much loh allocation during bgc.
awr_loh_alloc_during_bgc = 11
};
struct alloc_thread_wait_data
{
int awr;
};
enum msl_take_state
{
mt_get_large_seg,
mt_wait_bgc_plan,
mt_wait_bgc,
mt_block_gc,
mt_clr_mem,
mt_clr_large_mem,
mt_t_eph_gc,
mt_t_full_gc,
mt_alloc_small,
mt_alloc_large,
mt_alloc_small_cant,
mt_alloc_large_cant,
mt_try_alloc,
mt_try_budget
};
enum msl_enter_state
{
me_acquire,
me_release
};
struct spinlock_info
{
msl_enter_state enter_state;
msl_take_state take_state;
EEThreadId thread_id;
};
const unsigned HS_CACHE_LINE_SIZE = 128;
#ifdef SNOOP_STATS
struct snoop_stats_data
{
int heap_index;
// total number of objects that we called
// gc_mark on.
size_t objects_checked_count;
// total number of time we called gc_mark
// on a 0 reference.
size_t zero_ref_count;
// total objects actually marked.
size_t objects_marked_count;
// number of objects written to the mark stack because