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