author | dlong |
Tue, 24 Apr 2018 09:04:57 -0700 | |
changeset 49873 | 26ebfe8ce852 |
parent 48861 | 47f19ff9903c |
child 50330 | 2cbc42a5764b |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
2 |
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
package org.graalvm.compiler.hotspot; |
|
24 |
||
46344 | 25 |
import static jdk.vm.ci.common.InitTimer.timer; |
26 |
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; |
|
27 |
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale; |
|
43972 | 28 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; |
46344 | 29 |
import static org.graalvm.compiler.core.common.GraalOptions.HotSpotPrintInlining; |
46640 | 30 |
import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM; |
43972 | 31 |
|
46344 | 32 |
import java.util.ArrayList; |
46807 | 33 |
import java.util.EnumMap; |
46344 | 34 |
import java.util.List; |
46807 | 35 |
import java.util.Map; |
49873 | 36 |
import java.util.concurrent.atomic.AtomicReference; |
43972 | 37 |
|
49873 | 38 |
import jdk.internal.vm.compiler.collections.EconomicMap; |
39 |
import jdk.internal.vm.compiler.collections.EconomicSet; |
|
40 |
import jdk.internal.vm.compiler.collections.Equivalence; |
|
41 |
import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor; |
|
43972 | 42 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; |
43 |
import org.graalvm.compiler.api.runtime.GraalRuntime; |
|
46807 | 44 |
import org.graalvm.compiler.core.CompilationWrapper.ExceptionAction; |
46640 | 45 |
import org.graalvm.compiler.core.common.CompilationIdentifier; |
43972 | 46 |
import org.graalvm.compiler.core.common.GraalOptions; |
47 |
import org.graalvm.compiler.core.target.Backend; |
|
46640 | 48 |
import org.graalvm.compiler.debug.DebugContext; |
49 |
import org.graalvm.compiler.debug.DebugContext.Description; |
|
46680 | 50 |
import org.graalvm.compiler.debug.DebugHandlersFactory; |
49873 | 51 |
import org.graalvm.compiler.debug.DebugOptions; |
46680 | 52 |
import org.graalvm.compiler.debug.DiagnosticsOutputDirectory; |
46640 | 53 |
import org.graalvm.compiler.debug.GlobalMetrics; |
43972 | 54 |
import org.graalvm.compiler.debug.GraalError; |
55 |
import org.graalvm.compiler.debug.TTY; |
|
46344 | 56 |
import org.graalvm.compiler.hotspot.CompilationStatistics.Options; |
43972 | 57 |
import org.graalvm.compiler.hotspot.CompilerConfigurationFactory.BackendMap; |
58 |
import org.graalvm.compiler.hotspot.debug.BenchmarkCounters; |
|
59 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
|
60 |
import org.graalvm.compiler.nodes.spi.StampProvider; |
|
49873 | 61 |
import org.graalvm.compiler.options.EnumOptionKey; |
62 |
import org.graalvm.compiler.options.OptionDescriptor; |
|
63 |
import org.graalvm.compiler.options.OptionDescriptors; |
|
64 |
import org.graalvm.compiler.options.OptionKey; |
|
46344 | 65 |
import org.graalvm.compiler.options.OptionValues; |
49873 | 66 |
import org.graalvm.compiler.options.OptionsParser; |
43972 | 67 |
import org.graalvm.compiler.phases.tiers.CompilerConfiguration; |
68 |
import org.graalvm.compiler.replacements.SnippetCounter; |
|
46344 | 69 |
import org.graalvm.compiler.replacements.SnippetCounter.Group; |
43972 | 70 |
import org.graalvm.compiler.runtime.RuntimeProvider; |
49873 | 71 |
import org.graalvm.compiler.serviceprovider.GraalServices; |
43972 | 72 |
|
73 |
import jdk.vm.ci.code.Architecture; |
|
74 |
import jdk.vm.ci.code.stack.StackIntrospection; |
|
75 |
import jdk.vm.ci.common.InitTimer; |
|
49873 | 76 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest; |
43972 | 77 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
49873 | 78 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
79 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaType; |
|
80 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; |
|
43972 | 81 |
import jdk.vm.ci.hotspot.HotSpotVMConfigStore; |
82 |
import jdk.vm.ci.meta.JavaKind; |
|
46459 | 83 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
49873 | 84 |
import jdk.vm.ci.meta.ResolvedJavaType; |
85 |
import jdk.vm.ci.runtime.JVMCI; |
|
43972 | 86 |
import jdk.vm.ci.runtime.JVMCIBackend; |
87 |
||
88 |
//JaCoCo Exclude |
|
89 |
||
90 |
/** |
|
91 |
* Singleton class holding the instance of the {@link GraalRuntime}. |
|
92 |
*/ |
|
93 |
public final class HotSpotGraalRuntime implements HotSpotGraalRuntimeProvider { |
|
94 |
||
95 |
private static boolean checkArrayIndexScaleInvariants() { |
|
96 |
assert getArrayIndexScale(JavaKind.Byte) == 1; |
|
97 |
assert getArrayIndexScale(JavaKind.Boolean) == 1; |
|
98 |
assert getArrayIndexScale(JavaKind.Char) == 2; |
|
99 |
assert getArrayIndexScale(JavaKind.Short) == 2; |
|
100 |
assert getArrayIndexScale(JavaKind.Int) == 4; |
|
101 |
assert getArrayIndexScale(JavaKind.Long) == 8; |
|
102 |
assert getArrayIndexScale(JavaKind.Float) == 4; |
|
103 |
assert getArrayIndexScale(JavaKind.Double) == 8; |
|
104 |
return true; |
|
105 |
} |
|
106 |
||
49873 | 107 |
private final String runtimeName; |
43972 | 108 |
private final HotSpotBackend hostBackend; |
46640 | 109 |
private final GlobalMetrics metricValues = new GlobalMetrics(); |
46344 | 110 |
private final List<SnippetCounter.Group> snippetCounterGroups; |
43972 | 111 |
|
46344 | 112 |
private final EconomicMap<Class<? extends Architecture>, HotSpotBackend> backends = EconomicMap.create(Equivalence.IDENTITY); |
43972 | 113 |
|
114 |
private final GraalHotSpotVMConfig config; |
|
115 |
||
49873 | 116 |
/** |
117 |
* The options can be {@linkplain #setOptionValues(String[], String[]) updated} by external |
|
118 |
* interfaces such as JMX. This comes with the risk that inconsistencies can arise as an |
|
119 |
* {@link OptionValues} object can be cached by various parts of Graal instead of always |
|
120 |
* obtaining them from this object. However, concurrent updates are never lost. |
|
121 |
*/ |
|
122 |
private AtomicReference<OptionValues> optionsRef = new AtomicReference<>(); |
|
123 |
||
124 |
private final HotSpotGraalCompiler compiler; |
|
125 |
||
46680 | 126 |
private final DiagnosticsOutputDirectory outputDirectory; |
46807 | 127 |
private final Map<ExceptionAction, Integer> compilationProblemsPerAction; |
46344 | 128 |
|
43972 | 129 |
/** |
49873 | 130 |
* @param nameQualifier a qualifier to be added to this runtime's {@linkplain #getName() name} |
43972 | 131 |
* @param compilerConfigurationFactory factory for the compiler configuration |
46344 | 132 |
* {@link CompilerConfigurationFactory#selectFactory(String, OptionValues)} |
43972 | 133 |
*/ |
134 |
@SuppressWarnings("try") |
|
49873 | 135 |
HotSpotGraalRuntime(String nameQualifier, HotSpotJVMCIRuntime jvmciRuntime, CompilerConfigurationFactory compilerConfigurationFactory, OptionValues initialOptions) { |
136 |
this.runtimeName = getClass().getSimpleName() + ":" + nameQualifier; |
|
43972 | 137 |
HotSpotVMConfigStore store = jvmciRuntime.getConfigStore(); |
46344 | 138 |
config = GeneratePIC.getValue(initialOptions) ? new AOTGraalHotSpotVMConfig(store) : new GraalHotSpotVMConfig(store); |
43972 | 139 |
|
140 |
// Only set HotSpotPrintInlining if it still has its default value (false). |
|
46344 | 141 |
if (GraalOptions.HotSpotPrintInlining.getValue(initialOptions) == false && config.printInlining) { |
49873 | 142 |
optionsRef.set(new OptionValues(initialOptions, HotSpotPrintInlining, true)); |
46344 | 143 |
} else { |
49873 | 144 |
optionsRef.set(initialOptions); |
145 |
} |
|
146 |
OptionValues options = optionsRef.get(); |
|
147 |
||
148 |
if (config.useCMSGC) { |
|
149 |
// Graal doesn't work with the CMS collector (e.g. GR-6777) |
|
150 |
// and is deprecated (http://openjdk.java.net/jeps/291). |
|
151 |
throw new GraalError("Graal does not support the CMS collector"); |
|
43972 | 152 |
} |
153 |
||
46680 | 154 |
outputDirectory = new DiagnosticsOutputDirectory(options); |
46807 | 155 |
compilationProblemsPerAction = new EnumMap<>(ExceptionAction.class); |
46344 | 156 |
snippetCounterGroups = GraalOptions.SnippetCounters.getValue(options) ? new ArrayList<>() : null; |
43972 | 157 |
CompilerConfiguration compilerConfiguration = compilerConfigurationFactory.createCompilerConfiguration(); |
46536 | 158 |
|
49873 | 159 |
compiler = new HotSpotGraalCompiler(jvmciRuntime, this, options); |
160 |
management = GraalServices.loadSingle(HotSpotGraalManagementRegistration.class, false); |
|
161 |
if (management != null) { |
|
162 |
management.initialize(this); |
|
163 |
} |
|
46536 | 164 |
|
43972 | 165 |
BackendMap backendMap = compilerConfigurationFactory.createBackendMap(); |
166 |
||
167 |
JVMCIBackend hostJvmciBackend = jvmciRuntime.getHostJVMCIBackend(); |
|
168 |
Architecture hostArchitecture = hostJvmciBackend.getTarget().arch; |
|
169 |
try (InitTimer t = timer("create backend:", hostArchitecture)) { |
|
170 |
HotSpotBackendFactory factory = backendMap.getBackendFactory(hostArchitecture); |
|
171 |
if (factory == null) { |
|
172 |
throw new GraalError("No backend available for host architecture \"%s\"", hostArchitecture); |
|
173 |
} |
|
174 |
hostBackend = registerBackend(factory.createBackend(this, compilerConfiguration, jvmciRuntime, null)); |
|
175 |
} |
|
176 |
||
177 |
for (JVMCIBackend jvmciBackend : jvmciRuntime.getJVMCIBackends().values()) { |
|
178 |
if (jvmciBackend == hostJvmciBackend) { |
|
179 |
continue; |
|
180 |
} |
|
181 |
||
182 |
Architecture gpuArchitecture = jvmciBackend.getTarget().arch; |
|
183 |
HotSpotBackendFactory factory = backendMap.getBackendFactory(gpuArchitecture); |
|
184 |
if (factory == null) { |
|
185 |
throw new GraalError("No backend available for specified GPU architecture \"%s\"", gpuArchitecture); |
|
186 |
} |
|
187 |
try (InitTimer t = timer("create backend:", gpuArchitecture)) { |
|
188 |
registerBackend(factory.createBackend(this, compilerConfiguration, null, hostBackend)); |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
// Complete initialization of backends |
|
193 |
try (InitTimer st = timer(hostBackend.getTarget().arch.getName(), ".completeInitialization")) { |
|
46344 | 194 |
hostBackend.completeInitialization(jvmciRuntime, options); |
43972 | 195 |
} |
46344 | 196 |
for (HotSpotBackend backend : backends.getValues()) { |
43972 | 197 |
if (backend != hostBackend) { |
198 |
try (InitTimer st = timer(backend.getTarget().arch.getName(), ".completeInitialization")) { |
|
46344 | 199 |
backend.completeInitialization(jvmciRuntime, options); |
43972 | 200 |
} |
201 |
} |
|
202 |
} |
|
203 |
||
46344 | 204 |
BenchmarkCounters.initialize(jvmciRuntime, options); |
43972 | 205 |
|
206 |
assert checkArrayIndexScaleInvariants(); |
|
207 |
||
208 |
runtimeStartTime = System.nanoTime(); |
|
209 |
bootstrapJVMCI = config.getFlag("BootstrapJVMCI", Boolean.class); |
|
210 |
} |
|
211 |
||
212 |
private HotSpotBackend registerBackend(HotSpotBackend backend) { |
|
213 |
Class<? extends Architecture> arch = backend.getTarget().arch.getClass(); |
|
214 |
HotSpotBackend oldValue = backends.put(arch, backend); |
|
215 |
assert oldValue == null : "cannot overwrite existing backend for architecture " + arch.getSimpleName(); |
|
216 |
return backend; |
|
217 |
} |
|
218 |
||
219 |
@Override |
|
220 |
public HotSpotProviders getHostProviders() { |
|
221 |
return getHostBackend().getProviders(); |
|
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public GraalHotSpotVMConfig getVMConfig() { |
|
226 |
return config; |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
46640 | 230 |
public DebugContext openDebugContext(OptionValues compilationOptions, CompilationIdentifier compilationId, Object compilable, Iterable<DebugHandlersFactory> factories) { |
49873 | 231 |
if (management != null && management.poll(false) != null) { |
232 |
if (compilable instanceof HotSpotResolvedJavaMethod) { |
|
233 |
HotSpotResolvedObjectType type = ((HotSpotResolvedJavaMethod) compilable).getDeclaringClass(); |
|
234 |
if (type instanceof HotSpotResolvedJavaType) { |
|
235 |
Class<?> clazz = ((HotSpotResolvedJavaType) type).mirror(); |
|
236 |
try { |
|
237 |
ClassLoader cl = clazz.getClassLoader(); |
|
238 |
if (cl != null) { |
|
239 |
loaders.add(cl); |
|
240 |
} |
|
241 |
} catch (SecurityException e) { |
|
242 |
// This loader can obviously not be used for resolving class names |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
46640 | 247 |
Description description = new Description(compilable, compilationId.toString(CompilationIdentifier.Verbosity.ID)); |
248 |
return DebugContext.create(compilationOptions, description, metricValues, DEFAULT_LOG_STREAM, factories); |
|
249 |
} |
|
250 |
||
251 |
@Override |
|
46344 | 252 |
public OptionValues getOptions() { |
49873 | 253 |
return optionsRef.get(); |
46459 | 254 |
} |
255 |
||
256 |
@Override |
|
49873 | 257 |
public Group createSnippetCounterGroup(String groupName) { |
46344 | 258 |
if (snippetCounterGroups != null) { |
49873 | 259 |
Group group = new Group(groupName); |
46344 | 260 |
snippetCounterGroups.add(group); |
261 |
return group; |
|
262 |
} |
|
263 |
return null; |
|
264 |
} |
|
265 |
||
266 |
@Override |
|
43972 | 267 |
public String getName() { |
49873 | 268 |
return runtimeName; |
43972 | 269 |
} |
270 |
||
271 |
@SuppressWarnings("unchecked") |
|
272 |
@Override |
|
273 |
public <T> T getCapability(Class<T> clazz) { |
|
274 |
if (clazz == RuntimeProvider.class) { |
|
275 |
return (T) this; |
|
46344 | 276 |
} else if (clazz == OptionValues.class) { |
49873 | 277 |
return (T) optionsRef.get(); |
43972 | 278 |
} else if (clazz == StackIntrospection.class) { |
279 |
return (T) this; |
|
280 |
} else if (clazz == SnippetReflectionProvider.class) { |
|
281 |
return (T) getHostProviders().getSnippetReflection(); |
|
282 |
} else if (clazz == StampProvider.class) { |
|
283 |
return (T) getHostProviders().getStampProvider(); |
|
284 |
} |
|
285 |
return null; |
|
286 |
} |
|
287 |
||
288 |
@Override |
|
289 |
public HotSpotBackend getHostBackend() { |
|
290 |
return hostBackend; |
|
291 |
} |
|
292 |
||
293 |
@Override |
|
294 |
public <T extends Architecture> Backend getBackend(Class<T> arch) { |
|
295 |
assert arch != Architecture.class; |
|
296 |
return backends.get(arch); |
|
297 |
} |
|
298 |
||
299 |
private long runtimeStartTime; |
|
46371 | 300 |
private boolean shutdown; |
43972 | 301 |
|
302 |
/** |
|
303 |
* Take action related to entering a new execution phase. |
|
304 |
* |
|
305 |
* @param phase the execution phase being entered |
|
306 |
*/ |
|
46344 | 307 |
void phaseTransition(String phase) { |
49873 | 308 |
if (Options.UseCompilationStatistics.getValue(optionsRef.get())) { |
46344 | 309 |
CompilationStatistics.clear(phase); |
310 |
} |
|
43972 | 311 |
} |
312 |
||
313 |
void shutdown() { |
|
46371 | 314 |
shutdown = true; |
49873 | 315 |
metricValues.print(optionsRef.get()); |
46640 | 316 |
|
43972 | 317 |
phaseTransition("final"); |
318 |
||
46344 | 319 |
if (snippetCounterGroups != null) { |
320 |
for (Group group : snippetCounterGroups) { |
|
321 |
TTY.out().out().println(group); |
|
322 |
} |
|
323 |
} |
|
49873 | 324 |
BenchmarkCounters.shutdown(runtime(), optionsRef.get(), runtimeStartTime); |
46371 | 325 |
|
46680 | 326 |
outputDirectory.close(); |
43972 | 327 |
} |
328 |
||
46640 | 329 |
void clearMetrics() { |
330 |
metricValues.clear(); |
|
43972 | 331 |
} |
332 |
||
333 |
private final boolean bootstrapJVMCI; |
|
334 |
private boolean bootstrapFinished; |
|
335 |
||
336 |
public void notifyBootstrapFinished() { |
|
337 |
bootstrapFinished = true; |
|
338 |
} |
|
339 |
||
340 |
@Override |
|
341 |
public boolean isBootstrapping() { |
|
342 |
return bootstrapJVMCI && !bootstrapFinished; |
|
343 |
} |
|
46371 | 344 |
|
345 |
@Override |
|
346 |
public boolean isShutdown() { |
|
347 |
return shutdown; |
|
348 |
} |
|
349 |
||
350 |
@Override |
|
46680 | 351 |
public DiagnosticsOutputDirectory getOutputDirectory() { |
352 |
return outputDirectory; |
|
46371 | 353 |
} |
46807 | 354 |
|
355 |
@Override |
|
356 |
public Map<ExceptionAction, Integer> getCompilationProblemsPerAction() { |
|
357 |
return compilationProblemsPerAction; |
|
358 |
} |
|
47557
8b2054b7d02c
8182701: Modify JVMCI to allow Graal Compiler to expose platform MBean
jtulach
parents:
47216
diff
changeset
|
359 |
|
49873 | 360 |
// ------- Management interface --------- |
361 |
||
362 |
private final HotSpotGraalManagementRegistration management; |
|
363 |
||
364 |
/** |
|
365 |
* @returns the management object for this runtime or {@code null} |
|
366 |
*/ |
|
367 |
public HotSpotGraalManagementRegistration getManagement() { |
|
368 |
return management; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Set of weak references to {@link ClassLoader}s available for resolving class names present in |
|
373 |
* management {@linkplain #invokeManagementAction(String, Object[]) action} arguments. |
|
374 |
*/ |
|
375 |
private final WeakClassLoaderSet loaders = new WeakClassLoaderSet(ClassLoader.getSystemClassLoader()); |
|
376 |
||
377 |
/** |
|
378 |
* Sets or updates this object's {@linkplain #getOptions() options} from {@code names} and |
|
379 |
* {@code values}. |
|
380 |
* |
|
381 |
* @param values the values to set. The empty string represents {@code null} which resets an |
|
382 |
* option to its default value. For string type options, a non-empty value must be |
|
383 |
* enclosed in double quotes. |
|
384 |
* @return an array of Strings where the element at index i is {@code names[i]} if setting the |
|
385 |
* denoted option succeeded, {@code null} if the option is unknown otherwise an error |
|
386 |
* message describing the failure to set the option |
|
387 |
*/ |
|
388 |
public String[] setOptionValues(String[] names, String[] values) { |
|
389 |
EconomicMap<String, OptionDescriptor> optionDescriptors = getOptionDescriptors(); |
|
390 |
EconomicMap<OptionKey<?>, Object> newValues = EconomicMap.create(names.length); |
|
391 |
EconomicSet<OptionKey<?>> resetValues = EconomicSet.create(names.length); |
|
392 |
String[] result = new String[names.length]; |
|
393 |
for (int i = 0; i < names.length; i++) { |
|
394 |
String name = names[i]; |
|
395 |
OptionDescriptor option = optionDescriptors.get(name); |
|
396 |
if (option != null) { |
|
397 |
String svalue = values[i]; |
|
398 |
Class<?> optionValueType = option.getOptionValueType(); |
|
399 |
OptionKey<?> optionKey = option.getOptionKey(); |
|
400 |
if (svalue == null || svalue.isEmpty() && !(optionKey instanceof EnumOptionKey)) { |
|
401 |
resetValues.add(optionKey); |
|
402 |
result[i] = name; |
|
403 |
} else { |
|
404 |
String valueToParse; |
|
405 |
if (optionValueType == String.class) { |
|
406 |
if (svalue.length() < 2 || svalue.charAt(0) != '"' || svalue.charAt(svalue.length() - 1) != '"') { |
|
407 |
result[i] = "Invalid value for String option '" + name + "': must be the empty string or be enclosed in double quotes: " + svalue; |
|
408 |
continue; |
|
409 |
} else { |
|
410 |
valueToParse = svalue.substring(1, svalue.length() - 1); |
|
411 |
} |
|
412 |
} else { |
|
413 |
valueToParse = svalue; |
|
414 |
} |
|
415 |
try { |
|
416 |
OptionsParser.parseOption(name, valueToParse, newValues, OptionsParser.getOptionsLoader()); |
|
417 |
result[i] = name; |
|
418 |
} catch (IllegalArgumentException e) { |
|
419 |
result[i] = e.getMessage(); |
|
420 |
continue; |
|
421 |
} |
|
422 |
} |
|
423 |
} else { |
|
424 |
result[i] = null; |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
OptionValues currentOptions; |
|
429 |
OptionValues newOptions; |
|
430 |
do { |
|
431 |
currentOptions = optionsRef.get(); |
|
432 |
UnmodifiableMapCursor<OptionKey<?>, Object> cursor = currentOptions.getMap().getEntries(); |
|
433 |
while (cursor.advance()) { |
|
434 |
OptionKey<?> key = cursor.getKey(); |
|
435 |
if (!resetValues.contains(key) && !newValues.containsKey(key)) { |
|
436 |
newValues.put(key, OptionValues.decodeNull(cursor.getValue())); |
|
437 |
} |
|
438 |
} |
|
439 |
newOptions = new OptionValues(newValues); |
|
440 |
} while (!optionsRef.compareAndSet(currentOptions, newOptions)); |
|
441 |
||
442 |
return result; |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Gets the values for the options corresponding to {@code names} encoded as strings. The empty |
|
447 |
* string represents {@code null}. For string type options, non-{@code null} values will be |
|
448 |
* enclosed in double quotes. |
|
449 |
* |
|
450 |
* @param names a list of option names |
|
451 |
* @return the values for each named option. If an element in {@code names} does not denote an |
|
452 |
* existing option, the corresponding element in the returned array will be {@code null} |
|
453 |
*/ |
|
454 |
public String[] getOptionValues(String... names) { |
|
455 |
String[] values = new String[names.length]; |
|
456 |
EconomicMap<String, OptionDescriptor> optionDescriptors = getOptionDescriptors(); |
|
457 |
for (int i = 0; i < names.length; i++) { |
|
458 |
OptionDescriptor option = optionDescriptors.get(names[i]); |
|
459 |
if (option != null) { |
|
460 |
OptionKey<?> optionKey = option.getOptionKey(); |
|
461 |
Object value = optionKey.getValue(getOptions()); |
|
462 |
String svalue; |
|
463 |
if (option.getOptionValueType() == String.class && value != null) { |
|
464 |
svalue = "\"" + value + "\""; |
|
465 |
} else if (value == null) { |
|
466 |
svalue = ""; |
|
467 |
} else { |
|
468 |
svalue = String.valueOf(value); |
|
469 |
} |
|
470 |
values[i] = svalue; |
|
471 |
} else { |
|
472 |
// null denotes the option does not exist |
|
473 |
values[i] = null; |
|
474 |
} |
|
475 |
} |
|
476 |
return values; |
|
477 |
} |
|
478 |
||
479 |
private static EconomicMap<String, OptionDescriptor> getOptionDescriptors() { |
|
480 |
EconomicMap<String, OptionDescriptor> result = EconomicMap.create(); |
|
481 |
for (OptionDescriptors set : OptionsParser.getOptionsLoader()) { |
|
482 |
for (OptionDescriptor option : set) { |
|
483 |
result.put(option.getName(), option); |
|
484 |
} |
|
485 |
} |
|
486 |
return result; |
|
487 |
} |
|
488 |
||
489 |
private void dumpMethod(String className, String methodName, String filter, String host, int port) throws Exception { |
|
490 |
EconomicSet<ClassNotFoundException> failures = EconomicSet.create(); |
|
491 |
EconomicSet<Class<?>> found = loaders.resolve(className, failures); |
|
492 |
if (found.isEmpty()) { |
|
493 |
ClassNotFoundException cause = failures.isEmpty() ? new ClassNotFoundException(className) : failures.iterator().next(); |
|
494 |
throw new Exception("Cannot find class " + className + " to schedule recompilation", cause); |
|
495 |
} |
|
496 |
for (Class<?> clazz : found) { |
|
497 |
ResolvedJavaType type = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(clazz); |
|
498 |
for (ResolvedJavaMethod method : type.getDeclaredMethods()) { |
|
499 |
if (methodName.equals(method.getName()) && method instanceof HotSpotResolvedJavaMethod) { |
|
500 |
HotSpotResolvedJavaMethod hotSpotMethod = (HotSpotResolvedJavaMethod) method; |
|
501 |
dumpMethod(hotSpotMethod, filter, host, port); |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
private void dumpMethod(HotSpotResolvedJavaMethod hotSpotMethod, String filter, String host, int port) throws Exception { |
|
508 |
EconomicMap<OptionKey<?>, Object> extra = EconomicMap.create(); |
|
509 |
extra.put(DebugOptions.Dump, filter); |
|
510 |
extra.put(DebugOptions.PrintGraphHost, host); |
|
511 |
extra.put(DebugOptions.PrintBinaryGraphPort, port); |
|
512 |
OptionValues compileOptions = new OptionValues(getOptions(), extra); |
|
513 |
compiler.compileMethod(new HotSpotCompilationRequest(hotSpotMethod, -1, 0L), false, compileOptions); |
|
514 |
} |
|
515 |
||
516 |
public Object invokeManagementAction(String actionName, Object[] params) throws Exception { |
|
517 |
if ("dumpMethod".equals(actionName)) { |
|
518 |
if (params.length != 0 && params[0] instanceof HotSpotResolvedJavaMethod) { |
|
519 |
HotSpotResolvedJavaMethod method = param(params, 0, "method", HotSpotResolvedJavaMethod.class, null); |
|
520 |
String filter = param(params, 1, "filter", String.class, ":3"); |
|
521 |
String host = param(params, 2, "host", String.class, "localhost"); |
|
522 |
Number port = param(params, 3, "port", Number.class, 4445); |
|
523 |
dumpMethod(method, filter, host, port.intValue()); |
|
524 |
} else { |
|
525 |
String className = param(params, 0, "className", String.class, null); |
|
526 |
String methodName = param(params, 1, "methodName", String.class, null); |
|
527 |
String filter = param(params, 2, "filter", String.class, ":3"); |
|
528 |
String host = param(params, 3, "host", String.class, "localhost"); |
|
529 |
Number port = param(params, 4, "port", Number.class, 4445); |
|
530 |
dumpMethod(className, methodName, filter, host, port.intValue()); |
|
531 |
} |
|
532 |
} |
|
533 |
return null; |
|
534 |
} |
|
535 |
||
536 |
private static <T> T param(Object[] arr, int index, String name, Class<T> type, T defaultValue) { |
|
537 |
Object value = arr.length > index ? arr[index] : null; |
|
538 |
if (value == null || (value instanceof String && ((String) value).isEmpty())) { |
|
539 |
if (defaultValue == null) { |
|
540 |
throw new IllegalArgumentException(name + " must be specified"); |
|
541 |
} |
|
542 |
value = defaultValue; |
|
543 |
} |
|
544 |
if (type.isInstance(value)) { |
|
545 |
return type.cast(value); |
|
546 |
} |
|
547 |
throw new IllegalArgumentException("Expecting " + type.getName() + " for " + name + " but was " + value); |
|
47557
8b2054b7d02c
8182701: Modify JVMCI to allow Graal Compiler to expose platform MBean
jtulach
parents:
47216
diff
changeset
|
548 |
} |
43972 | 549 |
} |