< prev index next >
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalServices.java
Print this page
@@ -27,82 +27,37 @@
import static java.lang.Thread.currentThread;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.Supplier;
-import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup.SpeculationContextObject;
-
-import jdk.vm.ci.code.BytecodePosition;
-import jdk.vm.ci.meta.ResolvedJavaField;
-import jdk.vm.ci.meta.ResolvedJavaMethod;
-import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
-import jdk.vm.ci.meta.SpeculationLog.SpeculationReasonEncoding;
import jdk.vm.ci.runtime.JVMCI;
import jdk.vm.ci.services.JVMCIPermission;
import jdk.vm.ci.services.Services;
-import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
-import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
-
/**
- * JDK 13+ version of {@link GraalServices}.
+ * JDK 9+ version of {@link GraalServices}.
*/
public final class GraalServices {
- private static final Map<Class<?>, List<?>> servicesCache = IS_BUILDING_NATIVE_IMAGE ? new HashMap<>() : null;
-
private GraalServices() {
}
/**
* Gets an {@link Iterable} of the providers available for a given service.
*
* @throws SecurityException if on JDK8 and a security manager is present and it denies
* {@link JVMCIPermission}
*/
- @SuppressWarnings("unchecked")
public static <S> Iterable<S> load(Class<S> service) {
- if (IS_IN_NATIVE_IMAGE || IS_BUILDING_NATIVE_IMAGE) {
- List<?> list = servicesCache.get(service);
- if (list != null) {
- return (Iterable<S>) list;
- }
- if (IS_IN_NATIVE_IMAGE) {
- throw new InternalError(String.format("No %s providers found when building native image", service.getName()));
- }
- }
-
- Iterable<S> providers = load0(service);
-
- if (IS_BUILDING_NATIVE_IMAGE) {
- synchronized (servicesCache) {
- ArrayList<S> providersList = new ArrayList<>();
- for (S provider : providers) {
- providersList.add(provider);
- }
- providers = providersList;
- servicesCache.put(service, providersList);
- return providers;
- }
- }
-
- return providers;
- }
-
- protected static <S> Iterable<S> load0(Class<S> service) {
- Iterable<S> iterable = ServiceLoader.load(service, GraalServices.class.getClassLoader());
+ Iterable<S> iterable = ServiceLoader.load(service);
return new Iterable<>() {
@Override
public Iterator<S> iterator() {
Iterator<S> iterator = iterable.iterator();
return new Iterator<>() {
@@ -133,21 +88,19 @@
* opened all its packages to the module defining {@link GraalServices}.
*
* @param other all JVMCI packages will be opened to the module defining this class
*/
static void openJVMCITo(Class<?> other) {
- if (IS_IN_NATIVE_IMAGE) return;
-
Module jvmciModule = JVMCI_MODULE;
Module otherModule = other.getModule();
if (jvmciModule != otherModule) {
for (String pkg : jvmciModule.getPackages()) {
if (!jvmciModule.isOpen(pkg, otherModule)) {
// JVMCI initialization opens all JVMCI packages
// to Graal which is a prerequisite for Graal to
// open JVMCI packages to other modules.
- JVMCI.getRuntime();
+ JVMCI.initialize();
jvmciModule.addOpens(pkg, otherModule);
}
}
}
@@ -224,11 +177,10 @@
*/
static final class DirectSpeculationReason implements SpeculationReason {
final int groupId;
final String groupName;
final Object[] context;
- private SpeculationReasonEncoding encoding;
DirectSpeculationReason(int groupId, String groupName, Object[] context) {
this.groupId = groupId;
this.groupName = groupName;
this.context = context;
@@ -250,127 +202,10 @@
@Override
public String toString() {
return String.format("%s@%d%s", groupName, groupId, Arrays.toString(context));
}
-
- @Override
- public SpeculationReasonEncoding encode(Supplier<SpeculationReasonEncoding> encodingSupplier) {
- if (encoding == null) {
- encoding = encodingSupplier.get();
- encoding.addInt(groupId);
- for (Object o : context) {
- if (o == null) {
- encoding.addInt(0);
- } else {
- addNonNullObject(encoding, o);
- }
- }
- }
- return encoding;
- }
-
- static void addNonNullObject(SpeculationReasonEncoding encoding, Object o) {
- Class<? extends Object> c = o.getClass();
- if (c == String.class) {
- encoding.addString((String) o);
- } else if (c == Byte.class) {
- encoding.addByte((Byte) o);
- } else if (c == Short.class) {
- encoding.addShort((Short) o);
- } else if (c == Character.class) {
- encoding.addShort((Character) o);
- } else if (c == Integer.class) {
- encoding.addInt((Integer) o);
- } else if (c == Long.class) {
- encoding.addLong((Long) o);
- } else if (c == Float.class) {
- encoding.addInt(Float.floatToRawIntBits((Float) o));
- } else if (c == Double.class) {
- encoding.addLong(Double.doubleToRawLongBits((Double) o));
- } else if (o instanceof Enum) {
- encoding.addInt(((Enum<?>) o).ordinal());
- } else if (o instanceof ResolvedJavaMethod) {
- encoding.addMethod((ResolvedJavaMethod) o);
- } else if (o instanceof ResolvedJavaType) {
- encoding.addType((ResolvedJavaType) o);
- } else if (o instanceof ResolvedJavaField) {
- encoding.addField((ResolvedJavaField) o);
- } else if (o instanceof SpeculationContextObject) {
- SpeculationContextObject sco = (SpeculationContextObject) o;
- // These are compiler objects which all have the same class
- // loader so the class name uniquely identifies the class.
- encoding.addString(o.getClass().getName());
- sco.accept(new EncodingAdapter(encoding));
- } else if (o.getClass() == BytecodePosition.class) {
- BytecodePosition p = (BytecodePosition) o;
- while (p != null) {
- encoding.addInt(p.getBCI());
- encoding.addMethod(p.getMethod());
- p = p.getCaller();
- }
- } else {
- throw new IllegalArgumentException("Unsupported type for encoding: " + c.getName());
- }
- }
- }
-
- static class EncodingAdapter implements SpeculationContextObject.Visitor {
- private final SpeculationReasonEncoding encoding;
-
- EncodingAdapter(SpeculationReasonEncoding encoding) {
- this.encoding = encoding;
- }
-
- @Override
- public void visitBoolean(boolean v) {
- encoding.addByte(v ? 1 : 0);
- }
-
- @Override
- public void visitByte(byte v) {
- encoding.addByte(v);
- }
-
- @Override
- public void visitChar(char v) {
- encoding.addShort(v);
- }
-
- @Override
- public void visitShort(short v) {
- encoding.addInt(v);
- }
-
- @Override
- public void visitInt(int v) {
- encoding.addInt(v);
- }
-
- @Override
- public void visitLong(long v) {
- encoding.addLong(v);
- }
-
- @Override
- public void visitFloat(float v) {
- encoding.addInt(Float.floatToRawIntBits(v));
- }
-
- @Override
- public void visitDouble(double v) {
- encoding.addLong(Double.doubleToRawLongBits(v));
- }
-
- @Override
- public void visitObject(Object v) {
- if (v == null) {
- encoding.addInt(0);
- } else {
- DirectSpeculationReason.addNonNullObject(encoding, v);
- }
- }
}
static SpeculationReason createSpeculationReason(int groupId, String groupName, Object... context) {
return new DirectSpeculationReason(groupId, groupName, context);
}
@@ -500,5 +335,6 @@
return null;
}
return jmx.getInputArguments();
}
}
+
< prev index next >