comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java @ 19247:128586040207

Truffle/Instrumentation: TruffleEventReceiver renamed to TruffleEventListener
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 10 Feb 2015 16:44:11 -0800
parents acd822f17ef5
children b5467bb34b24
comparison
equal deleted inserted replaced
19246:d3e835fa6bbf 19247:128586040207
316 @Test 316 @Test
317 public void testProbeLite() { 317 public void testProbeLite() {
318 318
319 // Use the "lite-probing" option, limited to a single pass of 319 // Use the "lite-probing" option, limited to a single pass of
320 // probing and a single Instrument at each probed node. This 320 // probing and a single Instrument at each probed node. This
321 // particular test uses a shared event receiver at every 321 // particular test uses a shared event listener at every
322 // lite-probed node. 322 // lite-probed node.
323 final TestEventReceiver receiver = new TestEventReceiver(); 323 final TestEventListener listener = new TestEventListener();
324 324
325 TestASTLiteProber astLiteProber = new TestASTLiteProber(receiver); 325 TestASTLiteProber astLiteProber = new TestASTLiteProber(listener);
326 Probe.registerASTProber(astLiteProber); 326 Probe.registerASTProber(astLiteProber);
327 327
328 // Create a simple addition AST 328 // Create a simple addition AST
329 final TruffleRuntime runtime = Truffle.getRuntime(); 329 final TruffleRuntime runtime = Truffle.getRuntime();
330 final TestValueNode leftValueNode = new TestValueNode(6); 330 final TestValueNode leftValueNode = new TestValueNode(6);
335 // Creating a call target sets the parent pointers in this tree and is necessary prior to 335 // Creating a call target sets the parent pointers in this tree and is necessary prior to
336 // checking any parent/child relationships 336 // checking any parent/child relationships
337 final CallTarget callTarget = runtime.createCallTarget(rootNode); 337 final CallTarget callTarget = runtime.createCallTarget(rootNode);
338 338
339 // Check that the instrument is working as expected. 339 // Check that the instrument is working as expected.
340 assertEquals(0, receiver.counter); 340 assertEquals(0, listener.counter);
341 callTarget.call(); 341 callTarget.call();
342 assertEquals(2, receiver.counter); 342 assertEquals(2, listener.counter);
343 343
344 // Check that you can't probe a node that's already received a probeLite() call 344 // Check that you can't probe a node that's already received a probeLite() call
345 try { 345 try {
346 leftValueNode.probe(); 346 leftValueNode.probe();
347 fail(); 347 fail();
407 } catch (ProbeException e) { 407 } catch (ProbeException e) {
408 assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE); 408 assertEquals(e.getFailure().getReason(), ProbeFailure.Reason.WRAPPER_NODE);
409 } 409 }
410 410
411 // Use reflection to check that each WrapperNode has a ProbeLiteNode with a 411 // Use reflection to check that each WrapperNode has a ProbeLiteNode with a
412 // SimpleEventReceiver 412 // SimpleEventListener
413 try { 413 try {
414 java.lang.reflect.Field probeNodeField = leftWrapper.getClass().getDeclaredField("probeNode"); 414 java.lang.reflect.Field probeNodeField = leftWrapper.getClass().getDeclaredField("probeNode");
415 415
416 // cheat: probeNode is private, so we change it's accessibility at runtime 416 // cheat: probeNode is private, so we change it's accessibility at runtime
417 probeNodeField.setAccessible(true); 417 probeNodeField.setAccessible(true);
418 ProbeNode probeNode = (ProbeNode) probeNodeField.get(leftWrapper); 418 ProbeNode probeNode = (ProbeNode) probeNodeField.get(leftWrapper);
419 419
420 // hack: Since ProbeLiteNode is not visible, we do a string compare here 420 // hack: Since ProbeLiteNode is not visible, we do a string compare here
421 assertTrue(probeNode.getClass().toString().endsWith("ProbeLiteNode")); 421 assertTrue(probeNode.getClass().toString().endsWith("ProbeLiteNode"));
422 422
423 // Now we do the same to check the type of the eventReceiver in ProbeLiteNode 423 // Now we do the same to check the type of the eventListener in ProbeLiteNode
424 java.lang.reflect.Field eventReceiverField = probeNode.getClass().getDeclaredField("eventReceiver"); 424 java.lang.reflect.Field eventListenerField = probeNode.getClass().getDeclaredField("eventListener");
425 eventReceiverField.setAccessible(true); 425 eventListenerField.setAccessible(true);
426 TruffleEventReceiver eventReceiver = (TruffleEventReceiver) eventReceiverField.get(probeNode); 426 TruffleEventListener eventListener = (TruffleEventListener) eventListenerField.get(probeNode);
427 assertTrue(eventReceiver instanceof SimpleEventReceiver); 427 assertTrue(eventListener instanceof SimpleEventListener);
428 428
429 // Reset accessibility 429 // Reset accessibility
430 probeNodeField.setAccessible(false); 430 probeNodeField.setAccessible(false);
431 eventReceiverField.setAccessible(false); 431 eventListenerField.setAccessible(false);
432 432
433 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 433 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
434 fail(); 434 fail();
435 } 435 }
436 436
586 public int enterCount = 0; 586 public int enterCount = 0;
587 public int leaveCount = 0; 587 public int leaveCount = 0;
588 public final Instrument instrument; 588 public final Instrument instrument;
589 589
590 public TestCounter() { 590 public TestCounter() {
591 instrument = Instrument.create(new SimpleEventReceiver() { 591 instrument = Instrument.create(new SimpleEventListener() {
592 592
593 @Override 593 @Override
594 public void enter(Node node, VirtualFrame frame) { 594 public void enter(Node node, VirtualFrame frame) {
595 enterCount++; 595 enterCount++;
596 } 596 }
639 node.accept(this); 639 node.accept(this);
640 } 640 }
641 } 641 }
642 642
643 /** 643 /**
644 * "lite-probes" every value node with a shared event receiver. 644 * "lite-probes" every value node with a shared event listener.
645 */ 645 */
646 private static final class TestASTLiteProber implements NodeVisitor, ASTProber { 646 private static final class TestASTLiteProber implements NodeVisitor, ASTProber {
647 private final TruffleEventReceiver eventReceiver; 647 private final TruffleEventListener eventListener;
648 648
649 public TestASTLiteProber(SimpleEventReceiver simpleEventReceiver) { 649 public TestASTLiteProber(SimpleEventListener simpleEventListener) {
650 this.eventReceiver = simpleEventReceiver; 650 this.eventListener = simpleEventListener;
651 } 651 }
652 652
653 public boolean visit(Node node) { 653 public boolean visit(Node node) {
654 if (node instanceof TestValueNode) { 654 if (node instanceof TestValueNode) {
655 final TestLanguageNode testNode = (TestValueNode) node; 655 final TestLanguageNode testNode = (TestValueNode) node;
656 testNode.probeLite(eventReceiver); 656 testNode.probeLite(eventListener);
657 } 657 }
658 return true; 658 return true;
659 } 659 }
660 660
661 public void probeAST(Node node) { 661 public void probeAST(Node node) {
665 665
666 /** 666 /**
667 * Counts the number of "enter" events at probed nodes. 667 * Counts the number of "enter" events at probed nodes.
668 * 668 *
669 */ 669 */
670 static final class TestEventReceiver extends SimpleEventReceiver { 670 static final class TestEventListener extends SimpleEventListener {
671 671
672 public int counter = 0; 672 public int counter = 0;
673 673
674 @Override 674 @Override
675 public void enter(Node node, VirtualFrame frame) { 675 public void enter(Node node, VirtualFrame frame) {
690 690
691 // Attach a new instrument for every Probe 691 // Attach a new instrument for every Probe
692 // where we want to count executions. 692 // where we want to count executions.
693 // it will get copied when ASTs cloned, so 693 // it will get copied when ASTs cloned, so
694 // keep the count in this outer class. 694 // keep the count in this outer class.
695 probe.attach(Instrument.create(new SimpleEventReceiver() { 695 probe.attach(Instrument.create(new SimpleEventListener() {
696 696
697 @Override 697 @Override
698 public void enter(Node node, VirtualFrame frame) { 698 public void enter(Node node, VirtualFrame frame) {
699 count++; 699 count++;
700 } 700 }