Search in sources :

Example 1 with GraalTruffleRuntimeListener

use of org.graalvm.compiler.truffle.runtime.GraalTruffleRuntimeListener in project graal by oracle.

the class TruffleBoundaryExceptionsTest method testExceptionOnTruffleBoundaryDoesNotDeop.

@Test
public void testExceptionOnTruffleBoundaryDoesNotDeop() {
    final int compilationThreshold = TruffleCompilerOptions.getValue(TruffleCompilationThreshold);
    class DeoptCountingExceptionOverBoundaryRootNode extends RootNode {

        protected DeoptCountingExceptionOverBoundaryRootNode() {
            super(null);
        }

        int deopCounter = 0;

        int catchCounter = 0;

        int interpretCount = 0;

        @Override
        public Object execute(VirtualFrame frame) {
            boolean startedCompiled = CompilerDirectives.inCompiledCode();
            if (!startedCompiled) {
                interpretCount++;
            }
            try {
                throwExceptionBoundary();
            } catch (RuntimeException e) {
                catchCounter++;
            }
            if (startedCompiled && CompilerDirectives.inInterpreter()) {
                deopCounter++;
            }
            return null;
        }

        @CompilerDirectives.TruffleBoundary
        public void throwExceptionBoundary() {
            throw new RuntimeException();
        }
    }
    final int[] compilationCount = { 0 };
    GraalTruffleRuntimeListener listener = new GraalTruffleRuntimeListener() {

        @Override
        public void onCompilationStarted(OptimizedCallTarget target) {
            compilationCount[0]++;
        }
    };
    final OptimizedCallTarget outerTarget = (OptimizedCallTarget) runtime.createCallTarget(new DeoptCountingExceptionOverBoundaryRootNode());
    for (int i = 0; i < compilationThreshold; i++) {
        outerTarget.call();
    }
    assertCompiled(outerTarget);
    runtime.addListener(listener);
    final int execCount = 10;
    for (int i = 0; i < execCount; i++) {
        outerTarget.call();
    }
    final int totalExecutions = compilationThreshold + execCount;
    int catchCount = ((DeoptCountingExceptionOverBoundaryRootNode) outerTarget.getRootNode()).catchCounter;
    Assert.assertEquals("Incorrect number of catch block executions", totalExecutions, catchCount);
    int interpretCount = ((DeoptCountingExceptionOverBoundaryRootNode) outerTarget.getRootNode()).interpretCount;
    int deopCount = ((DeoptCountingExceptionOverBoundaryRootNode) outerTarget.getRootNode()).deopCounter;
    Assert.assertEquals("Incorrect number of deops detected!", totalExecutions - interpretCount, deopCount);
    Assert.assertEquals("Compilation happened!", 0, compilationCount[0]);
    runtime.removeListener(listener);
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) GraalTruffleRuntimeListener(org.graalvm.compiler.truffle.runtime.GraalTruffleRuntimeListener) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) Test(org.junit.Test)

Aggregations

VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 GraalTruffleRuntimeListener (org.graalvm.compiler.truffle.runtime.GraalTruffleRuntimeListener)1 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)1 Test (org.junit.Test)1