/
Scheduler.java
258 lines (228 loc) · 6.1 KB
/
Scheduler.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
/*
* 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.scheduler;
import org.mmtk.harness.Collector;
import org.mmtk.harness.Harness;
import org.mmtk.harness.MMTkThread;
import org.mmtk.harness.Mutator;
import org.mmtk.harness.scheduler.javathreads.JavaThreadModel;
import org.mmtk.harness.scheduler.rawthreads.RawThreadModel;
import org.mmtk.utility.Log;
/**
* Facade class for the command-line selectable threading models available
* in the MMTk harness.
*/
public class Scheduler {
/**
* Possible threading models
*/
public enum Model {
JAVA,
DETERMINISTIC;
/** The values of this enum, converted to strings */
public static String[] valueNames() {
String[] result = new String[Scheduler.Model.values().length];
for (int i=0; i < Scheduler.Model.values().length; i++) {
result[i] = Scheduler.Model.values()[i].toString();
}
return result;
}
}
/**
* Possible thread-scheduling policies
*/
public enum SchedPolicy {
FIXED,
RANDOM,
NEVER;
/** The values of this enum, converted to strings */
public static String[] valueNames() {
String[] result = new String[Scheduler.SchedPolicy.values().length];
for (int i=0; i < Scheduler.SchedPolicy.values().length; i++) {
result[i] = Scheduler.SchedPolicy.values()[i].toString();
}
return result;
}
}
/**
* Thread Model factory - returns the thread model selected by user options.
*/
private static ThreadModel selectedThreadModel() {
switch (Harness.scheduler.model()) {
case JAVA:
return new JavaThreadModel();
case DETERMINISTIC:
return new RawThreadModel();
default:
throw new RuntimeException("Unknown thread model");
}
}
private static ThreadModel model;
/**
* Yield policy factory - return an instance of the the command-line
* selected yield policy
*/
public static Policy yieldPolicy(Thread thread) {
switch (Harness.policy.policy()) {
case FIXED:
int yieldInterval = Harness.yieldInterval.getValue();
if (yieldInterval == 1) {
return new YieldAlways(thread);
} else {
return new YieldEvery(thread,yieldInterval);
}
case RANDOM:
return new YieldRandomly(thread,
Harness.randomPolicySeed.getValue(),
Harness.randomPolicyLength.getValue(),
Harness.randomPolicyMin.getValue(),
Harness.randomPolicyMax.getValue());
case NEVER:
return new YieldNever(thread);
default:
throw new RuntimeException("Unknown scheduler policy");
}
}
/**
* Initialization
*/
public static void init() {
model = selectedThreadModel();
}
/**
* Advance collector threads to their initial 'wait for collection' barrier
*/
public static void initCollectors() {
model.initCollectors();
}
/**
* A yield-point.
*/
public static void yield() {
model.yield();
}
/**
* Create and start a new Mutator thread
* @param item The executable code to run in this thread
*/
public static void scheduleMutator(Schedulable item) {
model.scheduleMutator(item);
}
/**
* Create and start a new collector thread
*/
public static void scheduleCollector() {
model.scheduleCollector();
}
/**
* Create and start a new collector thread running a particular code
* sequence. Used to schedule unit tests in collector context.
*/
public static Thread scheduleCollector(Schedulable item) {
return model.scheduleCollector(item);
}
/**
* The current Log object.
* @return
*/
public static Log currentLog() {
return ((MMTkThread)Thread.currentThread()).getLog();
}
/**
* The current mutator object (if the current thread is a Mutator)
* @return
*/
public static Mutator currentMutator() {
return model.currentMutator();
}
/**
* The current collector object (if the current thread is a Collector)
* @return
*/
public static Collector currentCollector() {
return model.currentCollector();
}
/* schedule GC */
/**
* Request a GC. Once requested, mutator threads block at
* 'waitForGC' until a collection is performed.
*/
public static void triggerGC(int why) {
model.triggerGC(why);
}
/**
* A collector thread informs the scheduler that it has completed
* its GC work by calling this.
*/
public static void exitGC() {
model.exitGC();
}
/**
* Collector threads call this method to wait for a GC to be triggered.
*/
public static void waitForGCStart() {
model.waitForGCStart();
}
/**
* Why was the current GC triggered ?
* @return
*/
public static int getTriggerReason() {
return model.getTriggerReason();
}
/**
* Are there no threads currently in GC?
*/
public static boolean noThreadsInGC() {
return model.noThreadsInGC();
}
/**
* Has a GC been triggered?
*/
public static boolean gcTriggered() {
return model.gcTriggered();
}
/**
* Collector thread synchronization barrier
* @param where
* @return
*/
public static int rendezvous(int where) {
return model.rendezvous(where);
}
/**
* Cause the current thread to wait for a triggered GC to proceed.
*/
public static void waitForGC() {
model.waitForGC();
}
/**
* Schedule the threads
*/
public static void schedule() {
model.schedule();
}
/**
* Schedule the GC threads as though a GC had been triggered
* Used to run unit tests that must run in collector context.
*/
public static void scheduleGcThreads() {
model.scheduleGcThreads();
}
/**
* An MMTk lock - a factory method.
*/
public static Lock newLock(String name) {
return model.newLock(name);
}
}