Search in sources :

Example 1 with HotSpotCompilationRequest

use of jdk.vm.ci.hotspot.HotSpotCompilationRequest in project graal by oracle.

the class HotSpotGraalCompiler method compileMethod.

@SuppressWarnings("try")
CompilationRequestResult compileMethod(CompilationRequest request, boolean installAsDefault) {
    if (graalRuntime.isShutdown()) {
        return HotSpotCompilationRequestResult.failure(String.format("Shutdown entered"), false);
    }
    ResolvedJavaMethod method = request.getMethod();
    OptionValues options = graalRuntime.getOptions(method);
    if (graalRuntime.isBootstrapping()) {
        if (DebugOptions.BootstrapInitializeOnly.getValue(options)) {
            return HotSpotCompilationRequestResult.failure(String.format("Skip compilation because %s is enabled", DebugOptions.BootstrapInitializeOnly.getName()), true);
        }
        if (bootstrapWatchDog != null) {
            if (bootstrapWatchDog.hitCriticalCompilationRateOrTimeout()) {
                // Drain the compilation queue to expedite completion of the bootstrap
                return HotSpotCompilationRequestResult.failure("hit critical bootstrap compilation rate or timeout", true);
            }
        }
    }
    HotSpotCompilationRequest hsRequest = (HotSpotCompilationRequest) request;
    try (CompilationWatchDog w1 = CompilationWatchDog.watch(method, hsRequest.getId(), options);
        BootstrapWatchDog.Watch w2 = bootstrapWatchDog == null ? null : bootstrapWatchDog.watch(request);
        CompilationAlarm alarm = CompilationAlarm.trackCompilationPeriod(options)) {
        if (compilationCounters != null) {
            compilationCounters.countCompilation(method);
        }
        CompilationTask task = new CompilationTask(jvmciRuntime, this, hsRequest, true, installAsDefault, options);
        CompilationRequestResult r = null;
        try (DebugContext debug = graalRuntime.openDebugContext(options, task.getCompilationIdentifier(), method, getDebugHandlersFactories());
            Activation a = debug.activate()) {
            r = task.runCompilation(debug);
        }
        assert r != null;
        return r;
    }
}
Also used : CompilationRequestResult(jdk.vm.ci.code.CompilationRequestResult) HotSpotCompilationRequestResult(jdk.vm.ci.hotspot.HotSpotCompilationRequestResult) OptionValues(org.graalvm.compiler.options.OptionValues) CompilationAlarm(org.graalvm.compiler.core.common.util.CompilationAlarm) Activation(org.graalvm.compiler.debug.DebugContext.Activation) DebugContext(org.graalvm.compiler.debug.DebugContext) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 2 with HotSpotCompilationRequest

use of jdk.vm.ci.hotspot.HotSpotCompilationRequest in project graal by oracle.

the class MemoryUsageBenchmark method doCompilation.

@SuppressWarnings("try")
private void doCompilation(String methodName, String label) {
    HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(methodName);
    // invalidate any existing compiled code
    method.reprofile();
    long jvmciEnv = 0L;
    try (MemoryUsageCloseable c = label == null ? null : new MemoryUsageCloseable(label)) {
        HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
        int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
        HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, jvmciEnv);
        CompilationTask task = new CompilationTask(runtime, (HotSpotGraalCompiler) runtime.getCompiler(), request, true, false, getInitialOptions());
        task.runCompilation();
    }
}
Also used : HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask) HotSpotJVMCIRuntimeProvider(jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider)

Example 3 with HotSpotCompilationRequest

use of jdk.vm.ci.hotspot.HotSpotCompilationRequest in project graal by oracle.

the class CompileTheWorld method compileMethod.

/**
 * Compiles a method and gathers some statistics.
 */
private void compileMethod(HotSpotResolvedJavaMethod method, int counter) {
    try {
        long start = System.currentTimeMillis();
        long allocatedAtStart = getCurrentThreadAllocatedBytes();
        int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
        HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, 0L);
        // For more stable CTW execution, disable use of profiling information
        boolean useProfilingInfo = false;
        boolean installAsDefault = false;
        CompilationTask task = new CompilationTask(jvmciRuntime, compiler, request, useProfilingInfo, installAsDefault, currentOptions);
        task.runCompilation();
        // Invalidate the generated code so the code cache doesn't fill up
        HotSpotInstalledCode installedCode = task.getInstalledCode();
        if (installedCode != null) {
            installedCode.invalidate();
        }
        memoryUsed.getAndAdd(getCurrentThreadAllocatedBytes() - allocatedAtStart);
        compileTime.getAndAdd(System.currentTimeMillis() - start);
        compiledMethodsCounter.incrementAndGet();
    } catch (Throwable t) {
        // Catch everything and print a message
        println("CompileTheWorld (%d) : Error compiling method: %s", counter, method.format("%H.%n(%p):%r"));
        printStackTrace(t);
    }
}
Also used : HotSpotInstalledCode(jdk.vm.ci.hotspot.HotSpotInstalledCode) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) Print(org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Print) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask)

Example 4 with HotSpotCompilationRequest

use of jdk.vm.ci.hotspot.HotSpotCompilationRequest in project graal by oracle.

the class CompileTheWorld method compile.

/**
 * Compiles all methods in all classes in a given class path.
 *
 * @param classPath class path denoting classes to compile
 * @throws IOException
 */
@SuppressWarnings("try")
private void compile(String classPath) throws IOException {
    final String[] entries = classPath.split(File.pathSeparator);
    long start = System.currentTimeMillis();
    Map<Thread, StackTraceElement[]> initialThreads = Thread.getAllStackTraces();
    try {
        // compile dummy method to get compiler initialized outside of the
        // config debug override.
        HotSpotResolvedJavaMethod dummyMethod = (HotSpotResolvedJavaMethod) JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaMethod(CompileTheWorld.class.getDeclaredMethod("dummy"));
        int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
        boolean useProfilingInfo = false;
        boolean installAsDefault = false;
        CompilationTask task = new CompilationTask(jvmciRuntime, compiler, new HotSpotCompilationRequest(dummyMethod, entryBCI, 0L), useProfilingInfo, installAsDefault, currentOptions);
        task.runCompilation();
    } catch (NoSuchMethodException | SecurityException e1) {
        printStackTrace(e1);
    }
    /*
         * Always use a thread pool, even for single threaded mode since it simplifies the use of
         * DebugValueThreadFilter to filter on the thread names.
         */
    int threadCount = 1;
    if (Options.MultiThreaded.getValue(currentOptions)) {
        threadCount = Options.Threads.getValue(currentOptions);
        if (threadCount == 0) {
            threadCount = Runtime.getRuntime().availableProcessors();
        }
    } else {
        running = true;
    }
    OptionValues savedOptions = currentOptions;
    currentOptions = new OptionValues(compilationOptions);
    threadPool = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new CompilerThreadFactory("CompileTheWorld"));
    try {
        for (int i = 0; i < entries.length; i++) {
            final String entry = entries[i];
            ClassPathEntry cpe;
            if (entry.endsWith(".zip") || entry.endsWith(".jar")) {
                cpe = new JarClassPathEntry(entry);
            } else if (entry.equals(JRT_CLASS_PATH_ENTRY)) {
                cpe = new JRTClassPathEntry(entry, Options.LimitModules.getValue(currentOptions));
            } else {
                if (!new File(entry).isDirectory()) {
                    println("CompileTheWorld : Skipped classes in " + entry);
                    println();
                    continue;
                }
                cpe = new DirClassPathEntry(entry);
            }
            if (methodFilters == null || methodFilters.length == 0) {
                println("CompileTheWorld : Compiling all classes in " + entry);
            } else {
                String include = Arrays.asList(methodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
                println("CompileTheWorld : Compiling all methods in " + entry + " matching one of the following filters: " + include);
            }
            if (excludeMethodFilters != null && excludeMethodFilters.length > 0) {
                String exclude = Arrays.asList(excludeMethodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", "));
                println("CompileTheWorld : Excluding all methods matching one of the following filters: " + exclude);
            }
            println();
            ClassLoader loader = cpe.createClassLoader();
            for (String className : cpe.getClassNames()) {
                // Are we done?
                if (classFileCounter >= stopAt) {
                    break;
                }
                classFileCounter++;
                if (className.startsWith("jdk.management.") || className.startsWith("jdk.internal.cmm.*") || // These threads tend to cause deadlock at VM exit
                className.startsWith("sun.tools.jconsole.")) {
                    continue;
                }
                if (!isClassIncluded(className)) {
                    continue;
                }
                try {
                    // Load and initialize class
                    Class<?> javaClass = Class.forName(className, true, loader);
                    // Pre-load all classes in the constant pool.
                    try {
                        HotSpotResolvedObjectType objectType = HotSpotResolvedObjectType.fromObjectClass(javaClass);
                        ConstantPool constantPool = objectType.getConstantPool();
                        for (int cpi = 1; cpi < constantPool.length(); cpi++) {
                            constantPool.loadReferencedType(cpi, Bytecodes.LDC);
                        }
                    } catch (Throwable t) {
                        // If something went wrong during pre-loading we just ignore it.
                        if (isClassIncluded(className)) {
                            println("Preloading failed for (%d) %s: %s", classFileCounter, className, t);
                        }
                        continue;
                    }
                    // Are we compiling this class?
                    MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
                    if (classFileCounter >= startAt) {
                        println("CompileTheWorld (%d) : %s", classFileCounter, className);
                        // Compile each constructor/method in the class.
                        for (Constructor<?> constructor : javaClass.getDeclaredConstructors()) {
                            HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(constructor);
                            if (canBeCompiled(javaMethod, constructor.getModifiers())) {
                                compileMethod(javaMethod);
                            }
                        }
                        for (Method method : javaClass.getDeclaredMethods()) {
                            HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(method);
                            if (canBeCompiled(javaMethod, method.getModifiers())) {
                                compileMethod(javaMethod);
                            }
                        }
                        // Also compile the class initializer if it exists
                        HotSpotResolvedJavaMethod clinit = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaType(javaClass).getClassInitializer();
                        if (clinit != null && canBeCompiled(clinit, clinit.getModifiers())) {
                            compileMethod(clinit);
                        }
                    }
                } catch (Throwable t) {
                    if (isClassIncluded(className)) {
                        println("CompileTheWorld (%d) : Skipping %s %s", classFileCounter, className, t.toString());
                        printStackTrace(t);
                    }
                }
            }
            cpe.close();
        }
    } finally {
        currentOptions = savedOptions;
    }
    if (!running) {
        startThreads();
    }
    int wakeups = 0;
    while (threadPool.getCompletedTaskCount() != threadPool.getTaskCount()) {
        if (wakeups % 15 == 0) {
            TTY.println("CompileTheWorld : Waiting for " + (threadPool.getTaskCount() - threadPool.getCompletedTaskCount()) + " compiles");
        }
        try {
            threadPool.awaitTermination(1, TimeUnit.SECONDS);
            wakeups++;
        } catch (InterruptedException e) {
        }
    }
    threadPool = null;
    long elapsedTime = System.currentTimeMillis() - start;
    println();
    if (Options.MultiThreaded.getValue(currentOptions)) {
        TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms elapsed, %d ms compile time, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), elapsedTime, compileTime.get(), memoryUsed.get());
    } else {
        TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), compileTime.get(), memoryUsed.get());
    }
    // Apart from the main thread, there should be only be daemon threads
    // alive now. If not, then a class initializer has probably started
    // a thread that could cause a deadlock while trying to exit the VM.
    // One known example of this is sun.tools.jconsole.OutputViewer which
    // spawns threads to redirect sysout and syserr. To help debug such
    // scenarios, the stacks of potentially problematic threads are dumped.
    Map<Thread, StackTraceElement[]> suspiciousThreads = new HashMap<>();
    for (Map.Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
        Thread thread = e.getKey();
        if (thread != Thread.currentThread() && !initialThreads.containsKey(thread) && !thread.isDaemon() && thread.isAlive()) {
            suspiciousThreads.put(thread, e.getValue());
        }
    }
    if (!suspiciousThreads.isEmpty()) {
        TTY.println("--- Non-daemon threads started during CTW ---");
        for (Map.Entry<Thread, StackTraceElement[]> e : suspiciousThreads.entrySet()) {
            Thread thread = e.getKey();
            if (thread.isAlive()) {
                TTY.println(thread.toString() + " " + thread.getState());
                for (StackTraceElement ste : e.getValue()) {
                    TTY.println("\tat " + ste);
                }
            }
        }
        TTY.println("---------------------------------------------");
    }
}
Also used : OptionValues(org.graalvm.compiler.options.OptionValues) HashMap(java.util.HashMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) URLClassLoader(java.net.URLClassLoader) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) Method(java.lang.reflect.Method) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) Print(org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Print) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask) HotSpotResolvedObjectType(jdk.vm.ci.hotspot.HotSpotResolvedObjectType) ConstantPool(jdk.vm.ci.meta.ConstantPool) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) JarFile(java.util.jar.JarFile) File(java.io.File) EconomicMap(org.graalvm.collections.EconomicMap) Map(java.util.Map) HashMap(java.util.HashMap) UnmodifiableEconomicMap(org.graalvm.collections.UnmodifiableEconomicMap) CompilerThreadFactory(org.graalvm.compiler.core.CompilerThreadFactory) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider)

Example 5 with HotSpotCompilationRequest

use of jdk.vm.ci.hotspot.HotSpotCompilationRequest in project graal by oracle.

the class GraalOSRTestBase method compile.

protected static void compile(DebugContext debug, ResolvedJavaMethod method, int bci) {
    HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
    long jvmciEnv = 0L;
    HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
    HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
    CompilationTask task = new CompilationTask(runtime, compiler, request, true, true, debug.getOptions());
    if (method instanceof HotSpotResolvedJavaMethod) {
        HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
        GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
        if (((HotSpotResolvedJavaMethod) method).hasCodeAtLevel(bci, config.compilationLevelFullOptimization)) {
            return;
        }
    }
    HotSpotCompilationRequestResult result = task.runCompilation(debug);
    if (result.getFailure() != null) {
        throw new GraalError(result.getFailureMessage());
    }
}
Also used : HotSpotGraalCompiler(org.graalvm.compiler.hotspot.HotSpotGraalCompiler) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) GraalError(org.graalvm.compiler.debug.GraalError) HotSpotCompilationRequestResult(jdk.vm.ci.hotspot.HotSpotCompilationRequestResult) HotSpotGraalRuntimeProvider(org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask) GraalHotSpotVMConfig(org.graalvm.compiler.hotspot.GraalHotSpotVMConfig) HotSpotJVMCIRuntimeProvider(jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider)

Aggregations

HotSpotCompilationRequest (jdk.vm.ci.hotspot.HotSpotCompilationRequest)8 HotSpotResolvedJavaMethod (jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod)6 CompilationTask (org.graalvm.compiler.hotspot.CompilationTask)5 HotSpotJVMCIRuntimeProvider (jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider)3 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)3 OptionValues (org.graalvm.compiler.options.OptionValues)3 HotSpotCompilationRequestResult (jdk.vm.ci.hotspot.HotSpotCompilationRequestResult)2 Print (org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Print)2 HotSpotGraalCompiler (org.graalvm.compiler.hotspot.HotSpotGraalCompiler)2 File (java.io.File)1 Reference (java.lang.ref.Reference)1 WeakReference (java.lang.ref.WeakReference)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 JarFile (java.util.jar.JarFile)1 CompilationRequestResult (jdk.vm.ci.code.CompilationRequestResult)1