forked from eclipse-openj9/openj9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySubSpaceTarok.cpp
More file actions
1665 lines (1407 loc) · 64.3 KB
/
Copy pathMemorySubSpaceTarok.cpp
File metadata and controls
1665 lines (1407 loc) · 64.3 KB
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
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include <math.h>
#include "j9.h"
#include "j9cfg.h"
#include "modronopt.h"
#include "MemorySubSpaceTarok.hpp"
#include "AllocationContextBalanced.hpp"
#include "AllocationContextTarok.hpp"
#include "AllocateDescription.hpp"
#include "CardTable.hpp"
#include "Collector.hpp"
#include "Debug.hpp"
#include "EnvironmentBase.hpp"
#include "GlobalAllocationManagerTarok.hpp"
#include "GlobalCollector.hpp"
#include "Heap.hpp"
#include "HeapRegionDescriptorVLHGC.hpp"
#include "HeapRegionManager.hpp"
#include "HeapStats.hpp"
#include "IncrementalGenerationalGC.hpp"
#include "Math.hpp"
#include "MarkMap.hpp"
#include "MarkMapManager.hpp"
#include "MemoryPool.hpp"
#include "MemorySpace.hpp"
#include "MemorySubSpace.hpp"
#include "MemorySubSpaceRegionIterator.hpp"
#include "ModronTypes.hpp"
#include "ObjectAllocationInterface.hpp"
#include "PhysicalSubArena.hpp"
#define HEAP_FREE_RATIO_EXPAND_DIVISOR 100
#define HEAP_FREE_RATIO_EXPAND_MULTIPLIER 17
#define GMP_OVERHEAD_WEIGHT 0.4
/**
* Return the memory pool associated to the receiver.
* @return MM_MemoryPool
*/
MM_MemoryPool *
MM_MemorySubSpaceTarok::getMemoryPool()
{
Assert_MM_unreachable();
return NULL;
}
/**
* Return the number of memory pools associated to the receiver.
* @return count of number of memory pools
*/
uintptr_t
MM_MemorySubSpaceTarok::getMemoryPoolCount()
{
Assert_MM_unreachable();
return UDATA_MAX;
}
/**
* Return the number of active memory pools associated to the receiver.
* @return count of number of memory pools
*/
uintptr_t
MM_MemorySubSpaceTarok::getActiveMemoryPoolCount()
{
Assert_MM_unreachable();
return UDATA_MAX;
}
/**
* Return the memory pool associated with a given storage location
* @param Address of storage location
* @return MM_MemoryPool
*/
MM_MemoryPool *
MM_MemorySubSpaceTarok::getMemoryPool(void * addr)
{
MM_MemoryPool *pool = NULL;
if (NULL != addr) {
MM_HeapRegionDescriptorVLHGC *descriptor = (MM_HeapRegionDescriptorVLHGC *)_heapRegionManager->tableDescriptorForAddress(addr);
if (descriptor->containsObjects()) {
pool = descriptor->getMemoryPool();
}
}
return pool;
}
/**
* Return the memory pool associated with a given allocation size
* @param Size of allocation request
* @return MM_MemoryPool
*/
MM_MemoryPool *
MM_MemorySubSpaceTarok::getMemoryPool(uintptr_t size)
{
/* this function is only used by ConcurrentSweepScheme, which is disabled in Tarok */
Assert_MM_unreachable();
return NULL;
}
/**
* Return the memory pool associated with a specified range of storage locations.
*
* @param addrBase Low address in specified range
* @param addrTop High address in specified range
* @param highAddr If range spans end of memory pool set to address of first byte
* which does not belong in returned pool.
* @return MM_MemoryPool for storage location addrBase
*/
MM_MemoryPool *
MM_MemorySubSpaceTarok::getMemoryPool(MM_EnvironmentBase *env,
void *addrBase, void *addrTop,
void * &highAddr)
{
MM_MemoryPool *pool = NULL;
if ((NULL != addrBase) && (NULL != addrTop)) {
MM_HeapRegionDescriptorVLHGC *descriptor = (MM_HeapRegionDescriptorVLHGC *)_heapRegionManager->tableDescriptorForAddress(addrBase);
MM_HeapRegionDescriptorVLHGC *highDescriptor = (MM_HeapRegionDescriptorVLHGC *)_heapRegionManager->tableDescriptorForAddress((void *)((uintptr_t)addrTop-1));
/* we can only work on committed regions with in-use memory pools */
if (descriptor->containsObjects()) {
pool = descriptor->getMemoryPool();
if (descriptor != highDescriptor) {
/* they requested a spanning area so set the highAddr to indicate a split */
highAddr = descriptor->getHighAddress();
} else {
/* they requested a single region so set the highAddr to NULL to communicate that it is fully contained in the region */
highAddr = NULL;
}
}
}
return pool;
}
/* ***************************************
* Allocation
* ***************************************
*/
/**
* @copydoc MM_MemorySubSpace::getActualFreeMemorySize()
*/
uintptr_t
MM_MemorySubSpaceTarok::getActualFreeMemorySize()
{
if (isActive()) {
return _globalAllocationManagerTarok->getActualFreeMemorySize();
} else {
return 0;
}
}
/**
* @copydoc MM_MemorySubSpace::getApproximateFreeMemorySize()
*/
uintptr_t
MM_MemorySubSpaceTarok::getApproximateFreeMemorySize()
{
if (isActive()) {
return _globalAllocationManagerTarok->getApproximateFreeMemorySize();
} else {
return 0;
}
}
uintptr_t
MM_MemorySubSpaceTarok::getActiveMemorySize()
{
return getCurrentSize();
}
uintptr_t
MM_MemorySubSpaceTarok::getActiveMemorySize(uintptr_t includeMemoryType)
{
if (getTypeFlags() & includeMemoryType) {
return getCurrentSize();
} else {
return 0;
}
}
uintptr_t
MM_MemorySubSpaceTarok::getActiveLOAMemorySize(uintptr_t includeMemoryType)
{
/* LOA is not supported in Tarok */
return 0;
}
/**
* @copydoc MM_MemorySubSpace::getActualActiveFreeMemorySize()
*/
uintptr_t
MM_MemorySubSpaceTarok::getActualActiveFreeMemorySize()
{
return _globalAllocationManagerTarok->getActualFreeMemorySize();
}
/**
* @copydoc MM_MemorySubSpace::getActualActiveFreeMemorySize(uintptr_t)
*/
uintptr_t
MM_MemorySubSpaceTarok::getActualActiveFreeMemorySize(uintptr_t includeMemoryType)
{
if (getTypeFlags() & includeMemoryType ) {
return _globalAllocationManagerTarok->getActualFreeMemorySize();
} else {
return 0;
}
}
/**
* @copydoc MM_MemorySubSpace::getApproximateActiveFreeMemorySize()
*/
uintptr_t
MM_MemorySubSpaceTarok::getApproximateActiveFreeMemorySize()
{
return _globalAllocationManagerTarok->getApproximateFreeMemorySize();
}
/**
* @copydoc MM_MemorySubSpace::getApproximateActiveFreeMemorySize(uintptr_t)
*/
uintptr_t
MM_MemorySubSpaceTarok::getApproximateActiveFreeMemorySize(uintptr_t includeMemoryType)
{
if (getTypeFlags() & includeMemoryType ) {
return _globalAllocationManagerTarok->getApproximateFreeMemorySize();
} else {
return 0;
}
}
/**
* @copydoc MM_MemorySubSpace::getApproximateActiveFreeLOAMemorySize()
*/
uintptr_t
MM_MemorySubSpaceTarok::getApproximateActiveFreeLOAMemorySize()
{
/* LOA is not supported in Tarok */
return 0;
}
/**
* @copydoc MM_MemorySubSpace::getApproximateActiveFreeLOAMemorySize(uintptr_t)
*/
uintptr_t
MM_MemorySubSpaceTarok::getApproximateActiveFreeLOAMemorySize(uintptr_t includeMemoryType)
{
/* LOA is not supported in Tarok */
return 0;
}
void
MM_MemorySubSpaceTarok::mergeHeapStats(MM_HeapStats *heapStats)
{
Assert_MM_unreachable();
}
void
MM_MemorySubSpaceTarok::mergeHeapStats(MM_HeapStats *heapStats, uintptr_t includeMemoryType)
{
_globalAllocationManagerTarok->mergeHeapStats(heapStats, includeMemoryType);
}
void
MM_MemorySubSpaceTarok::resetHeapStatistics(bool globalCollect)
{
_globalAllocationManagerTarok->resetHeapStatistics(globalCollect);
}
/**
* Return the allocation failure stats for this subSpace.
*/
MM_AllocationFailureStats *
MM_MemorySubSpaceTarok::getAllocationFailureStats()
{
/* this subspace doesn't have a parent so it must have a collector. */
Assert_MM_true(NULL != _collector);
return MM_MemorySubSpace::getAllocationFailureStats();
}
/****************************************
* Allocation
****************************************
*/
void *
MM_MemorySubSpaceTarok::allocateObject(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription, MM_MemorySubSpace *baseSubSpace, MM_MemorySubSpace *previousSubSpace, bool shouldCollectOnFailure)
{
Assert_MM_unreachable();
return NULL;
}
/**
* Allocate the arraylet spine in immortal or scoped memory.
*/
void *
MM_MemorySubSpaceTarok::allocateArrayletLeaf(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription, MM_MemorySubSpace *baseSubSpace, MM_MemorySubSpace *previousSubSpace, bool shouldCollectOnFailure)
{
Assert_MM_unreachable();
return NULL;
}
void *
MM_MemorySubSpaceTarok::allocationRequestFailed(MM_EnvironmentBase *env, MM_AllocateDescription *allocateDescription, AllocationType allocationType, MM_ObjectAllocationInterface *objectAllocationInterface, MM_MemorySubSpace *baseSubSpace, MM_MemorySubSpace *previousSubSpace)
{
Assert_MM_unreachable();
return NULL;
}
#if defined(J9VM_GC_THREAD_LOCAL_HEAP)
void *
MM_MemorySubSpaceTarok::allocateTLH(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription, MM_ObjectAllocationInterface *objectAllocationInterface, MM_MemorySubSpace *baseSubSpace, MM_MemorySubSpace *previousSubSpace, bool shouldCollectOnFailure)
{
Assert_MM_unreachable();
return NULL;
}
#endif /* J9VM_GC_THREAD_LOCAL_HEAP */
/****************************************
* Internal Allocation
****************************************
*/
void *
MM_MemorySubSpaceTarok::collectorAllocate(MM_EnvironmentBase *env, MM_Collector *requestCollector, MM_AllocateDescription *allocDescription)
{
Assert_MM_unreachable();
return NULL;
}
#if defined(J9VM_GC_THREAD_LOCAL_HEAP)
void *
MM_MemorySubSpaceTarok::collectorAllocateTLH(MM_EnvironmentBase *env, MM_Collector *requestCollector, MM_AllocateDescription *allocDescription,
uintptr_t maximumBytesRequired, void * &addrBase, void * &addrTop)
{
Assert_MM_unreachable();
return NULL;
}
#endif /* J9VM_GC_THREAD_LOCAL_HEAP */
void
MM_MemorySubSpaceTarok::abandonHeapChunk(void *addrBase, void *addrTop)
{
if (addrBase != addrTop) {
MM_HeapRegionDescriptorVLHGC *base = (MM_HeapRegionDescriptorVLHGC *)_heapRegionManager->tableDescriptorForAddress(addrBase);
MM_HeapRegionDescriptorVLHGC *verify = (MM_HeapRegionDescriptorVLHGC *)_heapRegionManager->tableDescriptorForAddress((void *)((uintptr_t)addrTop - 1));
Assert_MM_true(base == verify);
/* we can only work on committed regions with in-use memory pools */
Assert_MM_true(base->containsObjects());
base->getMemoryPool()->abandonHeapChunk(addrBase, addrTop);
}
}
/****************************************
* Sub Space Categorization
****************************************
*/
MM_MemorySubSpace *
MM_MemorySubSpaceTarok::getDefaultMemorySubSpace()
{
return this;
}
MM_MemorySubSpace *
MM_MemorySubSpaceTarok::getTenureMemorySubSpace()
{
return this;
}
bool
MM_MemorySubSpaceTarok::isActive()
{
Assert_MM_true(NULL == _parent);
return true;
}
/**
* Ask memory pools if a complete rebuild of freelist is required
*/
bool
MM_MemorySubSpaceTarok::completeFreelistRebuildRequired(MM_EnvironmentBase *env)
{
/*
* this function is only used by MemoryPoolLargeObjects. Since we don't
* support LOA in Tarok we can just return false.
*/
return false;
}
/****************************************
* Free list building
****************************************
*/
void
MM_MemorySubSpaceTarok::reset(MM_EnvironmentBase *env)
{
/* unused in Tarok collectors */
Assert_MM_unreachable();
}
/**
* As opposed to reset, which will empty out, this will fill out as if everything is free
*/
void
MM_MemorySubSpaceTarok::rebuildFreeList(MM_EnvironmentBase *env)
{
Assert_MM_unreachable();
}
void
MM_MemorySubSpaceTarok::resetLargestFreeEntry()
{
_globalAllocationManagerTarok->resetLargestFreeEntry();
Assert_MM_true(NULL == getChildren());
}
void
MM_MemorySubSpaceTarok::recycleRegion(MM_EnvironmentBase *env, MM_HeapRegionDescriptor *region)
{
MM_EnvironmentVLHGC *envVLHGC = (MM_EnvironmentVLHGC *)env;
MM_HeapRegionDescriptorVLHGC *regionStandard = (MM_HeapRegionDescriptorVLHGC *)region;
/* first try to recycle into the original owning context, if there is one */
MM_AllocationContextTarok *context = regionStandard->_allocateData._originalOwningContext;
//TODO: move selection logic into ACT to minimize #ifdefs
if (NULL == context) {
context = regionStandard->_allocateData._owningContext;
}
switch (region->getRegionType()) {
case MM_HeapRegionDescriptor::ADDRESS_ORDERED:
case MM_HeapRegionDescriptor::ADDRESS_ORDERED_MARKED:
/* declare previous mark map cleared, except if the region is arraylet leaf.
* leaving _nextMarkMapCleared unchanged
*/
regionStandard->_previousMarkMapCleared = true;
case MM_HeapRegionDescriptor::ARRAYLET_LEAF:
context->recycleRegion(envVLHGC, regionStandard);
break;
default:
Assert_MM_unreachable();
}
}
uintptr_t
MM_MemorySubSpaceTarok::findLargestFreeEntry(MM_EnvironmentBase *env, MM_AllocateDescription *allocateDescription)
{
return _globalAllocationManagerTarok->getLargestFreeEntry();
}
/**
* Initialization
*/
MM_MemorySubSpaceTarok *
MM_MemorySubSpaceTarok::newInstance(MM_EnvironmentBase *env, MM_PhysicalSubArena *physicalSubArena, MM_GlobalAllocationManagerTarok *gamt, bool usesGlobalCollector, uintptr_t minimumSize, uintptr_t initialSize, uintptr_t maximumSize, uintptr_t memoryType, U_32 objectFlags)
{
MM_MemorySubSpaceTarok *memorySubSpace;
memorySubSpace = (MM_MemorySubSpaceTarok *)env->getForge()->allocate(sizeof(MM_MemorySubSpaceTarok), MM_AllocationCategory::FIXED, J9_GET_CALLSITE());
if (NULL != memorySubSpace) {
MM_HeapRegionManager *heapRegionManager = MM_GCExtensions::getExtensions(env)->heapRegionManager;
new(memorySubSpace) MM_MemorySubSpaceTarok(env, physicalSubArena, gamt, heapRegionManager, usesGlobalCollector, minimumSize, initialSize, maximumSize, memoryType, objectFlags);
if (!memorySubSpace->initialize(env)) {
memorySubSpace->kill(env);
memorySubSpace = NULL;
}
}
return memorySubSpace;
}
bool
MM_MemorySubSpaceTarok::initialize(MM_EnvironmentBase *env)
{
if(!MM_MemorySubSpace::initialize(env)) {
return false;
}
if (!_expandLock.initialize(env, &MM_GCExtensions::getExtensions(env)->lnrlOptions, "MM_MemorySubSpaceTarok:_expandLock")) {
return false;
}
return true;
}
void
MM_MemorySubSpaceTarok::tearDown(MM_EnvironmentBase *env)
{
/* shutdown all regions with pools */
GC_MemorySubSpaceRegionIterator regionIterator(this);
MM_HeapRegionDescriptorVLHGC *region = NULL;
while (NULL != (region = (MM_HeapRegionDescriptorVLHGC*)regionIterator.nextRegion())) {
/* first try to teardown with the original owning context, if there is one */
MM_AllocationContextTarok *context = region->_allocateData._originalOwningContext;
if (NULL == context) {
context = region->_allocateData._owningContext;
}
if (NULL != context) {
context->tearDownRegion(env, region);
}
}
_expandLock.tearDown();
MM_MemorySubSpace::tearDown(env);
}
/**
* Memory described by the range has added to the heap and been made available to the subspace as free memory.
*/
bool
MM_MemorySubSpaceTarok::expanded(
MM_EnvironmentBase *env,
MM_PhysicalSubArena *subArena,
MM_HeapRegionDescriptor *region,
bool canCoalesce)
{
void* regionLowAddress = region->getLowAddress();
void* regionHighAddress = region->getHighAddress();
/* Inform the sub space hierarchy of the size change */
bool result = heapAddRange(env, this, region->getSize(), regionLowAddress, regionHighAddress);
if (result) {
/* Expand the valid range for arraylets. */
MM_GCExtensions::getExtensions(_extensions)->indexableObjectModel.expandArrayletSubSpaceRange(this, regionLowAddress, regionHighAddress, largestDesirableArraySpine());
/* this region should be reserved when we first expand into it */
Assert_MM_true(MM_HeapRegionDescriptor::RESERVED == region->getRegionType());
/* Region about to be set reserved better not to be marked overflowed in GMP or PGC */
Assert_MM_true(0 == ((MM_HeapRegionDescriptorVLHGC *)region)->_markData._overflowFlags);
/* now, mark the region as free and pass it to the region pool for management in its free list */
region->setRegionType(MM_HeapRegionDescriptor::FREE);
((MM_HeapRegionDescriptorVLHGC *)region)->_previousMarkMapCleared = false;
((MM_HeapRegionDescriptorVLHGC *)region)->_nextMarkMapCleared = false;
if (_extensions->tarokEnableExpensiveAssertions) {
/* dirty the mark map (not assert code per se, but helps enhance other asserts when checkBitsForRegion is called) */
MM_MarkMapManager *markMapManager = ((MM_IncrementalGenerationalGC *)_extensions->getGlobalCollector())->getMarkMapManager();
markMapManager->getPartialGCMap()->setBitsForRegion(env, region, false);
markMapManager->getGlobalMarkPhaseMap()->setBitsForRegion(env, region, false);
}
result = _extensions->cardTable->commitCardsForRegion(env, region);
if (result) {
_extensions->cardTable->clearCardsInRange(env, region->getLowAddress(), region->getHighAddress());
_globalAllocationManagerTarok->expand(env, (MM_HeapRegionDescriptorVLHGC *)region);
} else {
heapRemoveRange(env, this, region->getSize(), regionLowAddress, regionHighAddress, NULL, NULL);
}
}
return result;
}
/**
* Memory described by the range which was already part of the heap has been made available to the subspace
* as free memory.
* @note Size information (current) is not updated.
* @warn This routine is fairly hacky - is there a better way?
*/
void
MM_MemorySubSpaceTarok::addExistingMemory(
MM_EnvironmentBase *env,
MM_PhysicalSubArena *subArena,
uintptr_t size,
void *lowAddress,
void *highAddress,
bool canCoalesce)
{
Assert_MM_unreachable();
}
/**
* Memory described by the range which was already part of the heap is being removed from the current memory spaces
* ownership. Adjust accordingly.
* @note Size information (current) is not updated.
*/
void *
MM_MemorySubSpaceTarok::removeExistingMemory(
MM_EnvironmentBase *env,
MM_PhysicalSubArena *subArena,
uintptr_t contractSize,
void *lowAddress,
void *highAddress)
{
/* Routine is normally used to contract within a memory pool (removing free memory) or adjusting barrier ranges, neither of which in this
* configuration are handled here (a contract is a full pool / region removal, and there is no barrier range "change" as a result of removing
* regions).
*/
return lowAddress;
}
MM_HeapRegionDescriptor *
MM_MemorySubSpaceTarok::selectRegionForContraction(MM_EnvironmentBase *env, uintptr_t numaNode)
{
MM_AllocationContextTarok * allocationContext = _globalAllocationManagerTarok->getAllocationContextForNumaNode(numaNode);
Assert_MM_true(NULL != allocationContext);
Assert_MM_true(allocationContext->getNumaNode() == numaNode);
MM_HeapRegionDescriptorVLHGC * region = allocationContext->selectRegionForContraction(env);
return region;
}
void *
MM_MemorySubSpaceTarok::lockedReplenishAndAllocate(MM_EnvironmentBase *env, MM_AllocationContext *context, MM_ObjectAllocationInterface *objectAllocationInterface, MM_AllocateDescription *allocateDescription, AllocationType allocationType)
{
Trc_MM_MemorySubSpaceTarok_lockedReplenishAndAllocate_Entry(env->getLanguageVMThread());
/* cast the context type since we know that we are operating on the Tarok context and we are its friend */
MM_AllocationContextTarok *tarokContext = (MM_AllocationContextTarok *) context;
void *result = tarokContext->lockedReplenishAndAllocate(env, objectAllocationInterface, allocateDescription, allocationType);
if (NULL == result) {
Trc_MM_MemorySubSpaceTarok_lockedReplenishAndAllocate_Failure(env->getLanguageVMThread(), _bytesRemainingBeforeTaxation);
} else {
Trc_MM_MemorySubSpaceTarok_lockedReplenishAndAllocate_Success(env->getLanguageVMThread(), result, _bytesRemainingBeforeTaxation);
}
return result;
}
void
MM_MemorySubSpaceTarok::setBytesRemainingBeforeTaxation(uintptr_t remaining)
{
Trc_MM_setBytesRemainingBeforeTaxation(remaining);
_bytesRemainingBeforeTaxation = remaining;
}
bool
MM_MemorySubSpaceTarok::consumeFromTaxationThreshold(MM_EnvironmentBase *env, uintptr_t bytesToConsume)
{
bool success = false;
/* loop until we either subtract bytesToConsume from _bytesRemainingBeforeTaxation,
* or set _bytesRemainingBeforeTaxation to zero
*/
bool thresholdUpdated = false;
do {
uintptr_t oldBytesRemaining = _bytesRemainingBeforeTaxation;
if (oldBytesRemaining < bytesToConsume) {
_bytesRemainingBeforeTaxation = 0;
success = false;
thresholdUpdated = true;
} else {
uintptr_t newBytesRemaining = oldBytesRemaining - bytesToConsume;
success = true;
thresholdUpdated = (MM_AtomicOperations::lockCompareExchange(&_bytesRemainingBeforeTaxation, oldBytesRemaining, newBytesRemaining) == oldBytesRemaining);
}
} while (!thresholdUpdated);
return success;
}
void *
MM_MemorySubSpaceTarok::replenishAllocationContextFailed(MM_EnvironmentBase *env, MM_MemorySubSpace *replenishingSpace, MM_AllocationContext *context, MM_ObjectAllocationInterface *objectAllocationInterface, MM_AllocateDescription *allocateDescription, AllocationType allocationType)
{
void *result = NULL;
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_Entry(env->getLanguageVMThread(), context, (uintptr_t)allocationType, allocateDescription->getContiguousBytes());
Assert_MM_true(this == replenishingSpace);
/* we currently have no design for handling AC replenishment in a multi-subspace world, so reach for the collector */
MM_IncrementalGenerationalGC *collector = (MM_IncrementalGenerationalGC*)MM_GCExtensions::getExtensions(env)->getGlobalCollector();
Assert_MM_true(NULL != collector);
allocateDescription->saveObjects(env);
if (!env->acquireExclusiveVMAccessForGC(collector, true)) {
allocateDescription->restoreObjects(env);
/* don't have exclusive access - another thread beat us to the GC. retry the allocate using the standard locking path */
result = context->allocate(env, objectAllocationInterface, allocateDescription, allocationType);
if (NULL == result) {
allocateDescription->saveObjects(env);
/* still can't satisfy - grab exclusive at all costs and retry the allocate */
if (!env->acquireExclusiveVMAccessForGC(collector)) {
allocateDescription->restoreObjects(env);
/* now that we have exclusive, see if there is memory to satisfy the allocate (since we might not be the first to get in here) */
result = lockedAllocate(env, context, objectAllocationInterface, allocateDescription, allocationType);
if (NULL != result) {
/* Satisfied the allocate after having grabbed exclusive access to perform a GC (without actually performing the GC). Raise
* an event for tracing / verbose to report the occurrence.
*/
reportAcquiredExclusiveToSatisfyAllocate(env, allocateDescription);
}
} else {
allocateDescription->restoreObjects(env);
}
}
} else {
allocateDescription->restoreObjects(env);
}
if (NULL == result) {
Assert_MM_mustHaveExclusiveVMAccess(env->getOmrVMThread());
/* check if this is a taxation point or a true failure */
if (0 == _bytesRemainingBeforeTaxation) {
allocateDescription->saveObjects(env);
collector->taxationEntryPoint(env, this, allocateDescription);
allocateDescription->restoreObjects(env);
result = lockedAllocate(env, context, objectAllocationInterface, allocateDescription, allocationType);
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_didPerformTaxationAndReplenish(env->getLanguageVMThread(), context, (uintptr_t)allocationType, allocateDescription->getContiguousBytes(), result);
}
}
if (NULL == result) {
Assert_MM_mustHaveExclusiveVMAccess(env->getOmrVMThread());
/* we failed, so this thread will handle the allocation failure */
reportAllocationFailureStart(env, allocateDescription);
/* first, try a resize to satisfy without invoking the collector */
performResize(env, allocateDescription);
result = lockedAllocate(env, context, objectAllocationInterface, allocateDescription, allocationType);
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_didPerformResizeAndReplenish(env->getLanguageVMThread(), context, (uintptr_t)allocationType, allocateDescription->getContiguousBytes(), result);
if (NULL == result) {
/* the resize wasn't enough so invoke the collector */
allocateDescription->saveObjects(env);
allocateDescription->setAllocationType(allocationType);
result = collector->garbageCollect(env, this, allocateDescription, J9MMCONSTANT_IMPLICIT_GC_DEFAULT, objectAllocationInterface, replenishingSpace, context);
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_didPerformCollect(env->getLanguageVMThread(), context, (uintptr_t)allocationType, allocateDescription->getContiguousBytes(), result);
allocateDescription->restoreObjects(env);
if (NULL == result) {
/* we _still_ failed so invoke an aggressive collect */
allocateDescription->saveObjects(env);
result = collector->garbageCollect(env, this, allocateDescription, J9MMCONSTANT_IMPLICIT_GC_AGGRESSIVE, objectAllocationInterface, replenishingSpace, context);
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_didPerformAggressiveCollect(env->getLanguageVMThread(), context, (uintptr_t)allocationType, allocateDescription->getContiguousBytes(), result);
allocateDescription->restoreObjects(env);
}
}
/* allocation failure is over, whether we satisfied the allocate or not */
reportAllocationFailureEnd(env);
}
Trc_MM_MemorySubSpaceTarok_replenishAllocationContextFailed_Exit(env->getLanguageVMThread(), result);
return result;
}
void*
MM_MemorySubSpaceTarok::lockedAllocate(MM_EnvironmentBase *env, MM_AllocationContext *context, MM_ObjectAllocationInterface *objectAllocationInterface, MM_AllocateDescription *allocateDescription, AllocationType allocationType)
{
void* result = NULL;
/* arraylet leaves/shared reserved regions need to be allocated directly after a new region has been found so fall through to the replenish path in that case */
if ((MM_MemorySubSpace::ALLOCATION_TYPE_LEAF != allocationType) && (MM_MemorySubSpace::ALLOCATION_TYPE_SHARED_RESERVED != allocationType)) {
result = ((MM_AllocationContextTarok *)context)->lockedAllocate(env, objectAllocationInterface, allocateDescription, allocationType);
}
if (NULL == result) {
/* we failed so do the locked replenish */
result = lockedReplenishAndAllocate(env, context, objectAllocationInterface, allocateDescription, allocationType);
}
return result;
}
/****************************************
* Expansion/Contraction
****************************************
*/
/**
* Adjust the specified expansion amount by the specified user increment amount (i.e. -Xmoi)
* @return the updated expand size
*/
uintptr_t
MM_MemorySubSpaceTarok::adjustExpansionWithinUserIncrement(MM_EnvironmentBase *env, uintptr_t expandSize)
{
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(env->getOmrVM());
if (extensions->allocationIncrementSetByUser) {
uintptr_t expandIncrement = extensions->allocationIncrement;
/* increment of 0 means no expansion is to occur. Don't round to a size of 0 */
if (0 == expandIncrement) {
return expandSize;
}
/* Round to the Xmoi value */
return MM_Math::roundToCeiling(expandIncrement, expandSize);
}
return MM_MemorySubSpace::adjustExpansionWithinUserIncrement(env, expandSize);
}
/**
* Determine the maximum expansion amount the memory subspace can expand by.
* The amount returned is restricted by values within the receiver of the call, as well as those imposed by
* the parents of the receiver and the owning MemorySpace of the receiver.
*
* @return the amount by which the receiver can expand
*/
uintptr_t
MM_MemorySubSpaceTarok::maxExpansionInSpace(MM_EnvironmentBase *env)
{
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(env->getOmrVM());
uintptr_t expandIncrement = extensions->allocationIncrement;
uintptr_t maxExpandAmount;
if (extensions->allocationIncrementSetByUser) {
/* increment of 0 means no expansion */
if (0 == expandIncrement) {
return 0;
}
}
maxExpandAmount = MM_MemorySubSpace::maxExpansionInSpace(env);
return maxExpandAmount;
}
/**
* Get the size of heap available for contraction.
* Return the amount of heap available to be contracted, factoring in any potential allocate that may require the
* available space.
* @return The amount of heap available for contraction factoring in the size of the allocate (if applicable)
*/
uintptr_t
MM_MemorySubSpaceTarok::getAvailableContractionSize(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription)
{
return _physicalSubArena->getAvailableContractionSize(env, this, allocDescription);
}
uintptr_t
MM_MemorySubSpaceTarok::collectorExpand(MM_EnvironmentBase *env, MM_Collector *requestCollector, MM_AllocateDescription *allocDescription)
{
/* we inherit this collectorExpand method, but don't implement it with this signature. */
Assert_MM_unreachable();
return 0;
}
uintptr_t
MM_MemorySubSpaceTarok::collectorExpand(MM_EnvironmentBase *env)
{
Trc_MM_MemorySubSpaceTarok_collectorExpand_Entry(env->getLanguageVMThread());
_expandLock.acquire();
/* Determine the amount to expand the heap */
uintptr_t expandSize = calculateCollectorExpandSize(env);
Assert_MM_true((0 == expandSize) || (_heapRegionManager->getRegionSize() == expandSize));
_extensions->heap->getResizeStats()->setLastExpandReason(SATISFY_COLLECTOR);
/* expand by a single region */
/* for the most part the code path is not multi-threaded safe, so we do this under expandLock */
uintptr_t expansionAmount= expand(env, expandSize);
Assert_MM_true((0 == expansionAmount) || (expandSize == expansionAmount));
/* Inform the requesting collector that an expand attempt took place (even if the expansion failed) */
MM_IncrementalGenerationalGC *collector = (MM_IncrementalGenerationalGC*)MM_GCExtensions::getExtensions(env)->getGlobalCollector();
Assert_MM_true(NULL != collector);
collector->collectorExpanded(env, this, expansionAmount);
_expandLock.release();
Trc_MM_MemorySubSpaceTarok_collectorExpand_Exit(env->getLanguageVMThread(), expansionAmount);
return expansionAmount;
}
/**
* Perform the contraction/expansion based on decisions made by checkResize.
* Adjustments in contraction size is possible (because compaction might have yielded less then optimal results),
* therefore allocDescriptor is still passed.
* @return the actual amount of resize (having intptr_t return result will contain valid value only if contract/expand size is half of maxOfuintptr_t)
*/
intptr_t
MM_MemorySubSpaceTarok::performResize(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription)
{
uintptr_t oldVMState = env->pushVMstate(OMRVMSTATE_GC_PERFORM_RESIZE);
MM_GCExtensions *extensions = MM_GCExtensions::getExtensions(env);
/* If -Xgc:fvtest=forceTenureResize is specified, then repeat a sequence of 5 expands followed by 5 contracts */
if (extensions->fvtest_forceOldResize) {
uintptr_t resizeAmount = 0;
uintptr_t regionSize = _extensions->regionSize;
resizeAmount = 2*regionSize;
if (5 > extensions->fvtest_oldResizeCounter) {
uintptr_t expansionSize = MM_Math::roundToCeiling(extensions->heapAlignment, resizeAmount);
expansionSize = MM_Math::roundToCeiling(regionSize, expansionSize);
if (canExpand(env, expansionSize)) {
extensions->heap->getResizeStats()->setLastExpandReason(FORCED_NURSERY_EXPAND);
_contractionSize = 0;
_expansionSize = expansionSize;
extensions->fvtest_oldResizeCounter += 1;
}
} else if (10 > extensions->fvtest_oldResizeCounter) {
uintptr_t contractionSize = MM_Math::roundToCeiling(extensions->heapAlignment, resizeAmount);
contractionSize = MM_Math::roundToCeiling(regionSize, contractionSize);
if (canContract(env, contractionSize)) {
_contractionSize = contractionSize;
extensions->heap->getResizeStats()->setLastContractReason(FORCED_NURSERY_CONTRACT);
_expansionSize = 0;
extensions->fvtest_oldResizeCounter += 1;
}
}
if (10 <= extensions->fvtest_oldResizeCounter) {
extensions->fvtest_oldResizeCounter = 0;
}
}
intptr_t resizeAmount = 0;
if (_contractionSize != 0) {
resizeAmount = -(intptr_t)performContract(env, allocDescription);
} else if (_expansionSize != 0) {
resizeAmount = performExpand(env);
}
if (0 == resizeAmount) {
/**
* In case there is no heap resize, check if there is the case that free size is smaller than eden size
* due to the conflict between eden resize and heap resize, recalculateEdenSize if it happens.
*/
uintptr_t freeBytes = _globalAllocationManagerTarok->getFreeRegionCount()*_heapRegionManager->getRegionSize();
MM_IncrementalGenerationalGC *collector = (MM_IncrementalGenerationalGC*)_extensions->getGlobalCollector();
uintptr_t edenSizeInBytes = collector->getCurrentEdenSizeInBytes((MM_EnvironmentVLHGC *)env);
if (edenSizeInBytes > freeBytes) {
collector->recalculateEdenSize((MM_EnvironmentVLHGC *)env);
edenSizeInBytes = collector->getCurrentEdenSizeInBytes((MM_EnvironmentVLHGC *)env);
}
Assert_MM_true(freeBytes >= edenSizeInBytes);
}
env->popVMstate(oldVMState);
return resizeAmount;
}
/**
* Calculate the contraction/expansion size required (if any). Do not perform anything yet.
*/
void
MM_MemorySubSpaceTarok::checkResize(MM_EnvironmentBase *env, MM_AllocateDescription *allocDescription, bool _systemGC)
{
uintptr_t oldVMState = env->pushVMstate(OMRVMSTATE_GC_CHECK_RESIZE);
Trc_MM_MemorySubSpaceTarok_checkResize_1(env->getLanguageVMThread(), _extensions->globalVLHGCStats._heapSizingData.readyToResizeAtGlobalEnd ? "true" : "false");
intptr_t heapSizeChange = calculateHeapSizeChange(env, allocDescription, _systemGC);
intptr_t edenChangeRegions = _extensions->globalVLHGCStats._heapSizingData.edenRegionChange;
intptr_t edenChangeRegionsBytes = edenChangeRegions * (intptr_t)_heapRegionManager->getRegionSize();
Trc_MM_MemorySubSpaceTarok_checkResize_2(env->getLanguageVMThread(), heapSizeChange, edenChangeRegionsBytes);
ExpandReason nonEdenHeapLastExpandReason = _extensions->heap->getResizeStats()->getLastExpandReason();
ContractReason nonEdenHeapLastContractReason = _extensions->heap->getResizeStats()->getLastContractReason();
if (edenChangeRegionsBytes != 0) {
/*
* Report eden sizing by itself, as well as why eden is being resized.
* When contract/expand is actually performed, VGC will report the -overall- change in heap size, and report it accordingly.
* This -overall- change in heap size, is change in eden size + change in non-eden size
*/
if (edenChangeRegionsBytes > 0) {
_extensions->heap->getResizeStats()->setLastExpandReason(EDEN_EXPANDING);
reportHeapResizeAttempt(env, edenChangeRegionsBytes, HEAP_EXPAND, MEMORY_TYPE_NEW);
} else if (edenChangeRegionsBytes < 0) {
_extensions->heap->getResizeStats()->setLastContractReason(EDEN_CONTRACTING);
reportHeapResizeAttempt(env, (edenChangeRegionsBytes * -1), HEAP_CONTRACT, MEMORY_TYPE_NEW);
}