Search in sources :

Example 31 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testLanguageInitializedOnlyAppend.

@Test
public void testLanguageInitializedOnlyAppend() throws Exception {
    Source initSource = Source.create(InstrumentationTestLanguage.ID, "STATEMENT(EXPRESSION, EXPRESSION)");
    setupEngine(initSource, true);
    Instrument instrument = engine.getInstruments().get("testLangInitialized");
    // Events during language initialization phase are excluded,
    // but events from the same nodes used for initialization are appended:
    TestLangInitialized.initializationEvents = false;
    TestLangInitialized service = instrument.lookup(TestLangInitialized.class);
    run("LOOP(2, STATEMENT())");
    assertEquals("[FunctionRootNode, true, LoopNode, true, StatementNode, true, StatementNode, true, FunctionRootNode, true, StatementNode, true, ExpressionNode, true, ExpressionNode, true]", service.getEnteredNodes());
    engine.close();
    engine = null;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 32 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testLanguageInitializedOrNot.

@Test
public void testLanguageInitializedOrNot() throws Exception {
    Source initSource = Source.create(InstrumentationTestLanguage.ID, "STATEMENT(EXPRESSION, EXPRESSION)");
    setupEngine(initSource, false);
    Instrument instrument = engine.getInstruments().get("testLangInitialized");
    // Events during language initialization phase are included:
    TestLangInitialized.initializationEvents = true;
    TestLangInitialized service = instrument.lookup(TestLangInitialized.class);
    run("LOOP(2, STATEMENT())");
    assertEquals("[FunctionRootNode, false, StatementNode, false, ExpressionNode, false, ExpressionNode, false, FunctionRootNode, true, LoopNode, true, StatementNode, true, StatementNode, true]", service.getEnteredNodes());
    engine.close();
    engine = null;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 33 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class SourceListenerTest method testExecuteSourceNoRootSection.

@Test
public void testExecuteSourceNoRootSection() throws Exception {
    Instrument instrument = engine.getInstruments().get("testLoadExecuteSource");
    TestLoadExecuteSource impl = instrument.lookup(TestLoadExecuteSource.class);
    impl.attachExecute();
    testNoRootSectionImpl(impl);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 34 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class SourceListenerTest method testAllowOnlySourceQueries.

@Test
public void testAllowOnlySourceQueries() throws IOException {
    Instrument instrument = engine.getInstruments().get("testAllowOnlySourceQueries");
    assureEnabled(instrument);
    Source source = lines("");
    run(source);
    TestAllowOnlySourceQueries impl = instrument.lookup(TestAllowOnlySourceQueries.class);
    Assert.assertTrue(impl.success);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 35 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class ThreadsEventsTest method testGetStartedThreads.

@Test
public void testGetStartedThreads() throws Exception {
    try {
        TestThreadsInstrument.includeStartedThreads = true;
        final List<ThreadEvent> events;
        try (Context context = Context.newBuilder().allowCreateThread(true).build()) {
            context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(DEFINE(f1, EXPRESSION), SPAWN(f1), JOIN())"));
            Instrument testBlockOnStatementsInstrument = context.getEngine().getInstruments().get("testBlockOnStatementsInstrument");
            TestBlockOnStatementsInstrument testBlock = testBlockOnStatementsInstrument.lookup(TestBlockOnStatementsInstrument.class);
            CountDownLatch latch = new CountDownLatch(1);
            testBlock.runOnBlock = new Runnable() {

                @Override
                public void run() {
                    latch.countDown();
                }
            };
            context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo, STATEMENT), SPAWN(foo))"));
            latch.await();
            Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
            TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
            events = test.events;
            assertEquals(events.toString(), 2, events.size());
            assertTrue(events.get(0).isNew);
            assertNotNull(events.get(0).context);
            assertTrue(events.get(1).isNew);
            assertEquals(events.get(0).context, events.get(1).context);
            assertNotEquals(events.get(0).thread, events.get(1).thread);
            testBlock.blockOnStatements.set(false);
            synchronized (testBlock.blockOnStatements) {
                testBlock.blockOnStatements.notifyAll();
            }
            context.eval(Source.create(InstrumentationTestLanguage.ID, "JOIN()"));
            assertEquals(3, events.size());
            assertFalse(events.get(2).isNew);
            assertTrue(events.get(0).thread == events.get(2).thread || events.get(1).thread == events.get(2).thread);
        }
        assertEquals(4, events.size());
    } finally {
        TestThreadsInstrument.includeStartedThreads = false;
    }
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2