Search in sources :

Example 1 with AllocationReporter

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

the class AllocationReporterTest method testReporterChangeListener.

@Test
public void testReporterChangeListener() {
    try {
        Class.forName("java.beans.PropertyChangeListener");
    } catch (ClassNotFoundException ex) {
        // skip the test if running only with java.base JDK9 module
        return;
    }
    // Test of AllocationReporter property change listener notifications
    allocation.setEnabled(false);
    Source source = Source.create(AllocationReporterLanguage.ID, "NEW");
    allocation.setAllocationConsumers((info) -> {
    }, (info) -> {
    });
    context.eval(source);
    AllocationReporter reporter = (AllocationReporter) context.getPolyglotBindings().getMember(AllocationReporter.class.getSimpleName()).asHostObject();
    AtomicInteger listenerCalls = new AtomicInteger(0);
    AllocationReporterListener activatedListener = AllocationReporterListener.register(listenerCalls, reporter);
    assertEquals(0, listenerCalls.get());
    assertFalse(reporter.isActive());
    allocation.setEnabled(true);
    assertEquals(1, listenerCalls.get());
    activatedListener.unregister();
    listenerCalls.set(0);
    AllocationDeactivatedListener deactivatedListener = AllocationDeactivatedListener.register(listenerCalls, reporter);
    assertEquals(0, listenerCalls.get());
    assertTrue(reporter.isActive());
    allocation.setEnabled(false);
    assertEquals(1, listenerCalls.get());
    deactivatedListener.unregister();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllocationReporter(com.oracle.truffle.api.instrumentation.AllocationReporter) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with AllocationReporter

use of com.oracle.truffle.api.instrumentation.AllocationReporter 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

AllocationReporter (com.oracle.truffle.api.instrumentation.AllocationReporter)2 Test (org.junit.Test)2 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)1 Context (org.graalvm.polyglot.Context)1 Source (org.graalvm.polyglot.Source)1