/
Gen.java
467 lines (407 loc) · 14.1 KB
/
Gen.java
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
/*
* This file is part of the Jikes RVM project (http://jikesrvm.org).
*
* This file is licensed to You under the Common Public License (CPL);
* You may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.php
*
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership.
*/
package org.mmtk.plan.generational;
import org.mmtk.plan.*;
import org.mmtk.policy.CopySpace;
import org.mmtk.policy.Space;
import org.mmtk.utility.deque.*;
import org.mmtk.utility.heap.Map;
import org.mmtk.utility.heap.VMRequest;
import org.mmtk.utility.Log;
import org.mmtk.utility.options.Options;
import org.mmtk.utility.sanitychecker.SanityChecker;
import org.mmtk.utility.statistics.*;
import org.mmtk.vm.Collection;
import org.mmtk.vm.VM;
import org.vmmagic.pragma.*;
import org.vmmagic.unboxed.*;
/**
* This abstract class implements the core functionality of generic
* two-generationa copying collectors. Nursery collections occur when
* either the heap is full or the nursery is full. The nursery size
* is determined by an optional command line argument. If undefined,
* the nursery size is "infinite", so nursery collections only occur
* when the heap is full (this is known as a flexible-sized nursery
* collector). Thus both fixed and flexible nursery sizes are
* supported. Full heap collections occur when the nursery size has
* dropped to a statically defined threshold,
* <code>NURSERY_THRESHOLD</code><p>
*
* See also Plan.java for general comments on local vs global plan
* classes.
*/
@Uninterruptible
public abstract class Gen extends StopTheWorld {
/*****************************************************************************
*
* Constants
*/
protected static final float SURVIVAL_ESTIMATE = 0.8f; // est yield
protected static final float MATURE_FRACTION = 0.5f; // est yield
private static final float WORST_CASE_COPY_EXPANSION = 1.5f; // worst case for addition of one word overhead due to address based hashing
public static final boolean IGNORE_REMSETS = false;
public static final boolean USE_STATIC_WRITE_BARRIER = false;
public static final boolean USE_OBJECT_BARRIER_FOR_AASTORE = false; // choose between slot and object barriers
public static final boolean USE_OBJECT_BARRIER_FOR_PUTFIELD = false; // choose between slot and object barriers
public static final boolean USE_OBJECT_BARRIER = USE_OBJECT_BARRIER_FOR_AASTORE || USE_OBJECT_BARRIER_FOR_PUTFIELD;
private static final boolean USE_DISCONTIGUOUS_NURSERY = false;
// Allocators
public static final int ALLOC_NURSERY = ALLOC_DEFAULT;
public static final int ALLOC_MATURE = StopTheWorld.ALLOCATORS + 1;
public static final int ALLOC_MATURE_MINORGC = StopTheWorld.ALLOCATORS + 2;
public static final int ALLOC_MATURE_MAJORGC = StopTheWorld.ALLOCATORS + 3;
public static final int SCAN_NURSERY = 0;
public static final int SCAN_MATURE = 1;
/*****************************************************************************
*
* Class fields
*/
/* Statistics */
protected static final BooleanCounter fullHeap = new BooleanCounter("majorGC", true, true);
private static final Timer fullHeapTime = new Timer("majorGCTime", false, true);
protected static final EventCounter wbFast;
protected static final EventCounter wbSlow;
public static final SizeCounter nurseryMark;
public static final SizeCounter nurseryCons;
/** The nursery space is where all new objects are allocated by default */
private static final VMRequest vmRequest = USE_DISCONTIGUOUS_NURSERY ? VMRequest.create() : VMRequest.create(0.15f, true);
public static final CopySpace nurserySpace = new CopySpace("nursery", DEFAULT_POLL_FREQUENCY, false, vmRequest);
public static final int NURSERY = nurserySpace.getDescriptor();
private static final Address NURSERY_START = nurserySpace.getStart();
/*****************************************************************************
*
* Instance fields
*/
/* status fields */
public boolean gcFullHeap = false;
public boolean nextGCFullHeap = false;
/* The trace object */
public final Trace nurseryTrace = new Trace(metaDataSpace);
/**
* Remset pools
*/
public final SharedDeque modbufPool = new SharedDeque("modBufs",metaDataSpace, 1);
public final SharedDeque remsetPool = new SharedDeque("remSets",metaDataSpace, 1);
public final SharedDeque arrayRemsetPool = new SharedDeque("arrayRemSets",metaDataSpace, 2);
/*
* Class initializer
*/
static {
if (GATHER_WRITE_BARRIER_STATS) {
wbFast = new EventCounter("wbFast");
wbSlow = new EventCounter("wbSlow");
} else {
wbFast = null;
wbSlow = null;
}
if (Stats.GATHER_MARK_CONS_STATS) {
nurseryMark = new SizeCounter("nurseryMark", true, true);
nurseryCons = new SizeCounter("nurseryCons", true, true);
} else {
nurseryMark = null;
nurseryCons = null;
}
}
/*****************************************************************************
*
* Collection
*/
/**
* Force the next collection to be full heap.
*/
public void forceFullHeapCollection() {
nextGCFullHeap = true;
}
/**
* Perform a (global) collection phase.
*
* @param phaseId Collection phase to execute.
*/
@NoInline
public void collectionPhase(short phaseId) {
if (phaseId == SET_COLLECTION_KIND) {
super.collectionPhase(phaseId);
gcFullHeap = requiresFullHeapCollection();
return;
}
if (phaseId == PREPARE) {
nurserySpace.prepare(true);
if (traceFullHeap()){
if (gcFullHeap) {
if (Stats.gatheringStats()) fullHeap.set();
fullHeapTime.start();
}
super.collectionPhase(phaseId);
// we can throw away the remsets (but not modbuf) for a full heap GC
remsetPool.clearDeque(1);
arrayRemsetPool.clearDeque(2);
}
return;
}
if (phaseId == CLOSURE) {
if (!traceFullHeap()) {
nurseryTrace.prepare();
}
return;
}
if (phaseId == RELEASE) {
nurserySpace.release();
modbufPool.clearDeque(1);
remsetPool.clearDeque(1);
arrayRemsetPool.clearDeque(2);
if (!traceFullHeap()) {
nurseryTrace.release();
} else {
super.collectionPhase(phaseId);
if (gcFullHeap) fullHeapTime.stop();
}
nextGCFullHeap = (getPagesAvail() < Options.nurserySize.getMinNursery());
return;
}
super.collectionPhase(phaseId);
}
/**
* This method controls the triggering of a GC. It is called periodically
* during allocation. Returns true to trigger a collection.
*
* @param spaceFull Space request failed, must recover pages within 'space'.
* @return True if a collection is requested by the plan.
*/
public final boolean collectionRequired(boolean spaceFull) {
int nurseryPages = nurserySpace.reservedPages();
if (nurseryPages > Options.nurserySize.getMaxNursery()) {
return true;
}
if (virtualMemoryExhausted())
return true;
return super.collectionRequired(spaceFull);
}
/**
* Determine if this GC should be a full heap collection.
*
* @return True is this GC should be a full heap collection.
*/
protected boolean requiresFullHeapCollection() {
if (collectionTrigger == Collection.EXTERNAL_GC_TRIGGER && Options.fullHeapSystemGC.getValue()) {
return true;
}
if (nextGCFullHeap || collectionAttempt > 1) {
// Forces full heap collection
return true;
}
if (loSpace.allocationFailed() ||
nonMovingSpace.allocationFailed() ||
(USE_CODE_SPACE && (largeCodeSpace.allocationFailed() || smallCodeSpace.allocationFailed()))) {
// We need space from the nursery
return true;
}
if (virtualMemoryExhausted())
return true;
int smallNurseryPages = nurserySpace.committedPages();
int smallNurseryYield = (int)((smallNurseryPages << 1) * SURVIVAL_ESTIMATE);
if (smallNurseryYield < getPagesRequired()) {
// Our total yield is insufficent.
return true;
}
if (nurserySpace.allocationFailed()) {
if (smallNurseryYield < (nurserySpace.requiredPages() << 1)) {
// We have run out of VM pages in the nursery
return true;
}
}
return false;
}
/**
* Independent of how many pages remain in the page budget (a function of
* heap size), we must ensure we never exhaust virtual memory. Therefore
* we must never let the nursery grow to the extent that it can't be
* copied into the mature space.
*
* @return True if the nursery has grown to the extent that it may not be
* able to be copied into the mature space.
*/
private boolean virtualMemoryExhausted() {
return ((int) (nurserySpace.reservedPages()*WORST_CASE_COPY_EXPANSION) >= getMaturePhysicalPagesAvail());
}
/*****************************************************************************
*
* Correctness
*/
/*****************************************************************************
*
* Accounting
*/
/**
* Return the number of pages in use given the pending
* allocation. Simply add the nursery's contribution to that of
* the superclass.
*
* @return The number of pages reserved given the pending
* allocation, excluding space reserved for copying.
*/
@Override
public int getPagesUsed() {
return (nurserySpace.reservedPages() + super.getPagesUsed());
}
/**
* Return the number of pages available for allocation, <i>assuming
* all future allocation is to the nursery</i>.
*
* @return The number of pages available for allocation, <i>assuming
* all future allocation is to the nursery</i>.
*/
@Override
public int getPagesAvail() {
return super.getPagesAvail() >> 1;
}
/**
* Return the number of pages reserved for copying.
*
* @return The number of pages reserved given the pending
* allocation, including space reserved for copying.
*/
@Override
public int getCollectionReserve() {
return nurserySpace.reservedPages() + super.getCollectionReserve();
}
/**
* Return the number of pages available for allocation into the mature
* space.
*
* @return The number of pages available for allocation into the mature
* space.
*/
public abstract int getMaturePhysicalPagesAvail();
/**
* Calculate the number of pages a collection is required to free to satisfy
* outstanding allocation requests.
*
* @return the number of pages a collection is required to free to satisfy
* outstanding allocation requests.
*/
@Override
public int getPagesRequired() {
/* We don't currently pretenure, so mature space must be zero */
return super.getPagesRequired() + (nurserySpace.requiredPages() << 1);
}
/*****************************************************************************
*
* Miscellaneous
*/
/**
* Return true if the address resides within the nursery
*
* @param addr The object to be tested
* @return true if the address resides within the nursery
*/
@Inline
static boolean inNursery(Address addr) {
if (USE_DISCONTIGUOUS_NURSERY)
return Map.getDescriptorForAddress(addr) == NURSERY;
else
return addr.GE(NURSERY_START);
}
/**
* Return true if the object resides within the nursery
*
* @param obj The object to be tested
* @return true if the object resides within the nursery
*/
@Inline
static boolean inNursery(ObjectReference obj) {
return inNursery(obj.toAddress());
}
/**
* @return Does the mature space do copying ?
*/
protected boolean copyMature() {
return false;
}
/**
* Print pre-collection statistics. In this class we prefix the output
* indicating whether the collection was full heap or not.
*/
public void printPreStats() {
if ((Options.verbose.getValue() >= 1) && (gcFullHeap))
Log.write("[Full heap]");
super.printPreStats();
}
/**
* @return The mature space, set by each subclass of <code>Gen</code>.
*/
protected abstract Space activeMatureSpace();
/**
* @return True if we should trace the whole heap during collection. True if
* we're ignorning remsets or if we're doing a full heap GC.
*/
public final boolean traceFullHeap() {
return IGNORE_REMSETS || gcFullHeap;
}
/**
* @return Is current GC only collecting objects allocated since last GC.
*/
public final boolean isCurrentGCNursery() {
return !gcFullHeap;
}
/**
* @return Is last GC a full collection?
*/
public final boolean lastCollectionFullHeap() {
return gcFullHeap;
}
/**
* @see org.mmtk.plan.Plan#willNeverMove
*
* @param object Object in question
* @return True if the object will never move
*/
@Override
public boolean willNeverMove(ObjectReference object) {
if (Space.isInSpace(NURSERY, object))
return false;
return super.willNeverMove(object);
}
/**
* Return the expected reference count. For non-reference counting
* collectors this becomes a true/false relationship.
*
* @param object The object to check.
* @param sanityRootRC The number of root references to the object.
* @return The expected (root excluded) reference count.
*/
public int sanityExpectedRC(ObjectReference object, int sanityRootRC) {
Space space = Space.getSpaceForObject(object);
// Nursery
if (space == Gen.nurserySpace) {
return SanityChecker.DEAD;
}
// Immortal spaces
if (space == Gen.immortalSpace || space == Gen.vmSpace) {
return space.isReachable(object) ? SanityChecker.ALIVE : SanityChecker.DEAD;
}
// Mature space (nursery collection)
if (VM.activePlan.global().isCurrentGCNursery()) {
return SanityChecker.UNSURE;
}
// Mature space (full heap collection)
return space.isReachable(object) ? SanityChecker.ALIVE : SanityChecker.DEAD;
}
/**
* Register specialized methods.
*/
@Interruptible
protected void registerSpecializedMethods() {
TransitiveClosure.registerSpecializedScan(SCAN_NURSERY, GenNurseryTraceLocal.class);
super.registerSpecializedMethods();
}
}