Search in sources :

Example 1 with MetricKey

use of org.graalvm.compiler.debug.MetricKey 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, LibGraalParams libgraal) throws IOException {
    final String[] entries = classPath.split(File.pathSeparator);
    Map<Thread, StackTraceElement[]> initialThreads = Thread.getAllStackTraces();
    if (libgraal == null) {
        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);
            task.runCompilation(compilerOptions);
        } catch (NoSuchMethodException | SecurityException e1) {
            printStackTrace(e1);
        }
    }
    int startAtClass = startAt;
    int stopAtClass = stopAt;
    if (startAtClass >= stopAtClass) {
        throw new IllegalArgumentException(String.format("StartAt (%d) must be less than StopAt (%d)", startAtClass, stopAtClass));
    }
    int startAtCompile = Options.StartAtCompile.getValue(harnessOptions);
    int stopAtCompile = Options.StopAtCompile.getValue(harnessOptions);
    if (startAtCompile >= stopAtCompile) {
        throw new IllegalArgumentException(String.format("StartAtCompile (%d) must be less than StopAtCompile (%d)", startAtCompile, stopAtCompile));
    }
    int classStep = 1;
    if (maxClasses != Integer.MAX_VALUE) {
        int totalClassFileCount = 0;
        for (String entry : entries) {
            try (ClassPathEntry cpe = openClassPathEntry(entry)) {
                if (cpe != null) {
                    totalClassFileCount += cpe.getClassNames().size();
                }
            }
        }
        int lastClassFile = totalClassFileCount - 1;
        startAtClass = Math.min(startAt, lastClassFile);
        stopAtClass = Math.min(stopAt, lastClassFile);
        int range = stopAtClass - startAtClass + 1;
        if (maxClasses < range) {
            classStep = range / maxClasses;
        }
    }
    TTY.println("CompileTheWorld : Gathering compilations ...");
    Map<HotSpotResolvedJavaMethod, Integer> toBeCompiled = gatherCompilations(entries, startAtClass, stopAtClass, classStep);
    /*
         * 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(harnessOptions)) {
        threadCount = Options.Threads.getValue(harnessOptions);
        if (threadCount == 0) {
            threadCount = Runtime.getRuntime().availableProcessors();
        }
        TTY.println("CompileTheWorld : Using %d threads", threadCount);
    }
    threadPool = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new CTWThreadFactory(libgraal));
    TTY.println("CompileTheWorld : Starting compilations ...");
    long start = System.nanoTime();
    int compilationNum = 0;
    int allCompiles = Math.min(toBeCompiled.size(), stopAtCompile) - Math.max(0, startAtCompile);
    int maxCompiles = Options.MaxCompiles.getValue(harnessOptions);
    float selector = Math.max(0, startAtCompile);
    float selectorStep = maxCompiles < allCompiles ? (float) allCompiles / maxCompiles : 1.0f;
    int repeat = Options.Repeat.getValue(harnessOptions);
    long taskCount = 0;
    for (Map.Entry<HotSpotResolvedJavaMethod, Integer> e : toBeCompiled.entrySet()) {
        if (compilationNum >= startAtCompile && compilationNum < stopAtCompile) {
            if (Math.round(selector) == compilationNum) {
                for (int i = 0; i < repeat; i++) {
                    taskCount++;
                    threadPool.submit(new Runnable() {

                        @Override
                        public void run() {
                            compileMethod(e.getKey(), e.getValue(), libgraal);
                        }
                    });
                }
                selector += selectorStep;
            }
        }
        compilationNum++;
    }
    int wakeups = 0;
    long lastCompletedTaskCount = 0;
    int statsInterval = Options.StatsInterval.getValue(harnessOptions);
    long completedTaskCount;
    do {
        completedTaskCount = threadPool.getCompletedTaskCount();
        if (completedTaskCount != 0 && (wakeups % statsInterval == 0 || completedTaskCount == taskCount)) {
            long compilationsInInterval = completedTaskCount - lastCompletedTaskCount;
            double rate = (double) compilationsInInterval / statsInterval;
            long percent = completedTaskCount * 100 / taskCount;
            TTY.println("CompileTheWorld : [%2d%%, %.1f compiles/s] %d of %d compilations completed, %d in last interval", percent, rate, completedTaskCount, taskCount, compilationsInInterval);
            if (libgraal != null) {
                armPrintMetrics();
            }
            lastCompletedTaskCount = completedTaskCount;
        }
        try {
            threadPool.awaitTermination(1, TimeUnit.SECONDS);
            wakeups++;
        } catch (InterruptedException e) {
        }
    } while (completedTaskCount != taskCount);
    threadPool.shutdown();
    threadPool = null;
    long elapsedTime = System.nanoTime() - start;
    println();
    int compiledClasses = classFileCounter > startAtClass ? classFileCounter - startAtClass : 0;
    int compiledBytecodes = compileTimes.keySet().stream().collect(Collectors.summingInt(ResolvedJavaMethod::getCodeSize));
    int compiledMethods = compileTimes.size();
    long elapsedTimeSeconds = nanoToMillis(compileTime.get()) / 1_000;
    double rateInMethods = (double) compiledMethods / elapsedTimeSeconds;
    double rateInBytecodes = (double) compiledBytecodes / elapsedTimeSeconds;
    TTY.println("CompileTheWorld : ======================== Done ======================");
    TTY.println("CompileTheWorld :         Compiled classes: %,d", compiledClasses);
    TTY.println("CompileTheWorld :         Compiled methods: %,d [%,d bytecodes]", compiledMethods, compiledBytecodes);
    TTY.println("CompileTheWorld :             Elapsed time: %,d ms", nanoToMillis(elapsedTime));
    TTY.println("CompileTheWorld :             Compile time: %,d ms", nanoToMillis(compileTime.get()));
    TTY.println("CompileTheWorld :  Compilation rate/thread: %,.1f methods/sec, %,.0f bytecodes/sec", rateInMethods, rateInBytecodes);
    TTY.println("CompileTheWorld : HotSpot heap memory used: %,.3f MB", (double) memoryUsed.get() / 1_000_000);
    TTY.println("CompileTheWorld :     Huge methods skipped: %,d", hugeMethods.size());
    int limit = Options.MetricsReportLimit.getValue(harnessOptions);
    if (limit > 0) {
        TTY.println("Longest compile times:");
        compileTimes.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())).limit(limit).forEach(e -> {
            long time = nanoToMillis(e.getValue());
            ResolvedJavaMethod method = e.getKey();
            TTY.println("  %,10d ms   %s [bytecodes: %d]", time, method.format("%H.%n(%p)"), method.getCodeSize());
        });
        TTY.println("Largest methods skipped due to bytecode size exceeding HugeMethodLimit (%d):", getHugeMethodLimit(compiler.getGraalRuntime().getVMConfig()));
        hugeMethods.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())).limit(limit).forEach(e -> {
            ResolvedJavaMethod method = e.getKey();
            TTY.println("  %,10d      %s", e.getValue(), method.format("%H.%n(%p)"), method.getCodeSize());
        });
    }
    GlobalMetrics metricValues = ((HotSpotGraalRuntime) compiler.getGraalRuntime()).getMetricValues();
    EconomicMap<MetricKey, Long> map = metricValues.asKeyValueMap();
    Long compiledAndInstalledBytecodes = map.get(CompiledAndInstalledBytecodes);
    Long compilationTime = map.get(CompilationTime);
    if (compiledAndInstalledBytecodes != null && compilationTime != null) {
        TTY.println("CompileTheWorld : Aggregate compile speed %d bytecodes per second (%d / %d)", (int) (compiledAndInstalledBytecodes / (compilationTime / 1000000000.0)), compiledAndInstalledBytecodes, compilationTime);
    }
    metricValues.print(compilerOptions);
    metricValues.clear();
    // 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 : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MetricKey(org.graalvm.compiler.debug.MetricKey) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) GlobalMetrics(org.graalvm.compiler.debug.GlobalMetrics) Print(org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Print) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask) AtomicLong(java.util.concurrent.atomic.AtomicLong) HotSpotGraalRuntime(org.graalvm.compiler.hotspot.HotSpotGraalRuntime) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) EconomicMap(org.graalvm.collections.EconomicMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 2 with MetricKey

use of org.graalvm.compiler.debug.MetricKey in project graal by oracle.

the class BytecodeInterpreterPartialEvaluationTest method manyIfsProgram.

@Test
public void manyIfsProgram() {
    byte[] bytecodes = new byte[] { /* 0: */
    Bytecode.CONST, /* 1: */
    40, /* 2: */
    Bytecode.CONST, /* 3: */
    1, /* 4: */
    Bytecode.IFZERO, /* 5: */
    8, /* 6: */
    Bytecode.CONST, /* 7: */
    1, /* 8: */
    Bytecode.IFZERO, /* 9: */
    12, /* 10: */
    Bytecode.CONST, /* 11: */
    1, /* 12: */
    Bytecode.IFZERO, /* 13: */
    16, /* 14: */
    Bytecode.CONST, /* 15: */
    1, /* 16: */
    Bytecode.IFZERO, /* 17: */
    20, /* 18: */
    Bytecode.CONST, /* 19: */
    1, /* 20: */
    Bytecode.IFZERO, /* 21: */
    24, /* 22: */
    Bytecode.CONST, /* 23: */
    1, /* 24: */
    Bytecode.IFZERO, /* 25: */
    28, /* 26: */
    Bytecode.CONST, /* 27: */
    1, /* 28: */
    Bytecode.IFZERO, /* 29: */
    32, /* 30: */
    Bytecode.CONST, /* 31: */
    1, /* 32: */
    Bytecode.IFZERO, /* 33: */
    36, /* 34: */
    Bytecode.CONST, /* 35: */
    1, /* 36: */
    Bytecode.IFZERO, /* 37: */
    40, /* 38: */
    Bytecode.CONST, /* 39: */
    1, /* 40: */
    Bytecode.IFZERO, /* 41: */
    44, /* 42: */
    Bytecode.CONST, /* 43: */
    42, /* 44: */
    Bytecode.RETURN };
    long[] times = new long[5];
    String[] topPhases = new String[times.length];
    for (int i = 0; i < times.length; i++) {
        long start = System.currentTimeMillis();
        assertPartialEvalEqualsAndRunsCorrect(new Program("manyIfsProgram", bytecodes, 0, 3));
        long duration = System.currentTimeMillis() - start;
        times[i] = duration;
        Map<MetricKey, Long> metrics = lastDebug.getMetricsSnapshot();
        List<Map.Entry<MetricKey, Long>> entries = new ArrayList<>(metrics.entrySet());
        entries.sort((o1, o2) -> (o2.getValue().compareTo(o1.getValue())));
        int printed = 0;
        Formatter buf = new Formatter();
        for (Map.Entry<MetricKey, Long> e : entries) {
            if (printed++ > 20) {
                break;
            }
            MetricKey key = e.getKey();
            if (key instanceof TimerKey) {
                TimerKey timer = (TimerKey) key;
                long value = e.getValue();
                long ms = timer.getTimeUnit().toMillis(value);
                buf.format("  %s ms\t%s%n", ms, key.getName());
            }
        }
        topPhases[i] = buf.toString();
    }
    int limit = 15000;
    for (int i = 0; i < times.length; i++) {
        if (times[i] > limit) {
            Formatter msg = new Formatter();
            msg.format("manyIfsProgram iteration %d took %d ms which is longer than the limit of %d ms%n", i, times[i], limit);
            msg.format("%nDetailed info for each iteration%n");
            for (int j = 0; j < times.length; j++) {
                msg.format("%nIteration %d took %d ms%n", i, times[i]);
                msg.format("Top phase times in iteration %d:%n%s%n", i, topPhases[i]);
            }
            throw new AssertionError(msg.toString());
        }
    }
    long maxDuration = 0L;
    if (maxDuration > limit) {
        throw new AssertionError("manyIfsProgram took " + maxDuration + " ms which is longer than the limit of " + limit + " ms");
    }
}
Also used : Formatter(java.util.Formatter) ArrayList(java.util.ArrayList) TimerKey(org.graalvm.compiler.debug.TimerKey) MetricKey(org.graalvm.compiler.debug.MetricKey) Map(java.util.Map) Test(org.junit.Test)

Example 3 with MetricKey

use of org.graalvm.compiler.debug.MetricKey in project graal by oracle.

the class GraalObjectReplacer method apply.

@Override
public Object apply(Object source) {
    if (source == null) {
        return null;
    }
    Object dest = source;
    if (source instanceof RelocatedPointer) {
        return dest;
    }
    if (source instanceof SnippetResolvedJavaMethod || source instanceof SnippetResolvedJavaType) {
        return source;
    }
    if (source instanceof MetaAccessProvider) {
        dest = providerReplacements.getMetaAccessProvider();
    } else if (source instanceof HotSpotJVMCIRuntime) {
        throw new UnsupportedFeatureException("HotSpotJVMCIRuntime should not appear in the image: " + source);
    } else if (source instanceof GraalHotSpotVMConfig) {
        throw new UnsupportedFeatureException("GraalHotSpotVMConfig should not appear in the image: " + source);
    } else if (source instanceof HotSpotBackendFactory) {
        HotSpotBackendFactory factory = (HotSpotBackendFactory) source;
        Architecture hostArch = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getTarget().arch;
        if (!factory.getArchitecture().equals(hostArch.getClass())) {
            throw new UnsupportedFeatureException("Non-host archtecture HotSpotBackendFactory should not appear in the image: " + source);
        }
    } else if (source instanceof GraalRuntime) {
        dest = sGraalRuntime;
    } else if (source instanceof AnalysisConstantReflectionProvider) {
        dest = providerReplacements.getConstantReflectionProvider();
    } else if (source instanceof AnalysisConstantFieldProvider) {
        dest = providerReplacements.getConstantFieldProvider();
    } else if (source instanceof ForeignCallsProvider) {
        dest = providerReplacements.getForeignCallsProvider();
    } else if (source instanceof SnippetReflectionProvider) {
        dest = providerReplacements.getSnippetReflectionProvider();
    } else if (source instanceof MetricKey) {
        /* Ensure lazily initialized name fields are computed. */
        ((MetricKey) source).getName();
    } else if (source instanceof NodeClass) {
        /* Ensure lazily initialized shortName field is computed. */
        ((NodeClass<?>) source).shortName();
    } else if (source instanceof HotSpotResolvedJavaMethod) {
        throw new UnsupportedFeatureException(source.toString());
    } else if (source instanceof HotSpotResolvedJavaField) {
        throw new UnsupportedFeatureException(source.toString());
    } else if (source instanceof HotSpotResolvedJavaType) {
        throw new UnsupportedFeatureException(source.toString());
    } else if (source instanceof HotSpotSignature) {
        throw new UnsupportedFeatureException(source.toString());
    } else if (source instanceof HotSpotObjectConstant) {
        throw new UnsupportedFeatureException(source.toString());
    } else if (source instanceof ResolvedJavaMethod && !(source instanceof SubstrateMethod)) {
        dest = createMethod((ResolvedJavaMethod) source);
    } else if (source instanceof ResolvedJavaField && !(source instanceof SubstrateField)) {
        dest = createField((ResolvedJavaField) source);
    } else if (source instanceof ResolvedJavaType && !(source instanceof SubstrateType)) {
        dest = createType((ResolvedJavaType) source);
    } else if (source instanceof FieldLocationIdentity && !(source instanceof SubstrateFieldLocationIdentity)) {
        dest = createFieldLocationIdentity((FieldLocationIdentity) source);
    }
    assert dest != null;
    String className = dest.getClass().getName();
    assert SubstrateUtil.isBuildingLibgraal() || !className.contains(".hotspot.") || className.contains(".svm.jtt.hotspot.") : "HotSpot object in image " + className;
    assert !className.contains(".analysis.meta.") : "Analysis meta object in image " + className;
    assert !className.contains(".hosted.meta.") : "Hosted meta object in image " + className;
    assert !SubstrateUtil.isBuildingLibgraal() || !className.contains(".svm.hosted.snippets.") : "Hosted snippet object in image " + className;
    return dest;
}
Also used : HotSpotBackendFactory(org.graalvm.compiler.hotspot.HotSpotBackendFactory) ForeignCallsProvider(org.graalvm.compiler.core.common.spi.ForeignCallsProvider) AnalysisConstantReflectionProvider(com.oracle.svm.hosted.ameta.AnalysisConstantReflectionProvider) HotSpotJVMCIRuntime(jdk.vm.ci.hotspot.HotSpotJVMCIRuntime) RelocatedPointer(org.graalvm.nativeimage.c.function.RelocatedPointer) SubstrateField(com.oracle.svm.graal.meta.SubstrateField) HotSpotObjectConstant(jdk.vm.ci.hotspot.HotSpotObjectConstant) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) SnippetResolvedJavaType(org.graalvm.compiler.hotspot.SnippetResolvedJavaType) HotSpotResolvedJavaType(jdk.vm.ci.hotspot.HotSpotResolvedJavaType) ResolvedJavaField(jdk.vm.ci.meta.ResolvedJavaField) HotSpotResolvedJavaField(jdk.vm.ci.hotspot.HotSpotResolvedJavaField) SnippetResolvedJavaType(org.graalvm.compiler.hotspot.SnippetResolvedJavaType) NodeClass(org.graalvm.compiler.graph.NodeClass) SnippetReflectionProvider(org.graalvm.compiler.api.replacements.SnippetReflectionProvider) MetricKey(org.graalvm.compiler.debug.MetricKey) SnippetResolvedJavaMethod(org.graalvm.compiler.hotspot.SnippetResolvedJavaMethod) HotSpotSignature(jdk.vm.ci.hotspot.HotSpotSignature) GraalHotSpotVMConfig(org.graalvm.compiler.hotspot.GraalHotSpotVMConfig) SubstrateType(com.oracle.svm.graal.meta.SubstrateType) SubstrateMethod(com.oracle.svm.graal.meta.SubstrateMethod) AnalysisConstantFieldProvider(com.oracle.svm.hosted.ameta.AnalysisConstantFieldProvider) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) UnsupportedFeatureException(com.oracle.graal.pointsto.constraints.UnsupportedFeatureException) Architecture(jdk.vm.ci.code.Architecture) HotSpotResolvedJavaField(jdk.vm.ci.hotspot.HotSpotResolvedJavaField) GraalRuntime(org.graalvm.compiler.api.runtime.GraalRuntime) SubstrateGraalRuntime(com.oracle.svm.graal.SubstrateGraalRuntime) FieldLocationIdentity(org.graalvm.compiler.nodes.FieldLocationIdentity) SubstrateFieldLocationIdentity(com.oracle.svm.core.graal.nodes.SubstrateFieldLocationIdentity) HotSpotResolvedJavaType(jdk.vm.ci.hotspot.HotSpotResolvedJavaType) SubstrateFieldLocationIdentity(com.oracle.svm.core.graal.nodes.SubstrateFieldLocationIdentity) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) SnippetResolvedJavaMethod(org.graalvm.compiler.hotspot.SnippetResolvedJavaMethod) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

MetricKey (org.graalvm.compiler.debug.MetricKey)3 Map (java.util.Map)2 HotSpotResolvedJavaMethod (jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod)2 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)2 UnsupportedFeatureException (com.oracle.graal.pointsto.constraints.UnsupportedFeatureException)1 SubstrateFieldLocationIdentity (com.oracle.svm.core.graal.nodes.SubstrateFieldLocationIdentity)1 SubstrateGraalRuntime (com.oracle.svm.graal.SubstrateGraalRuntime)1 SubstrateField (com.oracle.svm.graal.meta.SubstrateField)1 SubstrateMethod (com.oracle.svm.graal.meta.SubstrateMethod)1 SubstrateType (com.oracle.svm.graal.meta.SubstrateType)1 AnalysisConstantFieldProvider (com.oracle.svm.hosted.ameta.AnalysisConstantFieldProvider)1 AnalysisConstantReflectionProvider (com.oracle.svm.hosted.ameta.AnalysisConstantReflectionProvider)1 ArrayList (java.util.ArrayList)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1