Search in sources :

Example 36 with VirtualFrame

use of com.oracle.truffle.api.frame.VirtualFrame in project graal by oracle.

the class InstrumentationEventTest method setUp.

@Before
public final void setUp() {
    this.context = Context.create();
    this.instrument = context.getEngine().getInstruments().get(InputFilterTestInstrument.ID).lookup(InputFilterTestInstrument.class);
    this.instrumenter = instrument.environment.getInstrumenter();
    this.events = new ArrayList<>();
    this.factory = new ExecutionEventNodeFactory() {

        public ExecutionEventNode create(EventContext c) {
            return new ExecutionEventNode() {

                {
                    for (int i = 0; i < getInputCount(); i++) {
                        Assert.assertNotNull(getInputContext(i));
                    }
                }

                @Override
                protected void onInputValue(VirtualFrame frame, EventContext inputContext, int inputIndex, Object inputValue) {
                    saveInputValue(frame, inputIndex, inputValue);
                    assertTrue(inputIndex < getInputCount());
                    assertSame(inputContext, getInputContext(inputIndex));
                    events.add(new Event(EventKind.INPUT_VALUE, c, frame, null, null, inputIndex, inputValue, createInputContexts()));
                }

                @Override
                public void onEnter(VirtualFrame frame) {
                    events.add(new Event(EventKind.ENTER, c, frame, null, null, -1, null, createInputContexts()));
                }

                @Override
                protected void onReturnValue(VirtualFrame frame, Object result) {
                    events.add(new Event(EventKind.RETURN_VALUE, c, frame, getSavedInputValues(frame), result, -1, null, createInputContexts()));
                }

                private EventContext[] createInputContexts() {
                    EventContext[] inputContexts = new EventContext[getInputCount()];
                    for (int i = 0; i < getInputCount(); i++) {
                        Assert.assertNotNull(getInputContext(i));
                        inputContexts[i] = getInputContext(i);
                    }
                    return inputContexts;
                }
            };
        }
    };
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) ExecutionEventNodeFactory(com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) Before(org.junit.Before)

Example 37 with VirtualFrame

use of com.oracle.truffle.api.frame.VirtualFrame in project graal by oracle.

the class InstrumentationMultiThreadingTest method createDummyBindings.

private static EventBinding<?>[] createDummyBindings(Instrumenter instrumenter) {
    EventBinding<?>[] bindings = new EventBinding<?>[5];
    int bi = 0;
    ExecutionEventListener dummyListener = new ExecutionEventListener() {

        public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
        }

        public void onEnter(EventContext context, VirtualFrame frame) {
        }
    };
    bindings[bi++] = instrumenter.attachExecutionEventListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build(), dummyListener);
    bindings[bi++] = instrumenter.attachExecutionEventListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.STATEMENT).build(), dummyListener);
    bindings[bi++] = instrumenter.attachLoadSourceListener(SourceFilter.ANY, new LoadSourceListener() {

        public void onLoad(LoadSourceEvent event) {
        }
    }, true);
    bindings[bi++] = instrumenter.attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build(), new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
        }
    }, true);
    bindings[bi++] = instrumenter.attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.STATEMENT).build(), new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
        }
    }, true);
    return bindings;
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) EventBinding(com.oracle.truffle.api.instrumentation.EventBinding) LoadSourceEvent(com.oracle.truffle.api.instrumentation.LoadSourceEvent) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) LoadSourceListener(com.oracle.truffle.api.instrumentation.LoadSourceListener) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener)

Example 38 with VirtualFrame

use of com.oracle.truffle.api.frame.VirtualFrame in project graal by oracle.

the class InstrumentationUpdateTest method setEventFilter.

private void setEventFilter(SourceSectionFilter filter) {
    EventBinding<?> old = this.eventBinding;
    eventBinding = instrumentEnv.getInstrumenter().attachExecutionEventListener(filter, new ExecutionEventListener() {

        public void onReturnValue(EventContext ctx, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext ctx, VirtualFrame frame, Throwable exception) {
        }

        public void onEnter(EventContext ctx, VirtualFrame frame) {
            executionEvents.add(ctx);
        }
    });
    instrumentEnv.getInstrumenter().attachLoadSourceSectionListener(filter, new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
            loadEvents.add(event);
        }
    }, true);
    // dispose afterwards to avoid disposal of instrumentation wrappers
    if (old != null) {
        old.dispose();
    }
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener)

Example 39 with VirtualFrame

use of com.oracle.truffle.api.frame.VirtualFrame in project graal by oracle.

the class SourceSectionFilterTest method createRootNode.

static RootNode createRootNode(Engine engine, final SourceSection section, final Boolean internal, Node... children) throws Exception {
    Language language = engine.getLanguages().get(InstrumentationTestLanguage.ID);
    Field impl = Language.class.getDeclaredField("impl");
    ReflectionUtils.setAccessible(impl, true);
    Object polyglotLanguage = impl.get(language);
    Method ensureInitialized = polyglotLanguage.getClass().getDeclaredMethod("ensureInitialized");
    ReflectionUtils.setAccessible(ensureInitialized, true);
    ensureInitialized.invoke(polyglotLanguage);
    Field info = polyglotLanguage.getClass().getDeclaredField("info");
    ReflectionUtils.setAccessible(info, true);
    LanguageInfo languageInfo = (LanguageInfo) info.get(polyglotLanguage);
    Field spi = LanguageInfo.class.getDeclaredField("spi");
    ReflectionUtils.setAccessible(spi, true);
    TruffleLanguage<?> truffleLanguage = (TruffleLanguage<?>) spi.get(languageInfo);
    return new RootNode(truffleLanguage) {

        @Node.Children
        Node[] rootChildren = children;

        @Override
        protected boolean isInstrumentable() {
            return true;
        }

        @Override
        public SourceSection getSourceSection() {
            return section;
        }

        @Override
        public boolean isInternal() {
            if (internal == null) {
                return super.isInternal();
            } else {
                return internal;
            }
        }

        @Override
        public Object execute(VirtualFrame frame) {
            return null;
        }
    };
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Field(java.lang.reflect.Field) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) RootNode(com.oracle.truffle.api.nodes.RootNode) Language(org.graalvm.polyglot.Language) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Method(java.lang.reflect.Method) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Example 40 with VirtualFrame

use of com.oracle.truffle.api.frame.VirtualFrame in project graal by oracle.

the class AllocationReporterPartialEvaluationTest method testConsistentAssertions.

@Test
public void testConsistentAssertions() {
    // Test that onEnter()/onReturnValue() are not broken
    // when only one of them is compiled with PE.
    Context context = Context.newBuilder(AllocationReporterLanguage.ID).build();
    context.initialize(AllocationReporterLanguage.ID);
    final TestAllocationReporter tester = context.getEngine().getInstruments().get(TestAllocationReporter.ID).lookup(TestAllocationReporter.class);
    assertNotNull(tester);
    final AllocationReporter reporter = (AllocationReporter) context.getPolyglotBindings().getMember(AllocationReporter.class.getSimpleName()).asHostObject();
    final Long[] value = new Long[] { 1L };
    OptimizedCallTarget enterTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            reporter.onEnter(value[0], 4, 8);
            return null;
        }
    });
    OptimizedCallTarget returnTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            reporter.onReturnValue(value[0], 4, 8);
            return null;
        }
    });
    // Interpret both:
    assertNotCompiled(enterTarget);
    enterTarget.call();
    assertNotCompiled(returnTarget);
    returnTarget.call();
    value[0]++;
    enterTarget.compile();
    returnTarget.compile();
    assertCompiled(enterTarget);
    assertCompiled(returnTarget);
    long expectedCounters = allocCounter(value[0]);
    assertEquals(expectedCounters, tester.getEnterCount());
    assertEquals(expectedCounters, tester.getReturnCount());
    for (int j = 0; j < 2; j++) {
        // Compile both:
        for (int i = 0; i < 5; i++) {
            assertCompiled(enterTarget);
            enterTarget.call();
            assertCompiled(returnTarget);
            returnTarget.call();
            value[0]++;
        }
        expectedCounters = allocCounter(value[0]);
        assertEquals(expectedCounters, tester.getEnterCount());
        assertEquals(expectedCounters, tester.getReturnCount());
        // Deoptimize enter:
        enterTarget.invalidate(this, "test");
        assertNotCompiled(enterTarget);
        enterTarget.call();
        assertCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
        // Deoptimize return:
        returnTarget.invalidate(this, "test");
        assertCompiled(enterTarget);
        enterTarget.call();
        assertNotCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
        // Deoptimize both:
        enterTarget.invalidate(this, "test");
        returnTarget.invalidate(this, "test");
        assertNotCompiled(enterTarget);
        enterTarget.call();
        assertNotCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
    }
    // Check that the allocation calls happened:
    expectedCounters = allocCounter(value[0]);
    assertEquals(expectedCounters, tester.getEnterCount());
    assertEquals(expectedCounters, tester.getReturnCount());
    assertCompiled(enterTarget);
    assertCompiled(returnTarget);
    // Verify that the assertions work in the compiled code:
    value[0] = null;
    boolean expectedFailure = true;
    // Deoptimize for assertions to be active
    enterTarget.invalidate(this, "test");
    try {
        enterTarget.call();
        expectedFailure = false;
    } catch (AssertionError err) {
    // O.K.
    }
    assertTrue("onEnter(null) did not fail!", expectedFailure);
    // Deoptimize for assertions to be active
    returnTarget.invalidate(this, "test");
    value[0] = Long.MIN_VALUE;
    try {
        returnTarget.call();
        expectedFailure = false;
    } catch (AssertionError err) {
    // O.K.
    }
    assertTrue("onReturn(<unseen value>) did not fail!", expectedFailure);
}
Also used : Context(org.graalvm.polyglot.Context) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) AllocationReporter(com.oracle.truffle.api.instrumentation.AllocationReporter) Test(org.junit.Test)

Aggregations

VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)48 Test (org.junit.Test)34 RootNode (com.oracle.truffle.api.nodes.RootNode)27 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)14 CallTarget (com.oracle.truffle.api.CallTarget)11 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)9 RootCallTarget (com.oracle.truffle.api.RootCallTarget)6 EventContext (com.oracle.truffle.api.instrumentation.EventContext)5 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)5 ExecutionEventListener (com.oracle.truffle.api.instrumentation.ExecutionEventListener)4 Node (com.oracle.truffle.api.nodes.Node)4 AbstractTestNode (org.graalvm.compiler.truffle.test.nodes.AbstractTestNode)4 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)4 Ignore (org.junit.Ignore)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 TruffleException (com.oracle.truffle.api.TruffleException)3 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)3 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)3