-
Notifications
You must be signed in to change notification settings - Fork 392
/
MarkingScheme.cpp
437 lines (387 loc) · 13 KB
/
MarkingScheme.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
/*******************************************************************************
* Copyright (c) 1991, 2020 IBM Corp. and others
*
* 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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "omrcfg.h"
#include "omr.h"
#include "GlobalCollector.hpp"
#include <string.h>
#if defined(OMR_GC_MODRON_CONCURRENT_MARK)
#include "ConcurrentGC.hpp"
#include "ConcurrentGCStats.hpp"
#endif /* defined(OMR_GC_MODRON_CONCURRENT_MARK) */
#include "Configuration.hpp"
#include "EnvironmentBase.hpp"
#include "GCExtensionsBase.hpp"
#include "Heap.hpp"
#include "MarkMap.hpp"
#include "MarkingScheme.hpp"
#include "Task.hpp"
#if defined(OMR_GC_REALTIME)
#include "WorkPacketsSATB.hpp"
#endif /* defined(OMR_GC_REALTIME) */
#include "RememberedSetSATB.hpp"
#if defined(OMR_GC_MODRON_CONCURRENT_MARK)
#include "WorkPacketsConcurrent.hpp"
#else
#include "WorkPacketsStandard.hpp"
#endif /* defined(OMR_GC_MODRON_CONCURRENT_MARK) */
/**
* Allocate and initialize a new instance of the receiver.
* @return a new instance of the receiver, or NULL on failure.
*/
MM_MarkingScheme *
MM_MarkingScheme::newInstance(MM_EnvironmentBase *env)
{
MM_MarkingScheme *markingScheme;
markingScheme = (MM_MarkingScheme *)env->getForge()->allocate(sizeof(MM_MarkingScheme), OMR::GC::AllocationCategory::FIXED, OMR_GET_CALLSITE());
if (markingScheme) {
new(markingScheme) MM_MarkingScheme(env);
if (!markingScheme->initialize(env)) {
markingScheme->kill(env);
markingScheme = NULL;
}
}
return markingScheme;
}
/**
* Free the receiver and all associated resources.
*/
void
MM_MarkingScheme::kill(MM_EnvironmentBase *env)
{
tearDown(env);
env->getForge()->free(this);
}
/**
* Initialize the receivers internal structures and resources.
* @return true if initialization is successful, false otherwise.
*/
bool
MM_MarkingScheme::initialize(MM_EnvironmentBase *env)
{
_markMap = MM_MarkMap::newInstance(env, _extensions->heap->getMaximumPhysicalRange());
if (!_markMap) {
goto error_no_memory;
}
_workPackets = createWorkPackets(env);
if (NULL == _workPackets) {
goto error_no_memory;
}
return _delegate.initialize(env, this);
error_no_memory:
return false;
}
/**
* Free the receivers internal structures and resources.
*/
void
MM_MarkingScheme::tearDown(MM_EnvironmentBase *env)
{
if (_markMap) {
_markMap->kill(env);
_markMap = NULL;
}
if(_workPackets) {
_workPackets->kill(env);
_workPackets = NULL;
}
}
/**
* Adjust internal structures to reflect the change in heap size.
*
* @param size The amount of memory added to the heap
* @param lowAddress The base address of the memory added to the heap
* @param highAddress The top address (non-inclusive) of the memory added to the heap
* @return true if operation completes with success
*/
bool
MM_MarkingScheme::heapAddRange(MM_EnvironmentBase *env, MM_MemorySubSpace *subspace, uintptr_t size, void *lowAddress, void *highAddress)
{
bool result = true;
/* Record the range in which valid objects appear */
_heapBase = _extensions->heap->getHeapBase();
_heapTop = _extensions->heap->getHeapTop();
if (result) {
result = _markMap->heapAddRange(env, size, lowAddress, highAddress);
}
return result;
}
/**
* Adjust internal structures to reflect the change in heap size.
*
* @param size The amount of memory added to the heap
* @param lowAddress The base address of the memory added to the heap
* @param highAddress The top address (non-inclusive) of the memory added to the heap
* @param lowValidAddress The first valid address previous to the lowest in the heap range being removed
* @param highValidAddress The first valid address following the highest in the heap range being removed
* @return true if operation completes with success
*/
bool
MM_MarkingScheme::heapRemoveRange(MM_EnvironmentBase *env, MM_MemorySubSpace *subspace, uintptr_t size, void *lowAddress, void *highAddress, void *lowValidAddress, void *highValidAddress)
{
bool result = true;
/* Record the range in which valid objects appear */
_heapBase = _extensions->heap->getHeapBase();
_heapTop = _extensions->heap->getHeapTop();
if (result) {
_markMap->heapRemoveRange(env, size, lowAddress, highAddress, lowValidAddress, highValidAddress);
}
return result;
}
/****************************************
* Runtime initialization
****************************************
*/
void
MM_MarkingScheme::mainSetupForGC(MM_EnvironmentBase *env)
{
/* Initialize the marking stack */
_workPackets->reset(env);
_delegate.mainSetupForGC(env);
}
/**
* Set up the marking scheme for marking the heap in preparation for walking the heap.
* This should be called in place of mainSetupForGC if no actual garbage collection
* is to take place.
*
* @parm[in] env the current environment
*/
void
MM_MarkingScheme::mainSetupForWalk(MM_EnvironmentBase *env)
{
/* Initialize the marking stack */
_workPackets->reset(env);
_delegate.mainSetupForWalk(env);
}
void
MM_MarkingScheme::mainCleanupAfterGC(MM_EnvironmentBase *env)
{
_delegate.mainCleanupAfterGC(env);
}
void
MM_MarkingScheme::workerSetupForGC(MM_EnvironmentBase *env)
{
env->_markStats.clear();
env->_workPacketStats.clear();
env->_workStack.reset(env, _workPackets);
_delegate.workerSetupForGC(env);
}
void
MM_MarkingScheme::workerCleanupAfterGC(MM_EnvironmentBase *env)
{
_delegate.workerCleanupAfterGC(env);
#if defined(OMR_GC_MODRON_STANDARD) || defined(OMR_GC_REALTIME)
_extensions->globalGCStats.markStats.merge(&env->_markStats);
_extensions->globalGCStats.workPacketStats.merge(&env->_workPacketStats);
#endif /* defined(OMR_GC_MODRON_STANDARD) || defined(OMR_GC_REALTIME) */
}
/****************************************
* Marking Helpers
****************************************
*/
/**
* External access to inlined method isMarked
*/
bool
MM_MarkingScheme::isMarkedOutline(omrobjectptr_t objectPtr)
{
return isMarked(objectPtr);
}
bool
MM_MarkingScheme::markObjectNoCheck(MM_EnvironmentBase *env, omrobjectptr_t objectPtr, bool leafType)
{
return inlineMarkObjectNoCheck(env, objectPtr, leafType);
}
bool
MM_MarkingScheme::markObject(MM_EnvironmentBase *env, omrobjectptr_t objectPtr, bool leafType)
{
return inlineMarkObject(env, objectPtr, leafType);
}
/**************************************************************************
* name - numMarkBitsInRange
*
* description - Determine number of mark bits for specified heap range
*
*
* parameters - heapBase - base of region of heap whoose mark bits are to be
* counted
* heapTop - top of region of heap whoose mark bits to be counted
*
* return - the number of bytes worth of mark bits for specified heap
* range
* *************************************************************************/
uintptr_t
MM_MarkingScheme::numMarkBitsInRange(MM_EnvironmentBase *env, void *heapBase, void *heapTop)
{
return _markMap->numberBitsInRange(env, heapBase, heapTop);
}
/**************************************************************************
* name - setMarkBitsInRange
*
* description - Set all mark bits for specified heap range either ON or OFF
*
*
* parameters - heapBase - base of region of heap whoose mark bits are to be set
* heapTop - top of region of heap whoose mark bits to be set
* clear - if TRUE set BITS OFF; otherwise set bits ON
*
* return - the amount of memory actually cleard
* *************************************************************************/
uintptr_t
MM_MarkingScheme::setMarkBitsInRange(MM_EnvironmentBase *env, void *heapBase, void *heapTop, bool clear)
{
return _markMap->setBitsInRange(env, heapBase, heapTop, clear);
}
/****************************************
* Scanning
****************************************
*/
/**
* Private internal. Called exclusively from completeScan();
*/
uintptr_t
MM_MarkingScheme::scanObject(MM_EnvironmentBase *env, omrobjectptr_t objectPtr)
{
uintptr_t sizeToDo = UDATA_MAX;
GC_ObjectScannerState objectScannerState;
GC_ObjectScanner *objectScanner = _delegate.getObjectScanner(env, objectPtr, &objectScannerState, SCAN_REASON_PACKET, &sizeToDo);
if (NULL != objectScanner) {
bool isLeafSlot = false;
GC_SlotObject *slotObject;
#if defined(OMR_GC_LEAF_BITS)
while (NULL != (slotObject = objectScanner->getNextSlot(&isLeafSlot))) {
#else /* OMR_GC_LEAF_BITS */
while (NULL != (slotObject = objectScanner->getNextSlot())) {
#endif /* OMR_GC_LEAF_BITS */
fixupForwardedSlot(slotObject);
inlineMarkObjectNoCheck(env, slotObject->readReferenceFromSlot(), isLeafSlot);
}
}
return sizeToDo;
}
/**
* Scan until there are no more work packets to be processed.
* @note This is a joining scan: a thread will not exit this method until
* all threads have entered and all work packets are empty.
*/
void
MM_MarkingScheme::completeScan(MM_EnvironmentBase *env)
{
do {
omrobjectptr_t objectPtr = NULL;
while (NULL != (objectPtr = (omrobjectptr_t )env->_workStack.pop(env))) {
env->_markStats._bytesScanned += scanObject(env, objectPtr);
env->_markStats._objectsScanned += 1;
}
} while (_workPackets->handleWorkPacketOverflow(env));
}
/****************************************
* Marking Core Functionality
****************************************/
/**
* Initialization for Mark
* Actual startup for Mark procedure
* @param[in] env - passed Environment
* @param[in] initMarkMap instruct should mark map be initialized too
*/
void
MM_MarkingScheme::markLiveObjectsInit(MM_EnvironmentBase *env, bool initMarkMap)
{
workerSetupForGC(env);
if(initMarkMap) {
_markMap->initializeMarkMap(env);
env->_currentTask->synchronizeGCThreads(env, UNIQUE_ID);
}
}
/**
* Mark Roots
* Create Root Scanner and Mark all roots including classes and classloaders if dynamic class unloading is enabled
* @param[in] env - passed Environment
* @param[in] shouldScan instruct should scanning also be active while roots are marked
*/
void
MM_MarkingScheme::markLiveObjectsRoots(MM_EnvironmentBase *env, bool processLists)
{
_delegate.scanRoots(env, processLists);
}
void
MM_MarkingScheme::markLiveObjectsScan(MM_EnvironmentBase *env)
{
completeMarking(env);
}
void
MM_MarkingScheme::completeMarking(MM_EnvironmentBase *env)
{
completeScan(env);
_delegate.completeMarking(env);
}
/**
* Final Mark services including scanning of Clearables
* @param[in] env - passed Environment
*/
void
MM_MarkingScheme::markLiveObjectsComplete(MM_EnvironmentBase *env)
{
_delegate.workerCompleteGC(env);
}
MM_WorkPackets *
MM_MarkingScheme::createWorkPackets(MM_EnvironmentBase *env)
{
MM_WorkPackets *workPackets = NULL;
if (_extensions->isConcurrentMarkEnabled()) {
if (_extensions->configuration->isSnapshotAtTheBeginningBarrierEnabled()) {
#if defined(OMR_GC_REALTIME)
MM_WorkPacketsSATB *workPacketsSATB = MM_WorkPacketsSATB::newInstance(env);
_extensions->sATBBarrierRememberedSet = MM_RememberedSetSATB::newInstance(env, workPacketsSATB);
workPackets = workPacketsSATB;
#endif /* defined(OMR_GC_REALTIME) */
} else {
#if defined OMR_GC_MODRON_CONCURRENT_MARK
workPackets = MM_WorkPacketsConcurrent::newInstance(env);
#endif /* defined OMR_GC_MODRON_CONCURRENT_MARK */
}
} else {
workPackets = MM_WorkPacketsStandard::newInstance(env);
}
return workPackets;
}
void
MM_MarkingScheme::fixupForwardedSlotOutline(GC_SlotObject *slotObject) {
#if defined(OMR_GC_CONCURRENT_SCAVENGER)
bool const compressed = _extensions->compressObjectReferences();
if (_extensions->getGlobalCollector()->isStwCollectionInProgress()) {
MM_ForwardedHeader forwardHeader(slotObject->readReferenceFromSlot(), compressed);
omrobjectptr_t forwardPtr = forwardHeader.getNonStrictForwardedObject();
if (NULL != forwardPtr) {
if (forwardHeader.isSelfForwardedPointer()) {
forwardHeader.restoreSelfForwardedPointer();
} else {
slotObject->writeReferenceToSlot(forwardPtr);
}
}
}
#endif /* OMR_GC_CONCURRENT_SCAVENGER */
}
uintptr_t
MM_MarkingScheme::setupIndexableScanner(MM_EnvironmentBase *env, omrobjectptr_t objectPtr, MM_MarkingSchemeScanReason reason, uintptr_t *sizeToDo, uintptr_t *sizeInElementsToDo, fomrobject_t **basePtr, uintptr_t *flags)
{
return _delegate.setupIndexableScanner(env, objectPtr, reason, sizeToDo, sizeInElementsToDo, basePtr, flags);
}