Search in sources :

Example 1 with CompilerThreadFactory

use of org.graalvm.compiler.core.CompilerThreadFactory 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 2 with CompilerThreadFactory

use of org.graalvm.compiler.core.CompilerThreadFactory in project graal by oracle.

the class CheckGraalInvariants method runTest.

@SuppressWarnings("try")
public static void runTest(InvariantsTool tool) {
    RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
    Providers providers = rt.getHostBackend().getProviders();
    MetaAccessProvider metaAccess = providers.getMetaAccess();
    PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
    Plugins plugins = new Plugins(new InvocationPlugins());
    GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withUnresolvedIsError(true);
    graphBuilderSuite.appendPhase(new GraphBuilderPhase(config));
    HighTierContext context = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.NONE);
    Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
    String bootclasspath = tool.getClassPath();
    Assert.assertNotNull("Cannot find boot class path", bootclasspath);
    final List<String> classNames = new ArrayList<>();
    for (String path : bootclasspath.split(File.pathSeparator)) {
        if (tool.shouldProcess(path)) {
            try {
                final ZipFile zipFile = new ZipFile(new File(path));
                for (final Enumeration<? extends ZipEntry> entry = zipFile.entries(); entry.hasMoreElements(); ) {
                    final ZipEntry zipEntry = entry.nextElement();
                    String name = zipEntry.getName();
                    if (name.endsWith(".class") && !name.startsWith("META-INF/versions/")) {
                        String className = name.substring(0, name.length() - ".class".length()).replace('/', '.');
                        if (isInNativeImage(className)) {
                            /*
                                 * Native Image is an external tool and does not need to follow the
                                 * Graal invariants.
                                 */
                            continue;
                        }
                        classNames.add(className);
                    }
                }
            } catch (IOException ex) {
                Assert.fail(ex.toString());
            }
        }
    }
    Assert.assertFalse("Could not find graal jars on boot class path: " + bootclasspath, classNames.isEmpty());
    // Allows a subset of methods to be checked through use of a system property
    String property = System.getProperty(CheckGraalInvariants.class.getName() + ".filters");
    String[] filters = property == null ? null : property.split(",");
    OptionValues options = getInitialOptions();
    CompilerThreadFactory factory = new CompilerThreadFactory("CheckInvariantsThread");
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(availableProcessors, availableProcessors, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), factory);
    List<String> errors = Collections.synchronizedList(new ArrayList<>());
    for (Method m : BadUsageWithEquals.class.getDeclaredMethods()) {
        ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
        try (DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER)) {
            StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).method(method).build();
            try (DebugCloseable s = debug.disableIntercept();
                DebugContext.Scope ds = debug.scope("CheckingGraph", graph, method)) {
                graphBuilderSuite.apply(graph, context);
                // update phi stamps
                graph.getNodes().filter(PhiNode.class).forEach(PhiNode::inferStamp);
                checkGraph(context, graph);
                errors.add(String.format("Expected error while checking %s", m));
            } catch (VerificationError e) {
            // expected!
            } catch (Throwable e) {
                errors.add(String.format("Error while checking %s:%n%s", m, printStackTraceToString(e)));
            }
        }
    }
    if (errors.isEmpty()) {
        // Order outer classes before the inner classes
        classNames.sort((String a, String b) -> a.compareTo(b));
        // Initialize classes in single thread to avoid deadlocking issues during initialization
        List<Class<?>> classes = initializeClasses(tool, classNames);
        for (Class<?> c : classes) {
            String className = c.getName();
            executor.execute(() -> {
                try {
                    checkClass(c, metaAccess);
                } catch (Throwable e) {
                    errors.add(String.format("Error while checking %s:%n%s", className, printStackTraceToString(e)));
                }
            });
            for (Method m : c.getDeclaredMethods()) {
                if (Modifier.isNative(m.getModifiers()) || Modifier.isAbstract(m.getModifiers())) {
                // ignore
                } else {
                    String methodName = className + "." + m.getName();
                    if (matches(filters, methodName)) {
                        executor.execute(() -> {
                            try (DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER)) {
                                ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
                                StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).build();
                                try (DebugCloseable s = debug.disableIntercept();
                                    DebugContext.Scope ds = debug.scope("CheckingGraph", graph, method)) {
                                    checkMethod(method);
                                    graphBuilderSuite.apply(graph, context);
                                    // update phi stamps
                                    graph.getNodes().filter(PhiNode.class).forEach(PhiNode::inferStamp);
                                    checkGraph(context, graph);
                                } catch (VerificationError e) {
                                    errors.add(e.getMessage());
                                } catch (LinkageError e) {
                                // suppress linkages errors resulting from eager resolution
                                } catch (BailoutException e) {
                                // Graal bail outs on certain patterns in Java bytecode
                                // (e.g.,
                                // unbalanced monitors introduced by jacoco).
                                } catch (Throwable e) {
                                    try {
                                        tool.handleParsingException(e);
                                    } catch (Throwable t) {
                                        errors.add(String.format("Error while checking %s:%n%s", methodName, printStackTraceToString(e)));
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.HOURS);
        } catch (InterruptedException e1) {
            throw new RuntimeException(e1);
        }
    }
    if (!errors.isEmpty()) {
        StringBuilder msg = new StringBuilder();
        String nl = String.format("%n");
        for (String e : errors) {
            if (msg.length() != 0) {
                msg.append(nl);
            }
            msg.append(e);
        }
        Assert.fail(msg.toString());
    }
}
Also used : OptionValues(org.graalvm.compiler.options.OptionValues) VerificationError(org.graalvm.compiler.phases.VerifyPhase.VerificationError) ZipEntry(java.util.zip.ZipEntry) RuntimeProvider(org.graalvm.compiler.runtime.RuntimeProvider) ArrayList(java.util.ArrayList) Providers(org.graalvm.compiler.phases.util.Providers) VerifyPhase(org.graalvm.compiler.phases.VerifyPhase) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) GraphBuilderPhase(org.graalvm.compiler.java.GraphBuilderPhase) InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) Plugins(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins) InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) PhiNode(org.graalvm.compiler.nodes.PhiNode) GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) IOException(java.io.IOException) Method(java.lang.reflect.Method) JavaMethod(jdk.vm.ci.meta.JavaMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) DebugContext(org.graalvm.compiler.debug.DebugContext) BailoutException(jdk.vm.ci.code.BailoutException) ZipFile(java.util.zip.ZipFile) NodeClass(org.graalvm.compiler.graph.NodeClass) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) DebugCloseable(org.graalvm.compiler.debug.DebugCloseable) PhaseSuite(org.graalvm.compiler.phases.PhaseSuite) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) CompilerThreadFactory(org.graalvm.compiler.core.CompilerThreadFactory) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

File (java.io.File)2 Method (java.lang.reflect.Method)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 MetaAccessProvider (jdk.vm.ci.meta.MetaAccessProvider)2 CompilerThreadFactory (org.graalvm.compiler.core.CompilerThreadFactory)2 OptionValues (org.graalvm.compiler.options.OptionValues)2 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 BailoutException (jdk.vm.ci.code.BailoutException)1 HotSpotCompilationRequest (jdk.vm.ci.hotspot.HotSpotCompilationRequest)1 HotSpotResolvedJavaMethod (jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod)1 HotSpotResolvedObjectType (jdk.vm.ci.hotspot.HotSpotResolvedObjectType)1 ConstantPool (jdk.vm.ci.meta.ConstantPool)1