-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathMarkingSchemeRootClearer.cpp
459 lines (417 loc) · 18.8 KB
/
MarkingSchemeRootClearer.cpp
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
/*******************************************************************************
* 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 "j9.h"
#include "j9cfg.h"
#include "j9consts.h"
#include "ModronAssertions.h"
#if defined(J9VM_GC_FINALIZATION)
#include "CollectorLanguageInterfaceImpl.hpp"
#endif /* defined(J9VM_GC_FINALIZATION) */
#include "ConfigurationDelegate.hpp"
#include "EnvironmentBase.hpp"
#include "FinalizableObjectBuffer.hpp"
#include "FinalizableReferenceBuffer.hpp"
#include "GlobalCollector.hpp"
#include "Heap.hpp"
#include "HeapRegionDescriptorStandard.hpp"
#include "HeapRegionIteratorStandard.hpp"
#include "HeapRegionManager.hpp"
#include "MarkingScheme.hpp"
#include "MarkingSchemeRootClearer.hpp"
#include "ModronAssertions.h"
#include "OwnableSynchronizerObjectBuffer.hpp"
#include "ContinuationObjectBufferStandard.hpp"
#include "ParallelDispatcher.hpp"
#include "ReferenceObjectBuffer.hpp"
#include "ReferenceStats.hpp"
#include "RootScanner.hpp"
#include "StackSlotValidator.hpp"
#include "UnfinalizedObjectBuffer.hpp"
#if JAVA_SPEC_VERSION >= 19
#include "ContinuationHelpers.hpp"
#endif /* JAVA_SPEC_VERSION >= 19 */
void
MM_MarkingSchemeRootClearer::doSlot(omrobjectptr_t *slotPtr)
{
Assert_MM_unreachable();
}
void
MM_MarkingSchemeRootClearer::doClass(J9Class *clazz)
{
Assert_MM_unreachable();
}
void
MM_MarkingSchemeRootClearer::doFinalizableObject(omrobjectptr_t object)
{
Assert_MM_unreachable();
}
void
MM_MarkingSchemeRootClearer::scanWeakReferenceObjects(MM_EnvironmentBase *env)
{
reportScanningStarted(RootScannerEntity_WeakReferenceObjects);
GC_Environment *gcEnv = env->getGCEnvironment();
Assert_MM_true(gcEnv->_referenceObjectBuffer->isEmpty());
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
/* NOTE: we can't look at the list to determine if there's work to do since another thread may have already processed it and deleted everything */
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
MM_ReferenceObjectList *list = ®ionExtension->_referenceObjectLists[i];
list->startWeakReferenceProcessing();
if (!list->wasWeakListEmpty()) {
_markingDelegate->processReferenceList(env, region, list->getPriorWeakList(), &gcEnv->_markJavaStats._weakReferenceStats);
}
}
}
}
Assert_MM_true(gcEnv->_referenceObjectBuffer->isEmpty());
reportScanningEnded(RootScannerEntity_WeakReferenceObjects);
}
MM_RootScanner::CompletePhaseCode
MM_MarkingSchemeRootClearer::scanWeakReferencesComplete(MM_EnvironmentBase *env)
{
/* No new objects could have been discovered by soft / weak reference processing,
* but we must complete this phase prior to unfinalized processing to ensure that
* finalizable referents get cleared */
env->_currentTask->synchronizeGCThreads(env, UNIQUE_ID);
return complete_phase_OK;
}
void
MM_MarkingSchemeRootClearer::scanSoftReferenceObjects(MM_EnvironmentBase *env)
{
reportScanningStarted(RootScannerEntity_SoftReferenceObjects);
GC_Environment *gcEnv = env->getGCEnvironment();
Assert_MM_true(gcEnv->_referenceObjectBuffer->isEmpty());
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
/* NOTE: we can't look at the list to determine if there's work to do since another thread may have already processed it and deleted everything */
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
MM_ReferenceObjectList *list = ®ionExtension->_referenceObjectLists[i];
list->startSoftReferenceProcessing();
if (!list->wasSoftListEmpty()) {
_markingDelegate->processReferenceList(env, region, list->getPriorSoftList(), &gcEnv->_markJavaStats._softReferenceStats);
}
}
}
}
Assert_MM_true(gcEnv->_referenceObjectBuffer->isEmpty());
reportScanningEnded(RootScannerEntity_SoftReferenceObjects);
}
MM_RootScanner::CompletePhaseCode
MM_MarkingSchemeRootClearer::scanSoftReferencesComplete(MM_EnvironmentBase *env)
{
/* do nothing -- no new objects could have been discovered by soft reference processing */
return complete_phase_OK;
}
void
MM_MarkingSchemeRootClearer::scanPhantomReferenceObjects(MM_EnvironmentBase *env)
{
reportScanningStarted(RootScannerEntity_PhantomReferenceObjects);
/* ensure that all _referenceObjectBuffers are flushed before phantom references
* are processed since scanning unfinalizedObjects may resurrect a phantom reference
*/
GC_Environment *gcEnv = env->getGCEnvironment();
gcEnv->_referenceObjectBuffer->flush(env);
env->_currentTask->synchronizeGCThreads(env, UNIQUE_ID);
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
/* NOTE: we can't look at the list to determine if there's work to do since another thread may have already processed it and deleted everything */
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
MM_ReferenceObjectList *list = ®ionExtension->_referenceObjectLists[i];
list->startPhantomReferenceProcessing();
if (!list->wasPhantomListEmpty()) {
_markingDelegate->processReferenceList(env, region, list->getPriorPhantomList(), &gcEnv->_markJavaStats._phantomReferenceStats);
}
}
}
}
Assert_MM_true(gcEnv->_referenceObjectBuffer->isEmpty());
reportScanningEnded(RootScannerEntity_PhantomReferenceObjects);
}
MM_RootScanner::CompletePhaseCode
MM_MarkingSchemeRootClearer::scanPhantomReferencesComplete(MM_EnvironmentBase *env)
{
reportScanningStarted(RootScannerEntity_PhantomReferenceObjectsComplete);
if (env->_currentTask->synchronizeGCThreadsAndReleaseSingleThread(env, UNIQUE_ID)) {
env->_cycleState->_referenceObjectOptions |= MM_CycleState::references_clear_phantom;
env->_currentTask->releaseSynchronizedGCThreads(env);
}
/* phantom reference processing may resurrect objects - scan them now */
_markingScheme->completeMarking(env);
reportScanningEnded(RootScannerEntity_PhantomReferenceObjectsComplete);
return complete_phase_OK;
}
void
MM_MarkingSchemeRootClearer::scanUnfinalizedObjects(MM_EnvironmentBase *env)
{
if (_markingDelegate->shouldScanUnfinalizedObjects()) {
/* allow the marking scheme to handle this */
reportScanningStarted(RootScannerEntity_UnfinalizedObjects);
GC_Environment *gcEnv = env->getGCEnvironment();
GC_FinalizableObjectBuffer buffer(_extensions);
#if defined(J9VM_GC_FINALIZATION)
bool finalizationRequired = false;
#endif /* defined(J9VM_GC_FINALIZATION) */
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
MM_UnfinalizedObjectList *list = ®ionExtension->_unfinalizedObjectLists[i];
if (!list->wasEmpty()) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
omrobjectptr_t object = list->getPriorList();
while (NULL != object) {
gcEnv->_markJavaStats._unfinalizedCandidates += 1;
omrobjectptr_t next = _extensions->accessBarrier->getFinalizeLink(object);
if (_markingScheme->inlineMarkObject(env, object)) {
/* object was not previously marked -- it is now finalizable so push it to the local buffer */
buffer.add(env, object);
gcEnv->_markJavaStats._unfinalizedEnqueued += 1;
#if defined(J9VM_GC_FINALIZATION)
/* inform global GC if finalization is required */
if (!finalizationRequired) {
MM_GlobalCollector *globalCollector = (MM_GlobalCollector *)_extensions->getGlobalCollector();
globalCollector->getGlobalCollectorDelegate()->setFinalizationRequired();
finalizationRequired = true;
}
#endif /* defined(J9VM_GC_FINALIZATION) */
} else {
/* object was already marked. It is still unfinalized */
gcEnv->_unfinalizedObjectBuffer->add(env, object);
}
object = next;
}
}
}
}
}
/* Flush the local buffer of finalizable objects to the global list */
buffer.flush(env);
/* restore everything to a flushed state before exiting */
gcEnv->_unfinalizedObjectBuffer->flush(env);
reportScanningEnded(RootScannerEntity_UnfinalizedObjects);
}
}
MM_RootScanner::CompletePhaseCode
MM_MarkingSchemeRootClearer::scanUnfinalizedObjectsComplete(MM_EnvironmentBase *env)
{
if (_markingDelegate->shouldScanUnfinalizedObjects()) {
reportScanningStarted(RootScannerEntity_UnfinalizedObjectsComplete);
/* ensure that all unfinalized processing is complete before we start marking additional objects */
env->_currentTask->synchronizeGCThreads(env, UNIQUE_ID);
/* TODO: consider relaxing completeMarking into completeScan (which will skip over 'complete class marking' step).
* This is to avoid potentially unnecessary sync point in class marking step. The class marking step that we do after
* phantom processing might be sufficient.
*/
_markingScheme->completeMarking(env);
reportScanningEnded(RootScannerEntity_UnfinalizedObjectsComplete);
}
return complete_phase_OK;
}
void
MM_MarkingSchemeRootClearer::scanOwnableSynchronizerObjects(MM_EnvironmentBase *env)
{
if (_markingDelegate->shouldScanOwnableSynchronizerObjects()) {
/* allow the marking scheme to handle this */
reportScanningStarted(RootScannerEntity_OwnableSynchronizerObjects);
GC_Environment *gcEnv = env->getGCEnvironment();
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
MM_OwnableSynchronizerObjectList *list = ®ionExtension->_ownableSynchronizerObjectLists[i];
if (!list->wasEmpty()) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
omrobjectptr_t object = list->getPriorList();
while (NULL != object) {
gcEnv->_markJavaStats._ownableSynchronizerCandidates += 1;
omrobjectptr_t next = _extensions->accessBarrier->getOwnableSynchronizerLink(object);
if (_markingScheme->isMarked(object)) {
/* object was already marked. */
gcEnv->_ownableSynchronizerObjectBuffer->add(env, object);
} else {
/* object was not previously marked */
gcEnv->_markJavaStats._ownableSynchronizerCleared += 1;
}
object = next;
}
}
}
}
#if defined(J9VM_GC_MODRON_SCAVENGER)
/* correct scavenger statistics for ownableSynchronizerObjects, adjust survived ownableSynchronizerObject count in Nursery, only in generational gc */
if (_extensions->scavengerEnabled && (MEMORY_TYPE_NEW == (region->getTypeFlags() & MEMORY_TYPE_NEW))) {
gcEnv->_scavengerJavaStats.updateOwnableSynchronizerNurseryCounts(gcEnv->_markJavaStats._ownableSynchronizerCandidates - gcEnv->_markJavaStats._ownableSynchronizerCleared);
}
#endif /* defined(J9VM_GC_MODRON_SCAVENGER) */
}
/* restore everything to a flushed state before exiting */
gcEnv->_ownableSynchronizerObjectBuffer->flush(env);
reportScanningEnded(RootScannerEntity_OwnableSynchronizerObjects);
}
}
void
MM_MarkingSchemeRootClearer::scanContinuationObjects(MM_EnvironmentBase *env)
{
#if JAVA_SPEC_VERSION >= 19
if (_markingDelegate->shouldScanContinuationObjects()) {
/* allow the marking scheme to handle this */
reportScanningStarted(RootScannerEntity_ContinuationObjects);
GC_Environment *gcEnv = env->getGCEnvironment();
bool const compressed = _extensions->compressObjectReferences();
MM_HeapRegionDescriptorStandard *region = NULL;
GC_HeapRegionIteratorStandard regionIterator(_extensions->heap->getHeapRegionManager());
while (NULL != (region = regionIterator.nextRegion())) {
MM_HeapRegionDescriptorStandardExtension *regionExtension = MM_ConfigurationDelegate::getHeapRegionDescriptorStandardExtension(env, region);
for (uintptr_t i = 0; i < regionExtension->_maxListIndex; i++) {
MM_ContinuationObjectList *list = ®ionExtension->_continuationObjectLists[i];
if (!list->wasEmpty()) {
if (J9MODRON_HANDLE_NEXT_WORK_UNIT(env)) {
omrobjectptr_t object = list->getPriorList();
while (NULL != object) {
gcEnv->_markJavaStats._continuationCandidates += 1;
omrobjectptr_t next = _extensions->accessBarrier->getContinuationLink(object);
#if defined(OMR_GC_CONCURRENT_SCAVENGER)
{
/**
* In case of CS backout make sure the list contains the new version of the objects or if there is only one version,
* due to being self-forwarded, restore the self-forwarded bit.
*/
MM_ForwardedHeader forwardHeader(object, compressed);
omrobjectptr_t forwardPtr = forwardHeader.getNonStrictForwardedObject();
if (NULL != forwardPtr) {
Assert_MM_true(_extensions->isConcurrentScavengerEnabled() && _extensions->isScavengerBackOutFlagRaised());
Assert_MM_false(_markingScheme->isMarked(object));
if (forwardHeader.isSelfForwardedPointer()) {
forwardHeader.restoreSelfForwardedPointer();
} else {
object = forwardPtr;
}
}
}
#endif /* OMR_GC_CONCURRENT_SCAVENGER */
if (_markingScheme->isMarked(object) && !VM_ContinuationHelpers::isFinished(*VM_ContinuationHelpers::getContinuationStateAddress((J9VMThread *)env->getLanguageVMThread() , object))) {
/* object was already marked. */
gcEnv->_continuationObjectBuffer->add(env, object);
} else {
/* object was not previously marked */
gcEnv->_markJavaStats._continuationCleared += 1;
_extensions->releaseNativesForContinuationObject(env, object);
}
object = next;
}
}
}
}
}
/* restore everything to a flushed state before exiting */
gcEnv->_continuationObjectBuffer->flush(env);
reportScanningEnded(RootScannerEntity_ContinuationObjects);
}
#endif /* JAVA_SPEC_VERSION >= 19 */
}
void
MM_MarkingSchemeRootClearer::iterateAllContinuationObjects(MM_EnvironmentBase *env)
{
if (_markingDelegate->shouldScanContinuationObjects()) {
reportScanningStarted(RootScannerEntity_ContinuationObjectsComplete);
MM_ContinuationObjectBufferStandard::iterateAllContinuationObjects(env);
reportScanningEnded(RootScannerEntity_ContinuationObjectsComplete);
}
}
void
MM_MarkingSchemeRootClearer::doMonitorReference(J9ObjectMonitor *objectMonitor, GC_HashTableIterator *monitorReferenceIterator)
{
J9ThreadAbstractMonitor *monitor = (J9ThreadAbstractMonitor*)objectMonitor->monitor;
_env->getGCEnvironment()->_markJavaStats._monitorReferenceCandidates += 1;
if (!_markingScheme->isMarked((omrobjectptr_t )monitor->userData)) {
monitorReferenceIterator->removeSlot();
_env->getGCEnvironment()->_markJavaStats._monitorReferenceCleared += 1;
/* We must call objectMonitorDestroy (as opposed to omrthread_monitor_destroy) when the
* monitor is not internal to the GC */
_javaVM->internalVMFunctions->objectMonitorDestroy(_javaVM, (J9VMThread *)_env->getLanguageVMThread(), (omrthread_monitor_t)monitor);
}
}
MM_RootScanner::CompletePhaseCode
MM_MarkingSchemeRootClearer::scanMonitorReferencesComplete(MM_EnvironmentBase *env)
{
reportScanningStarted(RootScannerEntity_MonitorReferenceObjectsComplete);
_javaVM->internalVMFunctions->objectMonitorDestroyComplete(_javaVM, (J9VMThread *)env->getLanguageVMThread());
reportScanningEnded(RootScannerEntity_MonitorReferenceObjectsComplete);
return complete_phase_OK;
}
void
MM_MarkingSchemeRootClearer::doJNIWeakGlobalReference(omrobjectptr_t *slotPtr)
{
omrobjectptr_t objectPtr = *slotPtr;
if ((NULL != objectPtr) && !_markingScheme->isMarked(objectPtr)) {
*slotPtr = NULL;
}
}
void
MM_MarkingSchemeRootClearer::doRememberedSetSlot(omrobjectptr_t *slotPtr, GC_RememberedSetSlotIterator *rememberedSetSlotIterator)
{
omrobjectptr_t objectPtr = *slotPtr;
if (objectPtr == NULL) {
rememberedSetSlotIterator->removeSlot();
} else if (!_markingScheme->isMarked(objectPtr)) {
_extensions->objectModel.clearRemembered(objectPtr);
rememberedSetSlotIterator->removeSlot();
}
}
void
MM_MarkingSchemeRootClearer::doStringTableSlot(omrobjectptr_t *slotPtr, GC_StringTableIterator *stringTableIterator)
{
_env->getGCEnvironment()->_markJavaStats._stringConstantsCandidates += 1;
if (!_markingScheme->isMarked(*slotPtr)) {
_env->getGCEnvironment()->_markJavaStats._stringConstantsCleared += 1;
stringTableIterator->removeSlot();
}
}
/**
* @Clear the string table cache slot if the object is not marked
*/
void
MM_MarkingSchemeRootClearer::doStringCacheTableSlot(omrobjectptr_t *slotPtr)
{
omrobjectptr_t objectPtr = *slotPtr;
if ((NULL != objectPtr) && (!_markingScheme->isMarked(*slotPtr))) {
*slotPtr = NULL;
}
}
void
MM_MarkingSchemeRootClearer::doJVMTIObjectTagSlot(omrobjectptr_t *slotPtr, GC_JVMTIObjectTagTableIterator *objectTagTableIterator)
{
if (!_markingScheme->isMarked(*slotPtr)) {
objectTagTableIterator->removeSlot();
}
}