/
TraceLocal.java
571 lines (529 loc) · 18.4 KB
/
TraceLocal.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
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
/*
* 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;
import org.mmtk.policy.Space;
import org.mmtk.utility.Constants;
import org.mmtk.utility.Log;
import org.mmtk.utility.deque.*;
import org.mmtk.utility.options.Options;
import org.mmtk.vm.VM;
import org.vmmagic.pragma.*;
import org.vmmagic.unboxed.*;
/**
* This abstract class and its global counterpart implement the core
* functionality for a transitive closure over the heap graph. This class
* specifically implements the unsynchronized thread-local component
* (ie the 'fast path') of the trace mechanism.<p>
*
* @see org.mmtk.plan.Plan
* @see org.mmtk.plan.Trace
*/
@Uninterruptible
public abstract class TraceLocal extends TransitiveClosure implements Constants {
/****************************************************************************
*
* Instance variables
*/
/* gray object */
protected final ObjectReferenceDeque values;
/* delayed root slots */
protected final AddressDeque rootLocations;
/****************************************************************************
*
* Initialization
*/
/**
* Constructor
*
* @param trace The global trace class to use.
*/
public TraceLocal(Trace trace) {
this(-1, trace);
}
/**
* Constructor
*
* @param specializedScan The specialized scan id.
* @param trace The global trace class to use.
*/
public TraceLocal(int specializedScan, Trace trace) {
super(specializedScan);
values = new ObjectReferenceDeque("value", trace.valuePool);
rootLocations = new AddressDeque("roots", trace.rootLocationPool);
}
/****************************************************************************
*
* Internally visible Object processing and tracing
*/
/**
* Trace a reference during GC. This involves determining which
* collection policy applies and calling the appropriate
* <code>trace</code> method.
*
* @param source The source of the reference.
* @param slot The location containing the object reference to be
* traced. The object reference is <i>NOT</i> an interior pointer.
*/
@Inline
public final void processEdge(ObjectReference source, Address slot) {
ObjectReference object = VM.activePlan.global().loadObjectReference(slot);
ObjectReference newObject = traceObject(object, false);
VM.activePlan.global().storeObjectReference(slot, newObject);
}
/**
* Report a root edge to be processed during GC. As the given reference
* may theoretically point to an object required during root scanning,
* the caller has requested processing be delayed.
*
* NOTE: delayed roots are assumed to be raw.
*
* @param slot The location containing the object reference to be
* traced. The object reference is <i>NOT</i> an interior pointer.
*/
@Inline
public final void reportDelayedRootEdge(Address slot) {
rootLocations.push(slot);
}
/**
* Trace a reference during GC. This involves determining which
* collection policy applies and calling the appropriate
* <code>trace</code> method.
*
* @param slot The location containing the object reference to be
* traced. The object reference is <i>NOT</i> an interior pointer.
* @param untraced True if <code>objLoc</code> is an untraced root.
*/
@Inline
public final void processRootEdge(Address slot, boolean untraced) {
ObjectReference object;
if (untraced) object = slot.loadObjectReference();
else object = VM.activePlan.global().loadObjectReference(slot);
ObjectReference newObject = traceObject(object, true);
if (untraced) slot.store(newObject);
else VM.activePlan.global().storeObjectReference(slot, newObject);
}
/**
* Trace a reference during GC. This involves determining which
* collection policy applies and calling the appropriate
* <code>trace</code> method.
*
* @param target The object the interior edge points within.
* @param slot The location of the interior edge.
* @param root True if this is a root edge.
*/
public final void processInteriorEdge(ObjectReference target, Address slot, boolean root) {
Address interiorRef = slot.loadAddress();
Offset offset = interiorRef.diff(target.toAddress());
ObjectReference newTarget = traceObject(target, root);
if (VM.VERIFY_ASSERTIONS) {
if (offset.sLT(Offset.zero()) || offset.sGT(Offset.fromIntSignExtend(1<<24))) {
// There is probably no object this large
Log.writeln("ERROR: Suspiciously large delta to interior pointer");
Log.write(" object base = "); Log.writeln(target);
Log.write(" interior reference = "); Log.writeln(interiorRef);
Log.write(" delta = "); Log.writeln(offset);
VM.assertions._assert(false);
}
}
slot.store(newTarget.toAddress().plus(offset));
}
/**
* Collectors that move objects <b>must</b> override this method.
* It performs the deferred scanning of objects which are forwarded
* during bootstrap of each copying collection. Because of the
* complexities of the collection bootstrap (such objects are
* generally themselves gc-critical), the forwarding and scanning of
* the objects must be dislocated. It is an error for a non-moving
* collector to call this method.
*
* @param object The forwarded object to be scanned
*/
@Inline
protected void scanObject(ObjectReference object) {
if (specializedScan >= 0) {
VM.scanning.specializedScanObject(specializedScan, this, object);
} else {
VM.scanning.scanObject(this, object);
}
}
/****************************************************************************
*
* Externally visible Object processing and tracing
*/
/**
* Add a gray object
*
* @param object The object to be enqueued
*/
@Inline
public final void processNode(ObjectReference object) {
values.push(object);
}
/**
* Flush the local buffers of all deques.
*/
public final void flush() {
values.flushLocal();
rootLocations.flushLocal();
}
/**
* Is the specified object live?
*
* @param object The object.
* @return True if the object is live.
*/
@Inline
public boolean isLive(ObjectReference object) {
Space space = Space.getSpaceForObject(object);
if (space == Plan.loSpace)
return Plan.loSpace.isLive(object);
else if (space == Plan.nonMovingSpace)
return Plan.nonMovingSpace.isLive(object);
else if (Plan.USE_CODE_SPACE && space == Plan.smallCodeSpace)
return Plan.smallCodeSpace.isLive(object);
else if (Plan.USE_CODE_SPACE && space == Plan.largeCodeSpace)
return Plan.largeCodeSpace.isLive(object);
else if (space == null) {
if (VM.VERIFY_ASSERTIONS) {
Log.write("space failure: "); Log.writeln(object);
}
}
return true;
}
/**
* Is the specified object reachable? Used for GC Trace
*
* @param object The object.
* @return True if the object is live.
*/
@Inline
public boolean isReachable(ObjectReference object) {
return Space.getSpaceForObject(object).isReachable(object);
}
/**
* Is the specified referent of a reference type object live?
*
* @param object The object.
* @return True if the reference object is live.
*/
@Inline
public boolean isReferentLive(ObjectReference object) {
return isLive(object);
}
/**
* This method is the core method during the trace of the object graph.
* The role of this method is to:
*
* 1. Ensure the traced object is not collected.
* 2. If this is the first visit to the object enqueue it to be scanned.
* 3. Return the forwarded reference to the object.
*
* @param object The object to be traced.
* @return The new reference to the same object instance.
*/
@Inline
public ObjectReference traceObject(ObjectReference object) {
if (Space.isInSpace(Plan.VM_SPACE, object))
return (Plan.SCAN_BOOT_IMAGE) ? object : Plan.vmSpace.traceObject(this, object);
if (Space.isInSpace(Plan.IMMORTAL, object))
return Plan.immortalSpace.traceObject(this, object);
if (Space.isInSpace(Plan.LOS, object))
return Plan.loSpace.traceObject(this, object);
if (Space.isInSpace(Plan.NON_MOVING, object))
return Plan.nonMovingSpace.traceObject(this, object);
if (Plan.USE_CODE_SPACE && Space.isInSpace(Plan.SMALL_CODE, object))
return Plan.smallCodeSpace.traceObject(this, object);
if (Plan.USE_CODE_SPACE && Space.isInSpace(Plan.LARGE_CODE, object))
return Plan.largeCodeSpace.traceObject(this, object);
if (VM.VERIFY_ASSERTIONS) {
Log.write("Failing object => "); Log.writeln(object);
Space.printVMMap();
VM.assertions._assert(false, "No special case for space in traceObject");
}
return ObjectReference.nullReference();
}
/**
* Ensure that this object will not move for the rest of the GC.
*
* @param object The object that must not move
* @return The new object, guaranteed stable for the rest of the GC.
*/
@Inline
public ObjectReference precopyObject(ObjectReference object) {
return traceObject(object);
}
/**
* This method traces an object with knowledge of the fact that object
* is a root or not. In simple collectors the fact it is a root is not
* important so this is the default implementation given here.
*
* @param object The object to be traced.
* @return The new reference to the same object instance.
*/
@Inline
public ObjectReference traceObject(ObjectReference object, boolean root) {
return traceObject(object);
}
/**
* Ensure that the referenced object will not move from this point through
* to the end of the collection. This can involve forwarding the object
* if necessary.
*
* <i>Non-copying collectors do nothing, copying collectors must
* override this method in each of their trace classes.</i>
*
* @param object The object that must not move during the collection.
* @return True If the object will not move during collection
*/
public boolean willNotMoveInCurrentCollection(ObjectReference object) {
if (!VM.activePlan.constraints().movesObjects())
return true;
if (Space.isInSpace(Plan.LOS, object))
return true;
if (Space.isInSpace(Plan.IMMORTAL, object))
return true;
if (Space.isInSpace(Plan.VM_SPACE, object))
return true;
if (Space.isInSpace(Plan.NON_MOVING, object))
return true;
if (Plan.USE_CODE_SPACE && Space.isInSpace(Plan.SMALL_CODE, object))
return true;
if (Plan.USE_CODE_SPACE && Space.isInSpace(Plan.LARGE_CODE, object))
return true;
if (VM.VERIFY_ASSERTIONS)
VM.assertions._assert(false, "willNotMove not defined properly in subclass");
return false;
}
/**
* If a Finalizable object has moved, return the new location.
*
* @param object The object which may have been forwarded.
* @return The new location of <code>object</code>.
*/
public ObjectReference getForwardedFinalizable(ObjectReference object) {
return getForwardedReference(object);
}
/**
* If the reference object (from a Reference Type) has object has moved,
* return the new location.
*
* @param object The object which may have been forwarded.
* @return The new location of <code>object</code>.
*/
@Inline
public ObjectReference getForwardedReferent(ObjectReference object) {
return getForwardedReference(object);
}
/**
* If the Reference Type object has moved, return the new location.
*
* @param object The object which may have been forwarded.
* @return The new location of <code>object</code>.
*/
@Inline
public ObjectReference getForwardedReferenceType(ObjectReference object) {
return getForwardedReference(object);
}
/**
* If the referenced object has moved, return the new location.
*
* Some copying collectors will need to override this method.
*
* @param object The object which may have been forwarded.
* @return The new location of <code>object</code>.
*/
@Inline
public ObjectReference getForwardedReference(ObjectReference object) {
return traceObject(object);
}
/**
* Make alive a referent object that is known not to be live
* (isLive is false). This is used by the ReferenceProcessor.
*
* <i>For many collectors these semantics relfect those of
* <code>traceObject</code>, which is implemented here. Other
* collectors must override this method.</i>
*
* @param object The object which is to be made alive.
* @return The possibly forwarded address of the object.
*/
@Inline
public ObjectReference retainReferent(ObjectReference object) {
return traceObject(object);
}
/**
* An object is unreachable and is about to be added to the
* finalizable queue. The collector must ensure the object is not
* collected (despite being otherwise unreachable), and should
* return its forwarded address if keeping the object alive involves
* forwarding. This is only ever called once for an object.<p>
*
* <i>For many collectors these semantics relfect those of
* <code>traceObject</code>, which is implemented here. Other
* collectors must override this method.</i>
*
* @param object The object which may have been forwarded.
* @return The forwarded value for <code>object</code>. <i>In this
* case return <code>object</code>, copying collectors must override
* this method.
*/
public ObjectReference retainForFinalize(ObjectReference object) {
return traceObject(object);
}
/**
* Return true if an object is ready to move to the finalizable
* queue, i.e. it has no regular references to it. This method may
* (and in some cases is) be overridden by subclasses. If this method
* returns true then it can be assumed that retainForFinalize will be
* called during the current collection.
*
* <i>For many collectors these semantics relfect those of
* <code>isLive</code>, which is implemented here. Other
* collectors must override this method.</i>
*
* @param object The object being queried.
* @return <code>true</code> if the object has no regular references
* to it.
*/
public boolean readyToFinalize(ObjectReference object) {
return !isLive(object);
}
/****************************************************************************
*
* Collection
*
* Important notes:
* . Global actions are executed by only one thread
* . Thread-local actions are executed by all threads
* . The following order is guaranteed by BasePlan, with each
* separated by a synchronization barrier.
* 1. globalPrepare()
* 2. threadLocalPrepare()
* 3. threadLocalRelease()
* 4. globalRelease()
*/
public void prepare() {
// Nothing to do
}
public void release() {
values.reset();
rootLocations.reset();
}
/**
* Process any roots for which processing was delayed.
*/
@Inline
public void processRoots() {
logMessage(5, "processing delayed root objects");
while (!rootLocations.isEmpty()) {
processRootEdge(rootLocations.pop(), true);
}
}
/**
* Finishing processing all GC work. This method iterates until all work queues
* are empty.
*/
@Inline
public void completeTrace() {
logMessage(4, "Processing GC in parallel");
if (!rootLocations.isEmpty()) {
processRoots();
}
logMessage(5, "processing gray objects");
assertMutatorRemsetsFlushed();
do {
while (!values.isEmpty()) {
ObjectReference v = values.pop();
scanObject(v);
}
processRememberedSets();
} while (!values.isEmpty());
assertMutatorRemsetsFlushed();
}
/**
* Process GC work until either complete or workLimit
* units of work are completed.
*
* @param workLimit The maximum units of work to perform.
* @return True if all work was completed within workLimit.
*/
@Inline
public boolean incrementalTrace(int workLimit) {
logMessage(4, "Continuing GC in parallel (incremental)");
logMessage(5, "processing gray objects");
int units = 0;
do {
while (!values.isEmpty() && units < workLimit) {
ObjectReference v = values.pop();
scanObject(v);
units++;
}
} while (!values.isEmpty() && units < workLimit);
return values.isEmpty();
}
/**
* Flush any remembered sets pertaining to the current collection.
* Non-generational collectors do nothing.
*/
protected void processRememberedSets() {}
/**
* Assert that the remsets have been flushed. This is critical to
* correctness. We need to maintain the invariant that remset entries
* do not accrue during GC. If the host JVM generates barrier entires
* it is its own responsibility to ensure that they are flushed before
* returning to MMTk.
*/
private void assertMutatorRemsetsFlushed() {
/* FIXME: PNT
if (VM.VERIFY_ASSERTIONS) {
for (int m = 0; m < VM.activePlan.mutatorCount(); m++) {
VM.activePlan.mutator(m).assertRemsetsFlushed();
}
}
*/
}
/**
* This method logs a message with preprended thread id, if the
* verbosity level is greater or equal to the passed level.
*
* @param minVerbose The required verbosity level
* @param message The message to display
*/
@Inline
protected final void logMessage(int minVerbose, String message) {
if (Options.verbose.getValue() >= minVerbose) {
Log.prependThreadId();
Log.write(" ");
Log.writeln(message);
}
}
/**
* Given a slot (ie the address of an ObjectReference), ensure that the
* referent will not move for the rest of the GC. This is achieved by
* calling the precopyObject method.
*
* @param slot The slot to check
* @param untraced Is this is an untraced reference?
*/
@Inline
public final void processPrecopyEdge(Address slot, boolean untraced) {
ObjectReference child;
if (untraced) child = slot.loadObjectReference();
else child = VM.activePlan.global().loadObjectReference(slot);
if (!child.isNull()) {
child = precopyObject(child);
if (untraced) slot.store(child);
else VM.activePlan.global().storeObjectReference(slot, child);
}
}
}