< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalServices.java

Print this page




  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 
  24 
  25 package org.graalvm.compiler.serviceprovider;
  26 
  27 import static java.lang.Thread.currentThread;
  28 
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.util.Arrays;
  32 import java.util.ArrayList;
  33 import java.util.HashMap;
  34 import java.util.Iterator;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.ServiceConfigurationError;
  38 import java.util.ServiceLoader;
  39 import java.util.concurrent.atomic.AtomicLong;
  40 import java.util.function.Supplier;
  41 
  42 import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup.SpeculationContextObject;
  43 
  44 import jdk.vm.ci.code.BytecodePosition;
  45 import jdk.vm.ci.meta.ResolvedJavaField;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
  49 import jdk.vm.ci.meta.SpeculationLog.SpeculationReasonEncoding;
  50 import jdk.vm.ci.runtime.JVMCI;
  51 import jdk.vm.ci.services.JVMCIPermission;
  52 import jdk.vm.ci.services.Services;
  53 
  54 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
  55 import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
  56 
  57 /**
  58  * JDK 13+ version of {@link GraalServices}.
  59  */
  60 public final class GraalServices {
  61 
  62     private static final Map<Class<?>, List<?>> servicesCache = IS_BUILDING_NATIVE_IMAGE ? new HashMap<>() : null;
  63 
  64     private GraalServices() {
  65     }
  66 
  67     /**
  68      * Gets an {@link Iterable} of the providers available for a given service.
  69      *
  70      * @throws SecurityException if on JDK8 and a security manager is present and it denies
  71      *             {@link JVMCIPermission}
  72      */
  73     @SuppressWarnings("unchecked")
  74     public static <S> Iterable<S> load(Class<S> service) {
  75         if (IS_IN_NATIVE_IMAGE || IS_BUILDING_NATIVE_IMAGE) {
  76             List<?> list = servicesCache.get(service);
  77             if (list != null) {
  78                 return (Iterable<S>) list;
  79             }
  80             if (IS_IN_NATIVE_IMAGE) {
  81                 throw new InternalError(String.format("No %s providers found when building native image", service.getName()));
  82             }
  83         }
  84 
  85         Iterable<S> providers = load0(service);
  86 
  87         if (IS_BUILDING_NATIVE_IMAGE) {
  88             synchronized (servicesCache) {
  89                 ArrayList<S> providersList = new ArrayList<>();
  90                 for (S provider : providers) {
  91                     providersList.add(provider);
  92                 }
  93                 providers = providersList;
  94                 servicesCache.put(service, providersList);
  95                 return providers;
  96             }
  97         }
  98 
  99         return providers;
 100     }
 101 
 102     protected static <S> Iterable<S> load0(Class<S> service) {
 103         Iterable<S> iterable = ServiceLoader.load(service, GraalServices.class.getClassLoader());
 104         return new Iterable<>() {
 105             @Override
 106             public Iterator<S> iterator() {
 107                 Iterator<S> iterator = iterable.iterator();
 108                 return new Iterator<>() {
 109                     @Override
 110                     public boolean hasNext() {
 111                         return iterator.hasNext();
 112                     }
 113 
 114                     @Override
 115                     public S next() {
 116                         S provider = iterator.next();
 117                         // Allow Graal extensions to access JVMCI
 118                         openJVMCITo(provider.getClass());
 119                         return provider;
 120                     }
 121 
 122                     @Override
 123                     public void remove() {
 124                         iterator.remove();
 125                     }
 126                 };
 127             }
 128         };
 129     }
 130 
 131     /**
 132      * Opens all JVMCI packages to the module of a given class. This relies on JVMCI already having
 133      * opened all its packages to the module defining {@link GraalServices}.
 134      *
 135      * @param other all JVMCI packages will be opened to the module defining this class
 136      */
 137     static void openJVMCITo(Class<?> other) {
 138         if (IS_IN_NATIVE_IMAGE) return;
 139 
 140         Module jvmciModule = JVMCI_MODULE;
 141         Module otherModule = other.getModule();
 142         if (jvmciModule != otherModule) {
 143             for (String pkg : jvmciModule.getPackages()) {
 144                 if (!jvmciModule.isOpen(pkg, otherModule)) {
 145                     // JVMCI initialization opens all JVMCI packages
 146                     // to Graal which is a prerequisite for Graal to
 147                     // open JVMCI packages to other modules.
 148                     JVMCI.getRuntime();
 149 
 150                     jvmciModule.addOpens(pkg, otherModule);
 151                 }
 152             }
 153         }
 154     }
 155 
 156     /**
 157      * Gets the provider for a given service for which at most one provider must be available.
 158      *
 159      * @param service the service whose provider is being requested
 160      * @param required specifies if an {@link InternalError} should be thrown if no provider of
 161      *            {@code service} is available
 162      * @return the requested provider if available else {@code null}
 163      * @throws SecurityException if on JDK8 and a security manager is present and it denies
 164      *             {@link JVMCIPermission}
 165      */
 166     public static <S> S loadSingle(Class<S> service, boolean required) {
 167         assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
 168         Iterable<S> providers = load(service);


 209      * trusted code.
 210      */
 211     public static boolean isToStringTrusted(Class<?> c) {
 212         Module module = c.getModule();
 213         Module jvmciModule = JVMCI_MODULE;
 214         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
 215         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
 216             // Can access non-statically-exported package in JVMCI
 217             return true;
 218         }
 219         return false;
 220     }
 221 
 222     /**
 223      * An implementation of {@link SpeculationReason} based on direct, unencoded values.
 224      */
 225     static final class DirectSpeculationReason implements SpeculationReason {
 226         final int groupId;
 227         final String groupName;
 228         final Object[] context;
 229         private SpeculationReasonEncoding encoding;
 230 
 231         DirectSpeculationReason(int groupId, String groupName, Object[] context) {
 232             this.groupId = groupId;
 233             this.groupName = groupName;
 234             this.context = context;
 235         }
 236 
 237         @Override
 238         public boolean equals(Object obj) {
 239             if (obj instanceof DirectSpeculationReason) {
 240                 DirectSpeculationReason that = (DirectSpeculationReason) obj;
 241                 return this.groupId == that.groupId && Arrays.equals(this.context, that.context);
 242             }
 243             return false;
 244         }
 245 
 246         @Override
 247         public int hashCode() {
 248             return groupId + Arrays.hashCode(this.context);
 249         }
 250 
 251         @Override
 252         public String toString() {
 253             return String.format("%s@%d%s", groupName, groupId, Arrays.toString(context));
 254         }
 255 
 256         @Override
 257         public SpeculationReasonEncoding encode(Supplier<SpeculationReasonEncoding> encodingSupplier) {
 258             if (encoding == null) {
 259                 encoding = encodingSupplier.get();
 260                 encoding.addInt(groupId);
 261                 for (Object o : context) {
 262                     if (o == null) {
 263                         encoding.addInt(0);
 264                     } else {
 265                         addNonNullObject(encoding, o);
 266                     }
 267                 }
 268             }
 269             return encoding;
 270         }
 271 
 272         static void addNonNullObject(SpeculationReasonEncoding encoding, Object o) {
 273             Class<? extends Object> c = o.getClass();
 274             if (c == String.class) {
 275                 encoding.addString((String) o);
 276             } else if (c == Byte.class) {
 277                 encoding.addByte((Byte) o);
 278             } else if (c == Short.class) {
 279                 encoding.addShort((Short) o);
 280             } else if (c == Character.class) {
 281                 encoding.addShort((Character) o);
 282             } else if (c == Integer.class) {
 283                 encoding.addInt((Integer) o);
 284             } else if (c == Long.class) {
 285                 encoding.addLong((Long) o);
 286             } else if (c == Float.class) {
 287                 encoding.addInt(Float.floatToRawIntBits((Float) o));
 288             } else if (c == Double.class) {
 289                 encoding.addLong(Double.doubleToRawLongBits((Double) o));
 290             } else if (o instanceof Enum) {
 291                 encoding.addInt(((Enum<?>) o).ordinal());
 292             } else if (o instanceof ResolvedJavaMethod) {
 293                 encoding.addMethod((ResolvedJavaMethod) o);
 294             } else if (o instanceof ResolvedJavaType) {
 295                 encoding.addType((ResolvedJavaType) o);
 296             } else if (o instanceof ResolvedJavaField) {
 297                 encoding.addField((ResolvedJavaField) o);
 298             } else if (o instanceof SpeculationContextObject) {
 299                 SpeculationContextObject sco = (SpeculationContextObject) o;
 300                 // These are compiler objects which all have the same class
 301                 // loader so the class name uniquely identifies the class.
 302                 encoding.addString(o.getClass().getName());
 303                 sco.accept(new EncodingAdapter(encoding));
 304             } else if (o.getClass() == BytecodePosition.class) {
 305                 BytecodePosition p = (BytecodePosition) o;
 306                 while (p != null) {
 307                     encoding.addInt(p.getBCI());
 308                     encoding.addMethod(p.getMethod());
 309                     p = p.getCaller();
 310                 }
 311             } else {
 312                 throw new IllegalArgumentException("Unsupported type for encoding: " + c.getName());
 313             }
 314         }
 315     }
 316 
 317     static class EncodingAdapter implements SpeculationContextObject.Visitor {
 318         private final SpeculationReasonEncoding encoding;
 319 
 320         EncodingAdapter(SpeculationReasonEncoding encoding) {
 321             this.encoding = encoding;
 322         }
 323 
 324         @Override
 325         public void visitBoolean(boolean v) {
 326             encoding.addByte(v ? 1 : 0);
 327         }
 328 
 329         @Override
 330         public void visitByte(byte v) {
 331             encoding.addByte(v);
 332         }
 333 
 334         @Override
 335         public void visitChar(char v) {
 336             encoding.addShort(v);
 337         }
 338 
 339         @Override
 340         public void visitShort(short v) {
 341             encoding.addInt(v);
 342         }
 343 
 344         @Override
 345         public void visitInt(int v) {
 346             encoding.addInt(v);
 347         }
 348 
 349         @Override
 350         public void visitLong(long v) {
 351             encoding.addLong(v);
 352         }
 353 
 354         @Override
 355         public void visitFloat(float v) {
 356             encoding.addInt(Float.floatToRawIntBits(v));
 357         }
 358 
 359         @Override
 360         public void visitDouble(double v) {
 361             encoding.addLong(Double.doubleToRawLongBits(v));
 362         }
 363 
 364         @Override
 365         public void visitObject(Object v) {
 366             if (v == null) {
 367                 encoding.addInt(0);
 368             } else {
 369                 DirectSpeculationReason.addNonNullObject(encoding, v);
 370             }
 371         }
 372     }
 373 
 374     static SpeculationReason createSpeculationReason(int groupId, String groupName, Object... context) {
 375         return new DirectSpeculationReason(groupId, groupName, context);
 376     }
 377 
 378     /**
 379      * Gets a unique identifier for this execution such as a process ID or a
 380      * {@linkplain #getGlobalTimeStamp() fixed timestamp}.
 381      */
 382     public static String getExecutionID() {
 383         return Long.toString(ProcessHandle.current().pid());
 384     }
 385 
 386     private static final AtomicLong globalTimeStamp = new AtomicLong();
 387 
 388     /**
 389      * Gets a time stamp for the current process. This method will always return the same value for
 390      * the current VM execution.
 391      */


 485      * arguments to the {@code main} method. This method returns an empty list if there is no input
 486      * argument to the Java virtual machine.
 487      * <p>
 488      * Some Java virtual machine implementations may take input arguments from multiple different
 489      * sources: for examples, arguments passed from the application that launches the Java virtual
 490      * machine such as the 'java' command, environment variables, configuration files, etc.
 491      * <p>
 492      * Typically, not all command-line options to the 'java' command are passed to the Java virtual
 493      * machine. Thus, the returned input arguments may not include all command-line options.
 494      *
 495      * @return the input arguments to the JVM or {@code null} if they are unavailable
 496      */
 497     public static List<String> getInputArguments() {
 498         JMXService jmx = JMXService.instance;
 499         if (jmx == null) {
 500             return null;
 501         }
 502         return jmx.getInputArguments();
 503     }
 504 }



  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 
  24 
  25 package org.graalvm.compiler.serviceprovider;
  26 
  27 import static java.lang.Thread.currentThread;
  28 
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.util.Arrays;


  32 import java.util.Iterator;
  33 import java.util.List;

  34 import java.util.ServiceConfigurationError;
  35 import java.util.ServiceLoader;
  36 import java.util.concurrent.atomic.AtomicLong;

  37 






  38 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;

  39 import jdk.vm.ci.runtime.JVMCI;
  40 import jdk.vm.ci.services.JVMCIPermission;
  41 import jdk.vm.ci.services.Services;
  42 



  43 /**
  44  * JDK 9+ version of {@link GraalServices}.
  45  */
  46 public final class GraalServices {
  47 


  48     private GraalServices() {
  49     }
  50 
  51     /**
  52      * Gets an {@link Iterable} of the providers available for a given service.
  53      *
  54      * @throws SecurityException if on JDK8 and a security manager is present and it denies
  55      *             {@link JVMCIPermission}
  56      */

  57     public static <S> Iterable<S> load(Class<S> service) {
  58         Iterable<S> iterable = ServiceLoader.load(service);




























  59         return new Iterable<>() {
  60             @Override
  61             public Iterator<S> iterator() {
  62                 Iterator<S> iterator = iterable.iterator();
  63                 return new Iterator<>() {
  64                     @Override
  65                     public boolean hasNext() {
  66                         return iterator.hasNext();
  67                     }
  68 
  69                     @Override
  70                     public S next() {
  71                         S provider = iterator.next();
  72                         // Allow Graal extensions to access JVMCI
  73                         openJVMCITo(provider.getClass());
  74                         return provider;
  75                     }
  76 
  77                     @Override
  78                     public void remove() {
  79                         iterator.remove();
  80                     }
  81                 };
  82             }
  83         };
  84     }
  85 
  86     /**
  87      * Opens all JVMCI packages to the module of a given class. This relies on JVMCI already having
  88      * opened all its packages to the module defining {@link GraalServices}.
  89      *
  90      * @param other all JVMCI packages will be opened to the module defining this class
  91      */
  92     static void openJVMCITo(Class<?> other) {


  93         Module jvmciModule = JVMCI_MODULE;
  94         Module otherModule = other.getModule();
  95         if (jvmciModule != otherModule) {
  96             for (String pkg : jvmciModule.getPackages()) {
  97                 if (!jvmciModule.isOpen(pkg, otherModule)) {
  98                     // JVMCI initialization opens all JVMCI packages
  99                     // to Graal which is a prerequisite for Graal to
 100                     // open JVMCI packages to other modules.
 101                     JVMCI.initialize();
 102 
 103                     jvmciModule.addOpens(pkg, otherModule);
 104                 }
 105             }
 106         }
 107     }
 108 
 109     /**
 110      * Gets the provider for a given service for which at most one provider must be available.
 111      *
 112      * @param service the service whose provider is being requested
 113      * @param required specifies if an {@link InternalError} should be thrown if no provider of
 114      *            {@code service} is available
 115      * @return the requested provider if available else {@code null}
 116      * @throws SecurityException if on JDK8 and a security manager is present and it denies
 117      *             {@link JVMCIPermission}
 118      */
 119     public static <S> S loadSingle(Class<S> service, boolean required) {
 120         assert !service.getName().startsWith("jdk.vm.ci") : "JVMCI services must be loaded via " + Services.class.getName();
 121         Iterable<S> providers = load(service);


 162      * trusted code.
 163      */
 164     public static boolean isToStringTrusted(Class<?> c) {
 165         Module module = c.getModule();
 166         Module jvmciModule = JVMCI_MODULE;
 167         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
 168         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
 169             // Can access non-statically-exported package in JVMCI
 170             return true;
 171         }
 172         return false;
 173     }
 174 
 175     /**
 176      * An implementation of {@link SpeculationReason} based on direct, unencoded values.
 177      */
 178     static final class DirectSpeculationReason implements SpeculationReason {
 179         final int groupId;
 180         final String groupName;
 181         final Object[] context;

 182 
 183         DirectSpeculationReason(int groupId, String groupName, Object[] context) {
 184             this.groupId = groupId;
 185             this.groupName = groupName;
 186             this.context = context;
 187         }
 188 
 189         @Override
 190         public boolean equals(Object obj) {
 191             if (obj instanceof DirectSpeculationReason) {
 192                 DirectSpeculationReason that = (DirectSpeculationReason) obj;
 193                 return this.groupId == that.groupId && Arrays.equals(this.context, that.context);
 194             }
 195             return false;
 196         }
 197 
 198         @Override
 199         public int hashCode() {
 200             return groupId + Arrays.hashCode(this.context);
 201         }
 202 
 203         @Override
 204         public String toString() {
 205             return String.format("%s@%d%s", groupName, groupId, Arrays.toString(context));
 206         }





















































































































 207     }
 208 
 209     static SpeculationReason createSpeculationReason(int groupId, String groupName, Object... context) {
 210         return new DirectSpeculationReason(groupId, groupName, context);
 211     }
 212 
 213     /**
 214      * Gets a unique identifier for this execution such as a process ID or a
 215      * {@linkplain #getGlobalTimeStamp() fixed timestamp}.
 216      */
 217     public static String getExecutionID() {
 218         return Long.toString(ProcessHandle.current().pid());
 219     }
 220 
 221     private static final AtomicLong globalTimeStamp = new AtomicLong();
 222 
 223     /**
 224      * Gets a time stamp for the current process. This method will always return the same value for
 225      * the current VM execution.
 226      */


 320      * arguments to the {@code main} method. This method returns an empty list if there is no input
 321      * argument to the Java virtual machine.
 322      * <p>
 323      * Some Java virtual machine implementations may take input arguments from multiple different
 324      * sources: for examples, arguments passed from the application that launches the Java virtual
 325      * machine such as the 'java' command, environment variables, configuration files, etc.
 326      * <p>
 327      * Typically, not all command-line options to the 'java' command are passed to the Java virtual
 328      * machine. Thus, the returned input arguments may not include all command-line options.
 329      *
 330      * @return the input arguments to the JVM or {@code null} if they are unavailable
 331      */
 332     public static List<String> getInputArguments() {
 333         JMXService jmx = JMXService.instance;
 334         if (jmx == null) {
 335             return null;
 336         }
 337         return jmx.getInputArguments();
 338     }
 339 }
 340 
< prev index next >