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 package MyPackage;
  25 
  26 /**
  27  * @test
  28  * @summary Verifies JVMTI support for Continuations
  29  * @compile ContFramePopTest.java
  30  * @run main/othervm/native -agentlib:ContFramePopTest MyPackage.ContFramePopTest
  31  */
  32 
  33 public class ContFramePopTest {
  34     private static final String agentLib = "ContFramePopTest";
  35     private static final ContinuationScope FOO = new ContinuationScope() {};
  36 
  37     static void log(String str) { System.out.println(str); }
  38 
  39     static native void enableEvents(Thread thread);
  40     static native boolean check();
  41 
  42     public static void main(String[] args) throws Exception {
  43         try {
  44             System.loadLibrary(agentLib);
  45         } catch (UnsatisfiedLinkError ex) {
  46             log("Failed to load " + agentLib + " lib");
  47             log("java.library.path: " + System.getProperty("java.library.path"));
  48             throw ex;
  49         }
  50         log("\n######   main: started   #####\n");
  51         enableEvents(Thread.currentThread());
  52 
  53         ContFramePopTest obj = new ContFramePopTest();
  54         obj.runTest();
  55 
  56         if (!check()) {
  57             throw new RuntimeException("ContFramePopTest failed: miscounted FramePop or MethodExit events!");
  58         }
  59         log("ContFramePopTest passed\n");
  60         log("\n#####   main: finished  #####\n");
  61     }
  62 
  63     public void runTest() {
  64         log("\n####  runTest: started  ####\n");
  65         Continuation cont = new Continuation(FOO, ()-> { 
  66             double dval = 0;
  67 
  68             log("\n##    cont: started     ##\n");
  69             for (int k = 1; k < 3; k++) {
  70                 int ival = 3;
  71                 String str = "abc";
  72 
  73                 log("\n cont: iteration #" + (k - 1));
  74 
  75                 log("\n<<<< runTest: before foo(): " + ival + ", " + str + ", " + dval + " <<<<"); 
  76                 dval += foo(k);
  77                 log(  ">>>> runTest:  after foo(): " + ival + ", " + str + ", " + dval + " >>>>"); 
  78             }
  79             log("\n##    cont: finished    ##\n");
  80         });
  81         int i = 0;
  82         while (!cont.isDone()) {
  83             log("\n##   runTest: iteration #" + (i++));
  84             cont.run();
  85             System.gc();
  86         }
  87         log("\n####  runTest: finished ####\n");
  88     }
  89 
  90     static double foo(int iarg) {
  91         long lval = 8;
  92         String str1 = "yyy";
  93 
  94         log("\n####   foo: started  ####\n");
  95         log("foo: before bar(): " + lval + ", " + str1 + ", " + iarg);
  96         String str2 = bar(iarg + 1);
  97         log("foo:  after bar(): " + lval + ", " + str1 + ", " + str2); 
  98 
  99         log("\n####   foo: finished ####\n");
 100         return Integer.parseInt(str2) + 1;
 101     }
 102 
 103     static String bar(long larg) {
 104         double dval = 9.99;
 105         String str = zzz();
 106 
 107         log("\n####   bar: started  ####\n");
 108         log("bar: before yield(): " + dval + ", " + str + ", " + larg); 
 109         Continuation.yield(FOO);
 110 
 111         long lval = larg + 1;
 112         log("bar:  after yield(): " + dval + ", " + str + ", " + lval); 
 113 
 114         str = zzz();
 115 
 116         log("\n####   bar: finished: str = " + str + " ####\n");
 117         return "" + lval;
 118     }
 119 
 120     static String zzz() { return "zzz"; }
 121 }