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();
}
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);
}
Aggregations