/
Phase.java
650 lines (572 loc) · 19.8 KB
/
Phase.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
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
/*
* 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.utility.Constants;
import org.mmtk.utility.Log;
import org.mmtk.utility.options.Options;
import org.mmtk.utility.statistics.Timer;
import org.mmtk.vm.Collection;
import org.mmtk.vm.VM;
import org.vmmagic.pragma.*;
/**
* A garbage collection proceeds as a sequence of phases. Each
* phase is either simple (singular) or complex (an array).
*
* The context an individual phase executes in may be global, mutator,
* or collector.
*
* Phases are executed within a stack and all synchronization between
* parallel GC threads is managed from within this class.
*
* @see CollectorContext#collectionPhase
* @see MutatorContext#collectionPhase
* @see Plan#collectionPhase
*/
@Uninterruptible
public abstract class Phase implements Constants {
/***********************************************************************
*
* Phase allocation and storage.
*/
/** The maximum number of phases */
private static final int MAX_PHASES = 64;
/** The array of phase instances. Zero is unused. */
private static final Phase[] phases = new Phase[MAX_PHASES];
/** The id to be allocated for the next phase */
private static short nextPhaseId = 1;
/** Run the phase globally. */
protected static final short SCHEDULE_GLOBAL = 1;
/** Run the phase on collectors. */
protected static final short SCHEDULE_COLLECTOR = 2;
/** Run the phase on mutators. */
protected static final short SCHEDULE_MUTATOR = 3;
/** Don't run this phase. */
protected static final short SCHEDULE_PLACEHOLDER = 100;
/** This is a complex phase. */
protected static final short SCHEDULE_COMPLEX = 101;
/**
* Retrieve a phase by the unique phase identifier.
*
* @param id The phase identifier.
* @return The Phase instance.
*/
public static Phase getPhase(short id) {
if (VM.VERIFY_ASSERTIONS) {
VM.assertions._assert(id < nextPhaseId, "Phase ID unknown");
VM.assertions._assert(phases[id] != null, "Uninitialised phase");
}
return phases[id];
}
/** Get the phase id component of an encoded phase */
protected static short getPhaseId(int scheduledPhase) {
short phaseId = (short)(scheduledPhase & 0x0000FFFF);
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(phaseId > 0);
return phaseId;
}
/**
* @param phaseId The unique phase identifier.
* @return The name of the phase.
*/
public static String getName(short phaseId) {
return phases[phaseId].name;
}
/** Get the ordering component of an encoded phase */
protected static short getSchedule(int scheduledPhase) {
short ordering = (short)((scheduledPhase >> 16) & 0x0000FFFF);
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(ordering > 0);
return ordering;
}
/** Get the ordering component of an encoded phase */
protected static String getScheduleName(short ordering) {
switch (ordering) {
case SCHEDULE_GLOBAL: return "Global";
case SCHEDULE_COLLECTOR: return "Collector";
case SCHEDULE_MUTATOR: return "Mutator";
case SCHEDULE_PLACEHOLDER: return "Placeholder";
case SCHEDULE_COMPLEX: return "Complex";
default: return "UNKNOWN!";
}
}
/**
* Construct a phase.
*
* @param name Display name of the phase
*/
@Interruptible
public static short createSimple(String name) {
return new SimplePhase(name).getId();
}
/**
* Construct a phase, re-using a specified timer.
*
* @param name Display name of the phase
*/
@Interruptible
public static short createSimple(String name, Timer timer) {
return new SimplePhase(name, timer).getId();
}
/**
* Construct a complex phase.
*
* @param name Display name of the phase
* @param scheduledPhases The phases in this complex phase.
*/
@Interruptible
public static short createComplex(String name,int... scheduledPhases) {
return new ComplexPhase(name, scheduledPhases).getId();
}
/**
* Construct a complex phase, re-using a specified timer.
*
* @param name Display name of the phase
* @param timer Timer for this phase to contribute to
* @param scheduledPhases The phases in this complex phase.
*/
@Interruptible
public static short createComplex(String name, Timer timer, int... scheduledPhases) {
return new ComplexPhase(name, timer, scheduledPhases).getId();
}
/**
* Take the passed phase and return an encoded phase to
* run that phase as a complex phase.
*
* @param phaseId The phase to run as complex
* @return The encoded phase value.
*/
public static int scheduleComplex(short phaseId) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Phase.getPhase(phaseId) instanceof ComplexPhase);
return (SCHEDULE_COMPLEX << 16) + phaseId;
}
/**
* Take the passed phase and return an encoded phase to
* run that phase in a global context;
*
* @param phaseId The phase to run globally
* @return The encoded phase value.
*/
public static int scheduleGlobal(short phaseId) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Phase.getPhase(phaseId) instanceof SimplePhase);
return (SCHEDULE_GLOBAL << 16) + phaseId;
}
/**
* Take the passed phase and return an encoded phase to
* run that phase in a collector context;
*
* @param phaseId The phase to run on collectors
* @return The encoded phase value.
*/
public static int scheduleCollector(short phaseId) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Phase.getPhase(phaseId) instanceof SimplePhase);
return (SCHEDULE_COLLECTOR << 16) + phaseId;
}
/**
* Take the passed phase and return an encoded phase to
* run that phase in a mutator context;
*
* @param phaseId The phase to run on mutators
* @return The encoded phase value.
*/
public static int scheduleMutator(short phaseId) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Phase.getPhase(phaseId) instanceof SimplePhase);
return (SCHEDULE_MUTATOR << 16) + phaseId;
}
/**
* Take the passed phase and return an encoded phase to
* run that phase in a mutator context;
*
* @param phaseId The phase to run on mutators
* @return The encoded phase value.
*/
public static int schedulePlaceholder(short phaseId) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Phase.getPhase(phaseId) instanceof SimplePhase);
return (SCHEDULE_PLACEHOLDER << 16) + phaseId;
}
/***********************************************************************
*
* Phase instance fields/methods.
*/
/**
* The unique phase identifier.
*/
protected final short id;
/**
* The name of the phase.
*/
protected final String name;
/**
* The Timer that is started and stopped around the execution of this
* phase.
*/
protected final Timer timer;
/**
* Create a new Phase. This involves creating a corresponding Timer
* instance, allocating a unique identifier, and registering the
* Phase.
*
* @param name The name for the phase.
*/
protected Phase(String name) {
this(name, new Timer(name, false, true));
}
/**
* Create a new phase. This involves setting the corresponding Timer
* instance, allocating a unique identifier, and registering the Phase.
*
* @param name The name of the phase.
* @param timer The timer, or null if this is an untimed phase.
*/
protected Phase(String name, Timer timer) {
this.name = name;
this.timer = timer;
this.id = nextPhaseId++;
phases[this.id] = this;
}
/**
* @return The unique identifier for this phase.
*/
public final short getId() {
return this.id;
}
/**
* Display a phase for debugging purposes.
*/
protected abstract void logPhase();
/***********************************************************************
*
* Phase stack
*/
/** The maximum stack depth for the phase stack. */
private static final int MAX_PHASE_STACK_DEPTH = MAX_PHASES;
/** Stores the current sub phase for a complex phase. Each entry corresponds to a phase stack entry */
private static int[] complexPhaseCursor = new int[MAX_PHASE_STACK_DEPTH];
/** The phase stack. Stores the current nesting of phases */
private static int[] phaseStack = new int[MAX_PHASE_STACK_DEPTH];
/** The current stack pointer */
private static int phaseStackPointer = -1;
/**
* The current even (0 mod 2) scheduled phase.
* As we only sync at the end of a phase we need this to ensure that
* the primary thread setting the phase does not race with the other
* threads reading it.
*/
private static int evenScheduledPhase;
/**
* The current odd (1 mod 2) scheduled phase.
* As we only sync at the end of a phase we need this to ensure that
* the primary thread setting the phase does not race with the other
* threads reading it.
*/
private static int oddScheduledPhase;
/**
* Do we need to add a sync point to reset the mutator count. This
* is necessary for consecutive mutator phases and unneccessary
* otherwise. Again we separate in even and odd to ensure that there
* is no race between the primary thread setting and the helper
* threads reading.
*/
private static boolean evenMutatorResetRendezvous;
/**
* Do we need to add a sync point to reset the mutator count. This
* is necessary for consecutive mutator phases and unneccessary
* otherwise. Again we separate in even and odd to ensure that there
* is no race between the primary thread setting and the helper
* threads reading.
*/
private static boolean oddMutatorResetRendezvous;
/**
* The complex phase whose timer should be started after the next
* rendezvous. We can not start the timer at the point we determine
* the next complex phase as we determine the next phase at the
* end of the previous phase before the sync point.
*/
private static short startComplexTimer;
/**
* The complex phase whose timer should be stopped after the next
* rendezvous. We can not start the timer at the point we determine
* the next complex phase as we determine the next phase at the
* end of the previous phase before the sync point.
*/
private static short stopComplexTimer;
/**
* Place a phase on the phase stack and begin processing.
*
* @param scheduledPhase The phase to execute
* @return True if the phase stack is exhausted.
*/
public static boolean beginNewPhaseStack(int scheduledPhase) {
int order = VM.collection.rendezvous(1001);
if (order == 1) {
pushScheduledPhase(scheduledPhase);
}
return processPhaseStack(false);
}
/**
* Process the phase stack. This method is called by multiple threads.
*/
private static boolean processPhaseStack(boolean resume) {
int order = VM.collection.rendezvous(1001);
final boolean primary = order == 1;
boolean log = Options.verbose.getValue() >= 6;
boolean logDetails = Options.verbose.getValue() >= 7;
if (primary && resume) {
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(!Phase.isPhaseStackEmpty());
if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(!Plan.gcInProgress());
Plan.setGCStatus(Plan.GC_PROPER);
}
/* In order to reduce the need for synchronization, we keep an odd or even
* counter for the number of phases processed. As each phase has a single
* rendezvous it is only possible to be out by one so the odd or even counter
* protects us. */
boolean isEvenPhase = true;
if (primary) {
/* First phase will be even, so we say we are odd here so that the next phase set is even*/
setNextPhase(false, getNextPhase(), false);
}
/* Make sure everyone sees the first phase */
VM.collection.rendezvous(1002);
/* Global and Collector instances used in phases */
Plan plan = VM.activePlan.global();
CollectorContext collector = VM.activePlan.collector();
/* The main phase execution loop */
int scheduledPhase;
while((scheduledPhase = getCurrentPhase(isEvenPhase)) > 0) {
short schedule = getSchedule(scheduledPhase);
short phaseId = getPhaseId(scheduledPhase);
Phase p = getPhase(phaseId);
/* Start the timer(s) */
if (primary) {
if (resume) {
resumeComplexTimers();
}
if (p.timer != null) p.timer.start();
if (startComplexTimer > 0) {
Phase.getPhase(startComplexTimer).timer.start();
startComplexTimer = 0;
}
}
if (log) {
Log.write("Execute ");
p.logPhase();
}
/* Execute a single simple scheduled phase */
switch (schedule) {
/* Global phase */
case SCHEDULE_GLOBAL: {
if (logDetails) Log.writeln(" as Global...");
if (primary) plan.collectionPhase(phaseId);
break;
}
/* Collector phase */
case SCHEDULE_COLLECTOR: {
if (logDetails) Log.writeln(" as Collector...");
collector.collectionPhase(phaseId, primary);
break;
}
/* Mutator phase */
case SCHEDULE_MUTATOR: {
if (logDetails) Log.writeln(" as Mutator...");
/* Iterate through all mutator contexts */
MutatorContext mutator;
while ((mutator = VM.activePlan.getNextMutator()) != null) {
mutator.collectionPhase(phaseId, primary);
}
break;
}
default: {
/* getNextPhase has done the wrong thing */
VM.assertions.fail("Invalid schedule in Phase.processPhaseStack");
break;
}
}
if (primary) {
/* Set the next phase by processing the stack */
int next = getNextPhase();
boolean needsResetRendezvous = (next > 0) && (schedule == SCHEDULE_MUTATOR && getSchedule(next) == SCHEDULE_MUTATOR);
setNextPhase(isEvenPhase, next, needsResetRendezvous);
}
/* Sync point after execution of a phase */
VM.collection.rendezvous(1004);
/* Mutator phase reset */
if (primary && schedule == SCHEDULE_MUTATOR) {
VM.activePlan.resetMutatorIterator();
}
/* At this point, in the case of consecutive phases with mutator
* scheduling, we have to double-synchronize to ensure all
* collector threads see the reset mutator counter. */
if (needsMutatorResetRendezvous(isEvenPhase)) {
VM.collection.rendezvous(1005);
}
/* Stop the timer(s) */
if (primary) {
if (p.timer != null) p.timer.stop();
if (stopComplexTimer > 0) {
Phase.getPhase(stopComplexTimer).timer.stop();
stopComplexTimer = 0;
}
}
/* Flip the even / odd phase sense */
isEvenPhase = !isEvenPhase;
resume = false;
}
/* Phase stack exhausted so we return true */
return true;
}
/**
* Get the next phase.
*/
private static int getCurrentPhase(boolean isEvenPhase) {
return isEvenPhase ? evenScheduledPhase : oddScheduledPhase;
}
/**
* Do we need a mutator reset rendezvous in this phase?
*/
private static boolean needsMutatorResetRendezvous(boolean isEvenPhase) {
return isEvenPhase ? evenMutatorResetRendezvous : oddMutatorResetRendezvous;
}
/**
* Set the next phase. If we are in an even phase the next phase is odd.
*/
private static void setNextPhase(boolean isEvenPhase, int scheduledPhase, boolean needsResetRendezvous) {
if (isEvenPhase) {
oddScheduledPhase = scheduledPhase;
evenMutatorResetRendezvous = needsResetRendezvous;
} else {
evenScheduledPhase = scheduledPhase;
oddMutatorResetRendezvous = needsResetRendezvous;
}
}
/**
* Pull the next scheduled phase off the stack. This may involve
* processing several complex phases and skipping placeholders, etc.
*
* @return The next phase to run, or -1 if no phases are left.
*/
private static int getNextPhase() {
boolean allowConcurrentPhase = Plan.collectionTrigger == Collection.INTERNAL_PHASE_GC_TRIGGER;
while (phaseStackPointer >= 0) {
int scheduledPhase = peekScheduledPhase();
short schedule = getSchedule(scheduledPhase);
short phaseId = getPhaseId(scheduledPhase);
switch(schedule) {
case SCHEDULE_PLACEHOLDER: {
/* Placeholders are ignored and we continue looking */
popScheduledPhase();
continue;
}
case SCHEDULE_GLOBAL:
case SCHEDULE_COLLECTOR:
case SCHEDULE_MUTATOR: {
/* Simple phases are just popped off the stack and executed */
popScheduledPhase();
return scheduledPhase;
}
case SCHEDULE_COMPLEX: {
/* A complex phase may either be a newly pushed complex phase,
* or a complex phase we are in the process of executing in
* which case we move to the next subphase. */
ComplexPhase p = (ComplexPhase)getPhase(phaseId);
int cursor = incrementComplexPhaseCursor();
if (cursor == 0 && p.timer != null) {
/* Tell the primary thread to start the timer after the next sync. */
startComplexTimer = phaseId;
}
if (cursor < p.count()) {
/* There are more entries, we push the next one and continue */
pushScheduledPhase(p.get(cursor));
continue;
}
/* We have finished this complex phase */
popScheduledPhase();
if (p.timer != null) {
/* Tell the primary thread to stop the timer after the next sync. */
stopComplexTimer = phaseId;
}
continue;
}
default: {
VM.assertions.fail("Invalid phase type encountered");
}
}
}
return -1;
}
/**
* Pause all of the timers for the complex phases sitting in the stack.
*/
private static void pauseComplexTimers() {
for(int i=phaseStackPointer; i >=0; i--) {
Phase p = getPhase(getPhaseId(phaseStack[i]));
if (p.timer != null) p.timer.stop();
}
}
/**
* Resume all of the timers for the complex phases sitting in the stack.
*/
private static void resumeComplexTimers() {
for(int i=phaseStackPointer; i >=0; i--) {
Phase p = getPhase(getPhaseId(phaseStack[i]));
if (p.timer != null) p.timer.start();
}
}
/**
* Return true if phase stack is empty, false otherwise.
*
* @return true if phase stack is empty, false otherwise.
*/
@Inline
public static boolean isPhaseStackEmpty() {
return phaseStackPointer < 0;
}
/**
* Clears the scheduled phase stack.
*/
@Inline
public static void resetPhaseStack() {
phaseStackPointer = -1;
}
/**
* Push a scheduled phase onto the top of the work stack.
*
* @param scheduledPhase The scheduled phase.
*/
@Inline
public static void pushScheduledPhase(int scheduledPhase) {
phaseStack[++phaseStackPointer] = scheduledPhase;
complexPhaseCursor[phaseStackPointer] = 0;
}
/**
* Increment the cursor associated with the current phase
* stack entry. This is used to remember the current sub phase
* when executing a complex phase.
*
* @return The old value of the cursor.
*/
@Inline
private static int incrementComplexPhaseCursor() {
return complexPhaseCursor[phaseStackPointer]++;
}
/**
* Pop off the scheduled phase at the top of the work stack.
*/
@Inline
private static int popScheduledPhase() {
return phaseStack[phaseStackPointer--];
}
/**
* Peek the scheduled phase at the top of the work stack.
*/
@Inline
private static int peekScheduledPhase() {
return phaseStack[phaseStackPointer];
}
}