/
Collector.java
250 lines (215 loc) · 6.8 KB
/
Collector.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
/*
* 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.harness;
import java.util.ArrayList;
import org.mmtk.harness.scheduler.Scheduler;
import org.mmtk.harness.vm.ActivePlan;
import org.mmtk.plan.CollectorContext;
import org.mmtk.plan.Plan;
import org.mmtk.utility.heap.HeapGrowthManager;
import org.mmtk.utility.options.Options;
import org.mmtk.vm.Collection;
/**
* This class represents a collector thread.
*/
public final class Collector implements Runnable {
/** Registered collectors */
private static ArrayList<Collector> collectors = new ArrayList<Collector>();
/**
* Get a collector by id.
*/
public static Collector get(int id) {
return collectors.get(id);
}
/**
* Get the currently executing collector.
*/
public static Collector current() {
Collector c = Scheduler.currentCollector();
assert c != null: "Collector.current() called from a thread without a collector context";
return c;
}
/**
* The number of collector threads that have been created.
*/
public static int count() {
return collectors.size();
}
/**
* Register a collector thread, returning the allocated id.
*/
public static synchronized int allocateCollectorId() {
int id = collectors.size();
collectors.add(null);
return id;
}
/**
* Initialise numCollector collector threads.
*/
public static void init(int numCollectors) {
for(int i = 0; i < numCollectors; i++) {
Scheduler.scheduleCollector();
}
}
/**
* The MMTk CollectorContext for this collector thread.
*/
private final CollectorContext context;
/**
* Create a new Collector
*/
public Collector() {
try {
Class<?> collectorClass = Class.forName(Harness.plan.getValue() + "Collector");
this.context = (CollectorContext)collectorClass.newInstance();
this.context.initCollector(allocateCollectorId());
} catch (Exception ex) {
throw new RuntimeException("Could not create Collector", ex);
}
collectors.set(context.getId(), this);
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.err.print("Collector " + context.getId() + " caused unexpected exception: ");
e.printStackTrace();
System.exit(1);
}
});
}
/** The number of collections that have occurred */
private static int collectionCount;
public static int getCollectionCount() {
return collectionCount;
}
/** The current base count of collection attempts */
private static int collectionAttemptBase;
public static int getCollectionAttemptBase() {
return collectionAttemptBase;
}
/** Has a heap dump been requested? */
private static boolean heapDumpRequested;
/**
* Request a heap dump at the next GC.
*/
public static void requestHeapDump() {
heapDumpRequested = true;
}
/**
* Trigger a collection for the given reason
*/
public static void triggerGC(int why) {
Scheduler.triggerGC(why);
}
/**
* Return the MMTk CollectorContext for this collector.
*/
public CollectorContext getContext() {
return context;
}
/**
* Rendezvous with all other processors, returning the rank
* (that is, the order this processor arrived at the barrier).
*/
public static int rendezvous(int where) {
return Scheduler.rendezvous(where);
}
/**
* The main collector execution loop. Wait for a GC to be triggered,
* do the GC work and then wait again.
*/
public void run() {
while(true) {
Scheduler.waitForGCStart();
/*
* Make all GC errors fatal
*/
try {
collect();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
Scheduler.exitGC();
}
}
/**
* Perform a GC
*/
private void collect() {
boolean primary = context.getId() == 0;
if (primary) {
Plan.setCollectionTrigger(Scheduler.getTriggerReason());
}
long startTime = System.nanoTime();
boolean internalPhaseTriggered = (Scheduler.getTriggerReason() == Collection.INTERNAL_PHASE_GC_TRIGGER);
boolean userTriggered = (Scheduler.getTriggerReason() == Collection.EXTERNAL_GC_TRIGGER);
rendezvous(5000);
do {
context.collect();
rendezvous(5200);
if (primary) {
long elapsedTime = System.nanoTime() - startTime;
HeapGrowthManager.recordGCTime(elapsedTime / 1e6);
if (ActivePlan.plan.lastCollectionFullHeap() && !internalPhaseTriggered) {
if (Options.variableSizeHeap.getValue() && !userTriggered) {
// Don't consider changing the heap size if gc was forced by System.gc()
HeapGrowthManager.considerHeapSize();
}
HeapGrowthManager.reset();
}
if (internalPhaseTriggered) {
if (ActivePlan.plan.lastCollectionFailed()) {
internalPhaseTriggered = false;
Plan.setCollectionTrigger(Collection.INTERNAL_GC_TRIGGER);
}
}
collectionAttemptBase++;
collectionCount += 1;
}
startTime = System.nanoTime();
rendezvous(5201);
} while (ActivePlan.plan.lastCollectionFailed() && !Plan.isEmergencyCollection());
if (primary && !internalPhaseTriggered) {
/* If the collection failed, we may need to throw OutOfMemory errors.
* As we have not cleared the GC flag, allocation is not budgeted.
*
* This is not flawless in the case we physically can not allocate
* anything right after a GC, but that case is unlikely (we can
* not make it happen) and is a lot of work to get around. */
if (Plan.isEmergencyCollection()) {
boolean gcFailed = ActivePlan.plan.lastCollectionFailed();
// Allocate OOMEs (some of which *may* not get used)
for(int m=0; m < Mutator.count(); m++) {
Mutator mutator = Mutator.get(m);
if (mutator.getCollectionAttempts() > 0) {
/* this thread was allocating */
if (gcFailed || mutator.isPhysicalAllocationFailure()) {
mutator.setOutOfMemory(true);
}
}
}
}
}
if (primary) {
collectionAttemptBase = 0;
/* This is where we would schedule Finalization, if we supported it. */
if (heapDumpRequested) {
Mutator.dumpHeap();
heapDumpRequested = false;
}
}
rendezvous(5202);
if (primary) {
Plan.collectionComplete();
}
}
}