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 ContStackDepthTest.java
  30  * @run main/othervm/native -agentlib:ContStackDepthTest MyPackage.ContStackDepthTest
  31  */
  32 
  33 import java.math.BigInteger;
  34 import java.math.BigInteger.*;
  35 
  36 public class ContStackDepthTest {
  37     private static final String agentLib = "ContStackDepthTest";
  38     private static ContinuationScope scope = new ContinuationScope("Fibonacci") {};
  39     private static Continuation cont;
  40     private static BigInteger value = BigInteger.ONE;
  41     private static boolean done = false;
  42 
  43     static void log(String str) { System.out.println(str); }
  44 
  45     static native void enableEvents(Thread thread);
  46     static native boolean check();
  47 
  48     public static void main(String[] args) throws Exception {
  49         try {
  50             System.loadLibrary(agentLib);
  51         } catch (UnsatisfiedLinkError ex) {
  52             log("Failed to load " + agentLib + " lib");
  53             log("java.library.path: " + System.getProperty("java.library.path"));
  54             throw ex;
  55         }
  56         log("\n######   main: started   #####\n");
  57         enableEvents(Thread.currentThread());
  58 
  59         ContStackDepthTest obj = new ContStackDepthTest();
  60         obj.runTest();
  61 
  62         if (!check()) {
  63             throw new RuntimeException(
  64                 "ContStackDepthTest failed: miscounted FramePop, MethodEnter or MethodExit events!");
  65         }
  66         log("ContStackDepthTest passed\n");
  67         log("\n#####   main: finished  #####\n");
  68     }
  69 
  70     public void runTest() {
  71         log("\n####  runTest: started  ####\n");
  72         fibTest();
  73         log("\n####  runTest: finished ####\n");
  74     }
  75 
  76     static final Runnable FIB = () -> {
  77         Continuation.yield(scope);
  78         var cur = value;
  79         var next = BigInteger.ONE;
  80         int iter = 0;
  81 
  82         while (true) {
  83             log("\n  ##  FIB iteration: " + (++iter) + "  ##\n");
  84             if (done) return;
  85 
  86             value = next;
  87             Continuation.yield(scope);
  88             var tmp = cur.add(next);
  89             cur = next;
  90             next = tmp;
  91         }
  92     };
  93 
  94     public static void fibTest() {
  95         cont = new Continuation(scope, FIB);
  96         log("\n####  fibTest: started  ####\n");
  97 
  98         System.out.println("getNextFib returned value: " + getNextFib());
  99         System.out.println("getNextFib returned value: " + getNextFib());
 100         System.out.println("getNextFib returned value: " + getNextFib());
 101 
 102         done = true;
 103 
 104         System.out.println("getNextFib returned value: " + getNextFib());
 105 
 106         log("\n####  fibTest: finished ####\n");
 107     }
 108 
 109     public static BigInteger getNextFib() {
 110         cont.run();
 111         return value;
 112     }
 113 }