1 /*
   2  * Copyright (c) 2014, 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 WBStackSize
  26  * @summary verify that whitebox functions getThreadFullStackSize() and getThreadRemainingStackSize are working
  27  * @library /testlibrary /testlibrary/whitebox
  28  * @build WBStackSize
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xss512k WBStackSize
  31  */
  32 
  33 import com.sun.management.HotSpotDiagnosticMXBean;
  34 import sun.hotspot.WhiteBox;
  35 
  36 public class WBStackSize {
  37 
  38     static WhiteBox wb = WhiteBox.getWhiteBox();
  39 
  40     static class SOE extends RuntimeException {
  41         long rss;
  42         SOE(long rss) { this.rss = rss; }
  43     }
  44 
  45     static void recursive(long n) {
  46         try {
  47             recursive(n + 1);
  48         } catch ( StackOverflowError e ) {
  49             throw new SOE(wb.getThreadRemainingStackSize());
  50         }
  51     }
  52 
  53     public static void main(String... args) {
  54         HotSpotDiagnosticMXBean bean = sun.management.ManagementFactoryHelper.getDiagnosticMXBean();
  55         long sso = Long.valueOf(bean.getVMOption("ThreadStackSize").getValue()) * 1024;
  56 
  57         System.out.println("ThreadStackSize VM option = " + sso);
  58 
  59         long fss = wb.getThreadFullStackSize();
  60         long rss = wb.getThreadRemainingStackSize();
  61 
  62         System.out.print("Full stack size " + fss + ": ");
  63 
  64         // Test has -Xss512k option
  65         if ( fss < sso * 0.8 || fss > sso * 1.2 ) {
  66             throw new RuntimeException("getThreadFullStackSize value [" + fss + "] should be within 80%..120% of the value returned by HotSpotDiagnosticMXBean");
  67         } else {
  68             System.out.println("OK");
  69         }
  70 
  71         System.out.print("Remaining stack size " + rss + ": ");
  72         if ( fss < sso * 0.8 || fss > sso * 1.2 ) {
  73             throw new RuntimeException("getThreadRemainingStackSize value [" + fss + "] should be within 80%..120% of the value returned by HotSpotDiagnosticMXBean");
  74         } else {
  75             System.out.println("OK");
  76         }
  77 
  78         try {
  79             recursive(0);
  80         } catch ( SOE e ) {
  81             System.out.print("Remaining stack size on stack overflow " + e.rss + ": ");
  82             if ( e.rss > 8 * 1024 ) {
  83                 throw new RuntimeException("Remaining stack size when StackOverflowError is caught should be at least 8K");
  84             } else {
  85                 System.out.print("OK");
  86             }
  87         }
  88     }
  89 }