1 /*
   2  * Copyright (c) 2014, 2016, 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 import jdk.test.lib.*;
  25 import sun.hotspot.WhiteBox;
  26 
  27 /*
  28  * @test CheckSegmentedCodeCache
  29  * @bug 8015774
  30  * @library /testlibrary /test/lib
  31  * @summary "Checks VM options related to the segmented code cache"
  32  * @modules java.base/sun.misc
  33  *          java.management
  34  * @build CheckSegmentedCodeCache
  35  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  37  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI CheckSegmentedCodeCache
  38  */
  39 public class CheckSegmentedCodeCache {
  40   private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  41   // Code heap names
  42   private static final String NON_METHOD = "CodeHeap 'non-nmethods'";
  43   private static final String PROFILED = "CodeHeap 'profiled nmethods'";
  44   private static final String NON_PROFILED = "CodeHeap 'non-profiled nmethods'";
  45 
  46   private static void verifySegmentedCodeCache(ProcessBuilder pb, boolean enabled) throws Exception {
  47     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  48     out.shouldHaveExitValue(0);
  49     if (enabled) {
  50       try {
  51         // Non-nmethod code heap should be always available with the segmented code cache
  52         out.shouldContain(NON_METHOD);
  53       } catch (RuntimeException e) {
  54         // Check if TieredCompilation is disabled (in a client VM)
  55         if(!out.getOutput().contains("-XX:+TieredCompilation not supported in this VM")) {
  56           // Code cache is not segmented
  57           throw new RuntimeException("No code cache segmentation.");
  58         }
  59       }
  60     } else {
  61       out.shouldNotContain(NON_METHOD);
  62     }
  63   }
  64 
  65   private static void verifyCodeHeapNotExists(ProcessBuilder pb, String... heapNames) throws Exception {
  66     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  67     out.shouldHaveExitValue(0);
  68     for (String name : heapNames) {
  69       out.shouldNotContain(name);
  70     }
  71   }
  72 
  73   private static void failsWith(ProcessBuilder pb, String message) throws Exception {
  74     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  75     out.shouldContain(message);
  76     out.shouldHaveExitValue(1);
  77   }
  78 
  79   /**
  80    * Check the result of segmented code cache related VM options.
  81    */
  82   public static void main(String[] args) throws Exception {
  83     ProcessBuilder pb;
  84 
  85     // Disabled with ReservedCodeCacheSize < 240MB
  86     pb = ProcessTools.createJavaProcessBuilder("-XX:ReservedCodeCacheSize=239m",
  87                                                "-XX:+PrintCodeCache", "-version");
  88     verifySegmentedCodeCache(pb, false);
  89 
  90     // Disabled without TieredCompilation
  91     pb = ProcessTools.createJavaProcessBuilder("-XX:-TieredCompilation",
  92                                                "-XX:+PrintCodeCache", "-version");
  93     verifySegmentedCodeCache(pb, false);
  94 
  95     // Enabled with TieredCompilation and ReservedCodeCacheSize >= 240MB
  96     pb = ProcessTools.createJavaProcessBuilder("-XX:+TieredCompilation",
  97                                                "-XX:ReservedCodeCacheSize=240m",
  98                                                "-XX:+PrintCodeCache", "-version");
  99     verifySegmentedCodeCache(pb, true);
 100     pb = ProcessTools.createJavaProcessBuilder("-XX:+TieredCompilation",
 101                                                "-XX:ReservedCodeCacheSize=400m",
 102                                                "-XX:+PrintCodeCache", "-version");
 103     verifySegmentedCodeCache(pb, true);
 104 
 105     // Always enabled if SegmentedCodeCache is set
 106     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 107                                                "-XX:-TieredCompilation",
 108                                                "-XX:ReservedCodeCacheSize=239m",
 109                                                "-XX:+PrintCodeCache", "-version");
 110     verifySegmentedCodeCache(pb, true);
 111 
 112     // The profiled and non-profiled code heaps should not be available in
 113     // interpreter-only mode
 114     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 115                                                "-Xint",
 116                                                "-XX:+PrintCodeCache", "-version");
 117     verifyCodeHeapNotExists(pb, PROFILED, NON_PROFILED);
 118 
 119     // If we stop compilation at CompLevel_none or CompLevel_simple we
 120     // don't need a profiled code heap.
 121     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 122                                                "-XX:TieredStopAtLevel=0",
 123                                                "-XX:+PrintCodeCache", "-version");
 124     verifyCodeHeapNotExists(pb, PROFILED);
 125     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 126                                                "-XX:TieredStopAtLevel=1",
 127                                                "-XX:+PrintCodeCache", "-version");
 128     verifyCodeHeapNotExists(pb, PROFILED);
 129 
 130     // Fails with too small non-nmethod code heap size
 131     pb = ProcessTools.createJavaProcessBuilder("-XX:NonNMethodCodeHeapSize=100K");
 132     failsWith(pb, "Invalid NonNMethodCodeHeapSize");
 133 
 134     // Fails if code heap sizes do not add up
 135     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 136                                                "-XX:ReservedCodeCacheSize=10M",
 137                                                "-XX:NonNMethodCodeHeapSize=5M",
 138                                                "-XX:ProfiledCodeHeapSize=5M",
 139                                                "-XX:NonProfiledCodeHeapSize=5M");
 140     failsWith(pb, "Invalid code heap sizes");
 141 
 142     // Fails if not enough space for VM internal code
 143     long minUseSpace = WHITE_BOX.getUintxVMFlag("CodeCacheMinimumUseSpace");
 144     // minimum size: CodeCacheMinimumUseSpace DEBUG_ONLY(* 3)
 145     long minSize = (Platform.isDebugBuild() ? 3 : 1) * minUseSpace;
 146     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
 147                                                "-XX:ReservedCodeCacheSize=" + minSize,
 148                                                "-XX:InitialCodeCacheSize=100K");
 149     failsWith(pb, "Not enough space in non-nmethod code heap to run VM");
 150   }
 151 }