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  */
  32 public class CheckSegmentedCodeCache {
  33 
  34   private static void verifySegmentedCodeCache(ProcessBuilder pb, boolean enabled) throws Exception {
  35     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  36     if (enabled) {
  37       try {
  38         out.shouldContain("CodeHeap");
  39       } catch (RuntimeException e) {
  40         out.shouldContain("TieredCompilation is disabled in this release.");
  41       }
  42     } else {
  43       out.shouldNotContain("CodeHeap");
  44     }
  45     out.shouldHaveExitValue(0);
  46   }
  47 
  48   private static void failsWith(ProcessBuilder pb, String message) throws Exception {
  49     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  50     out.shouldContain(message);
  51     out.shouldHaveExitValue(1);
  52   }
  53 
  54   /**
  55    * Check the result of segmented code cache related VM options.
  56    */
  57   public static void main(String[] args) throws Exception {
  58     ProcessBuilder pb;
  59 
  60     // Disabled with ReservedCodeCacheSize < 240MB
  61     pb = ProcessTools.createJavaProcessBuilder("-XX:ReservedCodeCacheSize=239m",
  62                                                "-XX:+PrintCodeCache", "-version");
  63     verifySegmentedCodeCache(pb, false);
  64 
  65     // Disabled without TieredCompilation
  66     pb = ProcessTools.createJavaProcessBuilder("-XX:-TieredCompilation",
  67                                                "-XX:+PrintCodeCache", "-version");
  68     verifySegmentedCodeCache(pb, false);
  69 
  70     // Enabled with TieredCompilation and ReservedCodeCacheSize >= 240MB
  71     pb = ProcessTools.createJavaProcessBuilder("-XX:+TieredCompilation",
  72                                                "-XX:ReservedCodeCacheSize=240m",
  73                                                "-XX:+PrintCodeCache", "-version");
  74     verifySegmentedCodeCache(pb, true);
  75 
  76     // Always enabled if SegmentedCodeCache is set
  77     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
  78                                                "-XX:-TieredCompilation",
  79                                                "-XX:ReservedCodeCacheSize=239m",
  80                                                "-XX:+PrintCodeCache", "-version");
  81     verifySegmentedCodeCache(pb, true);
  82 
  83     // Fails with too small non-method code heap size
  84     pb = ProcessTools.createJavaProcessBuilder("-XX:NonMethodCodeHeapSize=100K");
  85     failsWith(pb, "Invalid NonMethodCodeHeapSize");
  86 
  87     // Fails if code heap sizes do not add up
  88     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
  89                                                "-XX:ReservedCodeCacheSize=10M",
  90                                                "-XX:NonMethodCodeHeapSize=5M",
  91                                                "-XX:ProfiledCodeHeapSize=5M",
  92                                                "-XX:NonProfiledCodeHeapSize=5M");
  93     failsWith(pb, "Invalid code heap sizes");
  94 
  95     // Fails if not enough space for VM internal code
  96     pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
  97                                                "-XX:ReservedCodeCacheSize=1700K",
  98                                                "-XX:InitialCodeCacheSize=100K");
  99     failsWith(pb, "Not enough space in non-method code heap to run VM");
 100   }
 101 }