1 /*
  2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @summary Verifies JVMTI support for Continuations
 27  * @compile ContFramePopTest.java
 28  * @modules java.base/jdk.internal.vm
 29  * @run main/othervm/native -agentlib:ContFramePopTest ContFramePopTest
 30  */
 31 
 32 import jdk.internal.vm.Continuation;
 33 import jdk.internal.vm.ContinuationScope;
 34 
 35 public class ContFramePopTest {
 36     private static final String agentLib = "ContFramePopTest";
 37     private static final ContinuationScope FOO = new ContinuationScope() {};
 38 
 39     static void log(String str) { System.out.println(str); }
 40 
 41     static native void enableEvents(Thread thread);
 42     static native boolean check();
 43 
 44     public static void main(String[] args) throws Exception {
 45         try {
 46             System.loadLibrary(agentLib);
 47         } catch (UnsatisfiedLinkError ex) {
 48             log("Failed to load " + agentLib + " lib");
 49             log("java.library.path: " + System.getProperty("java.library.path"));
 50             throw ex;
 51         }
 52         log("\n######   main: started   #####\n");
 53         enableEvents(Thread.currentThread());
 54 
 55         ContFramePopTest obj = new ContFramePopTest();
 56         obj.runTest();
 57 
 58         if (!check()) {
 59             throw new RuntimeException("ContFramePopTest failed: miscounted FramePop or MethodExit events!");
 60         }
 61         log("ContFramePopTest passed\n");
 62         log("\n#####   main: finished  #####\n");
 63     }
 64 
 65     public void runTest() {
 66         log("\n####  runTest: started  ####\n");
 67         Continuation cont = new Continuation(FOO, ()-> { 
 68             double dval = 0;
 69 
 70             log("\n##    cont: started     ##\n");
 71             for (int k = 1; k < 3; k++) {
 72                 int ival = 3;
 73                 String str = "abc";
 74 
 75                 log("\n cont: iteration #" + (k - 1));
 76 
 77                 log("\n<<<< runTest: before foo(): " + ival + ", " + str + ", " + dval + " <<<<"); 
 78                 dval += foo(k);
 79 	        log(  ">>>> runTest:  after foo(): " + ival + ", " + str + ", " + dval + " >>>>"); 
 80             }
 81             log("\n##    cont: finished    ##\n");
 82         });
 83         int i = 0;
 84         while (!cont.isDone()) {
 85             log("\n##   runTest: iteration #" + (i++));
 86             cont.run();
 87             System.gc();
 88         }
 89         log("\n####  runTest: finished ####\n");
 90     }
 91 
 92     static double foo(int iarg) {
 93         long lval = 8;
 94         String str1 = "yyy";
 95 
 96         log("\n####   foo: started  ####\n");
 97         log("foo: before bar(): " + lval + ", " + str1 + ", " + iarg);
 98         String str2 = bar(iarg + 1);
 99 	log("foo:  after bar(): " + lval + ", " + str1 + ", " + str2); 
100 
101         log("\n####   foo: finished ####\n");
102         return Integer.parseInt(str2) + 1;
103     }
104 
105     static String bar(long larg) {
106         double dval = 9.99;
107         String str = zzz();
108 
109         log("\n####   bar: started  ####\n");
110         log("bar: before yield(): " + dval + ", " + str + ", " + larg); 
111         Continuation.yield(FOO);
112 
113         long lval = larg + 1;
114         log("bar:  after yield(): " + dval + ", " + str + ", " + lval); 
115 
116         str = zzz();
117 
118         log("\n####   bar: finished: str = " + str + " ####\n");
119         return "" + lval;
120     }
121 
122     static String zzz() { return "zzz"; }
123 }