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