Search in sources :

Example 1 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationListenerTest method testExceptionImpl.

private void testExceptionImpl(Runnable body, boolean throwOnEnter, boolean throwOnLeave) {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    ThreadsActivationListener listener = new ThreadsActivationListener() {

        @Override
        public void onEnterThread(TruffleContext c) {
            if (throwOnEnter) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                throw new RuntimeException();
            }
        }

        @Override
        public void onLeaveThread(TruffleContext c) {
            if (throwOnLeave) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                throw new RuntimeException();
            }
        }
    };
    EventBinding<? extends ThreadsActivationListener> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(listener);
    try {
        body.run();
    } finally {
        binding.dispose();
    }
}
Also used : ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage)

Example 2 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationListenerTest method testInternalContext.

@Test
public void testInternalContext() {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    Context c0 = this.context;
    TruffleContext tc0 = this.languageEnv.getContext();
    c0.enter();
    TruffleContext ic0 = this.languageEnv.newContextBuilder().build();
    TruffleContext ic0CreatorHandle = ic0;
    Object prev = ic0.enter(null);
    // look language handle on the context it is not the same as
    // the creator handle. The creator handle can be closed.
    ic0 = LanguageContext.get(null).getEnv().getContext();
    ic0.leave(null, prev);
    c0.leave();
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            entered.add(c);
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            left.add(c);
        }
    });
    assertList(entered);
    assertList(left);
    assertSame(tc0, ic0.getParent());
    assertList(entered);
    assertList(left);
    prev = ic0.enter(null);
    assertList(entered, ic0);
    assertList(left);
    ic0.leave(null, prev);
    assertList(entered, ic0);
    assertList(left, ic0);
    prev = tc0.enter(null);
    assertList(entered, ic0, tc0);
    assertList(left, ic0);
    Object prev1 = ic0.enter(null);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0);
    ic0.leave(null, prev1);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0);
    tc0.leave(null, prev);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0, tc0);
    binding.dispose();
    prev = tc0.enter(null);
    prev1 = ic0.enter(null);
    ic0.leave(null, prev1);
    tc0.leave(null, prev);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0, tc0);
    ic0CreatorHandle.close();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 3 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationListenerTest method testSingleContext.

@Test
public void testSingleContext() {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            entered.add(c);
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            left.add(c);
        }
    });
    assertList(entered);
    assertList(left);
    context.enter();
    TruffleContext to = languageEnv.getContext();
    assertList(entered, to);
    assertList(left);
    context.leave();
    assertList(entered, to);
    assertList(left, to);
    context.enter();
    assertList(entered, to, to);
    assertList(left, to);
    context.enter();
    assertList(entered, to, to, to);
    assertList(left, to);
    context.leave();
    assertList(entered, to, to, to);
    assertList(left, to, to);
    context.leave();
    assertList(entered, to, to, to);
    assertList(left, to, to, to);
    context.eval(ProxyLanguage.ID, "");
    assertList(entered, to, to, to, to);
    assertList(left, to, to, to, to);
    binding.dispose();
    context.enter();
    context.leave();
    assertList(entered, to, to, to, to);
    assertList(left, to, to, to, to);
}
Also used : ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) CallTarget(com.oracle.truffle.api.CallTarget) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 4 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationCompilationTest method testThreadActivationCompilation.

@Test
public void testThreadActivationCompilation() {
    setupEnv(Context.newBuilder().allowExperimentalOptions(true).option("engine.CompileImmediately", "false").option("engine.BackgroundCompilation", "false").build());
    AtomicReference<Boolean> compiledEnter = new AtomicReference<>();
    AtomicReference<Boolean> compiledLeave = new AtomicReference<>();
    compiledEnter.set(Boolean.FALSE);
    compiledLeave.set(Boolean.FALSE);
    Assumption singleContext = Truffle.getRuntime().createAssumption();
    instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @Override
        public void onLeaveThread(TruffleContext c) {
            if (singleContext.isValid()) {
                CompilerAsserts.partialEvaluationConstant(c);
                CompilerAsserts.partialEvaluationConstant(this);
            }
            if (CompilerDirectives.inCompiledCode()) {
                compiledLeave.set(Boolean.TRUE);
            }
        }

        @Override
        public void onEnterThread(TruffleContext c) {
            if (singleContext.isValid()) {
                CompilerAsserts.partialEvaluationConstant(c);
                CompilerAsserts.partialEvaluationConstant(this);
            }
            if (CompilerDirectives.inCompiledCode()) {
                compiledEnter.set(Boolean.TRUE);
            }
        }
    });
    context.enter();
    compiledEnter.set(Boolean.FALSE);
    compiledLeave.set(Boolean.FALSE);
    OptimizedCallTarget target = (OptimizedCallTarget) new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            TruffleContext tc = (TruffleContext) frame.getArguments()[0];
            Object prev = tc.enter(this);
            if (CompilerDirectives.inCompiledCode()) {
                if (!compiledEnter.get()) {
                    CompilerDirectives.shouldNotReachHere();
                }
            }
            tc.leave(this, prev);
            if (CompilerDirectives.inCompiledCode()) {
                if (!compiledLeave.get()) {
                    CompilerDirectives.shouldNotReachHere();
                }
            }
            return null;
        }
    }.getCallTarget();
    TruffleContext tc = LanguageContext.get(null).getEnv().getContext();
    singleContext.invalidate();
    target.call(tc);
    target.compile(true);
    assertTrue(target.isValidLastTier());
    target.call(tc);
    assertTrue(target.isValidLastTier());
    assertTrue(compiledEnter.get());
    assertTrue(compiledLeave.get());
    context.leave();
    context.close();
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleContext(com.oracle.truffle.api.TruffleContext) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) AtomicReference(java.util.concurrent.atomic.AtomicReference) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 5 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class TruffleContextTest method testCancelledAndResourceExhausted.

@Test
public void testCancelledAndResourceExhausted() throws InterruptedException {
    setupEnv();
    AtomicReference<Throwable> error = new AtomicReference<>();
    Thread t = new Thread(() -> {
        try {
            context.eval(InstrumentationTestLanguage.ID, "LOOP(infinity, STATEMENT)");
        } catch (Throwable e) {
            error.set(e);
        }
    });
    // avoid need for multi-threading
    context.leave();
    AtomicReference<TruffleContext> enter = new AtomicReference<>();
    Semaphore waitUntilEntered = new Semaphore(0);
    instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext tc) {
            enter.set(tc);
            waitUntilEntered.release();
        }

        public void onLeaveThread(TruffleContext tc) {
        }
    });
    t.start();
    if (!waitUntilEntered.tryAcquire(10000, TimeUnit.MILLISECONDS)) {
        throw new AssertionError(error.get());
    }
    TruffleContext tc = enter.get();
    tc.closeResourceExhausted(null, "testError");
    t.join();
    assertNotNull(error.get());
    assertTrue(error.get().toString(), error.get() instanceof PolyglotException);
    PolyglotException e = (PolyglotException) error.get();
    assertEquals("testError", e.getMessage());
    assertTrue(e.isCancelled());
    assertTrue(e.isResourceExhausted());
}
Also used : ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) TruffleContext(com.oracle.truffle.api.TruffleContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) PolyglotException(org.graalvm.polyglot.PolyglotException) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) LanguageSPIOrderTest(com.oracle.truffle.api.test.polyglot.LanguageSPIOrderTest) Test(org.junit.Test)

Aggregations

TruffleContext (com.oracle.truffle.api.TruffleContext)7 ThreadsActivationListener (com.oracle.truffle.api.instrumentation.ThreadsActivationListener)7 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)6 PolyglotException (org.graalvm.polyglot.PolyglotException)6 Test (org.junit.Test)6 CallTarget (com.oracle.truffle.api.CallTarget)5 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)5 ArrayList (java.util.ArrayList)4 LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Context (org.graalvm.polyglot.Context)2 Assumption (com.oracle.truffle.api.Assumption)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 LanguageSPIOrderTest (com.oracle.truffle.api.test.polyglot.LanguageSPIOrderTest)1 Semaphore (java.util.concurrent.Semaphore)1 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)1 Engine (org.graalvm.polyglot.Engine)1