Search in sources :

Example 36 with Instrument

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

the class ThreadsEventsTest method testSpawnManyThread.

@Test
public void testSpawnManyThread() {
    final int numThreads = 100;
    final List<ThreadEvent> events;
    try (Context context = Context.newBuilder().allowCreateThread(true).build()) {
        Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
        TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
        events = test.events;
        context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo, STATEMENT), LOOP(" + numThreads + ", SPAWN(foo)), JOIN())"));
    }
    assertEquals(2 + 2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(1 + numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(1 + numThreads, startEvents.size());
    TruffleContext tcontext = events.get(0).context;
    for (ThreadEvent event : events) {
        assertEquals(tcontext, event.context);
    }
    events.clear();
}
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) ArrayList(java.util.ArrayList) TruffleContext(com.oracle.truffle.api.TruffleContext) Test(org.junit.Test)

Example 37 with Instrument

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

the class ThreadsEventsTest method testContextThreads.

@Test
public void testContextThreads() throws Exception {
    Engine engine = Engine.create();
    Instrument testThreadsInstrument = engine.getInstruments().get("testThreadsInstrument");
    TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
    final List<ThreadEvent> events = test.events;
    Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                try (Context context = Context.newBuilder().engine(engine).build()) {
                    context.eval(source);
                }
            }
        };
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].join();
    }
    assertEquals(2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(numThreads, startEvents.size());
    // Verify that we got all threads in the events:
    Set<Thread> allThreads = new HashSet<>(Arrays.asList(threads));
    for (int i = 0; i < numThreads; i++) {
        assertTrue(allThreads.remove(startEvents.get(i).thread));
    }
    assertTrue(allThreads.toString(), allThreads.isEmpty());
    // Verify that we got 'numThreads' number of contexts:
    Set<TruffleContext> distinctContexts = new HashSet<>();
    for (ThreadEvent event : events) {
        distinctContexts.add(event.context);
    }
    assertEquals(numThreads, distinctContexts.size());
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) TruffleContext(com.oracle.truffle.api.TruffleContext) Source(org.graalvm.polyglot.Source) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Engine(org.graalvm.polyglot.Engine) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 38 with Instrument

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

the class ThreadsEventsTest method testSingleThread.

@Test
public void testSingleThread() {
    final List<ThreadEvent> events;
    try (Context context = Context.create()) {
        Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
        TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
        events = test.events;
        context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
        assertEquals(1, events.size());
        assertTrue(events.get(0).isNew);
        assertEquals(Thread.currentThread(), events.get(0).thread);
        assertNotNull(events.get(0).context);
    }
    assertEquals(2, events.size());
    assertFalse(events.get(1).isNew);
    assertEquals(Thread.currentThread(), events.get(1).thread);
    assertEquals(events.get(0).context, events.get(1).context);
    events.clear();
}
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) Test(org.junit.Test)

Example 39 with Instrument

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

the class DebuggerExampleTest method setupDebugger.

@Before
public void setupDebugger() throws IOException {
    // BEGIN: DebuggerExampleTest
    Instrument instrument = engine.getInstruments().get(DebuggerExample.ID);
    assert !isCreated(instrument) : "Not enabled yet";
    debugger = instrument.lookup(DebuggerController.class);
    assert isCreated(instrument) : "Got enabled";
    assert debugger != null : "We can control the debugger";
    // END: DebuggerExampleTest
    assertTrue("Enabled by requesting registered services class", isCreated(instrument));
    assertNotNull("Debugger interface found", debugger);
    DebuggerExample itself = instrument.lookup(DebuggerExample.class);
    assertNull("Debugger instrument itself isn't found", itself);
    TruffleInstrument instr = instrument.lookup(TruffleInstrument.class);
    assertNull("Instrument itself isn't found", instr);
    // ensure debugger gets loaded
    assertEvalOut("", "");
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Before(org.junit.Before)

Example 40 with Instrument

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

the class ContextsEventsTest method testContextsNotCloseable.

@Test
public void testContextsNotCloseable() {
    try (Engine engine = Engine.create()) {
        Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
        TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
        final List<ContextEvent> events = test.events;
        try (Context context = Context.newBuilder().engine(engine).build()) {
            context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
            for (ContextEvent event : events) {
                try {
                    event.context.close();
                    fail("Context close should fail.");
                } catch (UnsupportedOperationException ex) {
                // O.K.
                }
            }
        }
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Engine(org.graalvm.polyglot.Engine) 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