1 /*
   2  * Copyright (c) 2014, 2015, 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
  26  * @bug 8015774
  27  * @summary Verify SegmentedCodeCache option's processing
  28  * @library /testlibrary /test/lib /
  29  * @modules java.base/jdk.internal.misc
  30  *          java.compiler
  31  *          java.management
  32  *          jdk.jvmstat/sun.jvmstat.monitor
  33  *
  34  * @build jdk.test.lib.*
  35  * @run driver compiler.codecache.cli.TestSegmentedCodeCacheOption
  36  */
  37 
  38 package compiler.codecache.cli;
  39 
  40 import compiler.codecache.cli.common.CodeCacheOptions;
  41 import jdk.test.lib.ExitCode;
  42 import jdk.test.lib.Platform;
  43 import jdk.test.lib.cli.CommandLineOptionTest;
  44 import sun.hotspot.code.BlobType;
  45 
  46 public class TestSegmentedCodeCacheOption {
  47     private static final String INT_MODE = "-Xint";
  48     private static final String TIERED_COMPILATION = "TieredCompilation";
  49     private static final String SEGMENTED_CODE_CACHE = "SegmentedCodeCache";
  50     private static final String USE_SEGMENTED_CODE_CACHE
  51             = CommandLineOptionTest.prepareBooleanFlag(SEGMENTED_CODE_CACHE,
  52                     true);
  53     private static final long THRESHOLD_CC_SIZE_VALUE
  54             = CodeCacheOptions.mB(240);
  55     private static final long BELOW_THRESHOLD_CC_SIZE
  56             = THRESHOLD_CC_SIZE_VALUE - CodeCacheOptions.mB(1);
  57     private static final String[] UNEXPECTED_MESSAGES = new String[] {
  58             ".*" + SEGMENTED_CODE_CACHE + ".*"
  59     };
  60 
  61 
  62     private static enum TestCase {
  63         JVM_STARTUP {
  64             @Override
  65             public void run() throws Throwable {
  66                 // There should be no errors when we're trying to enable SCC ...
  67                 String testCaseWarningMessage = "JVM output should not contain "
  68                         + "any warnings related to " + SEGMENTED_CODE_CACHE;
  69                 String testCaseExitCodeMessage = "JVM should start without any "
  70                         + "issues with " + USE_SEGMENTED_CODE_CACHE;
  71 
  72                 CommandLineOptionTest.verifySameJVMStartup(
  73                         /* expectedMessages */ null, UNEXPECTED_MESSAGES,
  74                         testCaseExitCodeMessage, testCaseWarningMessage,
  75                         ExitCode.OK, USE_SEGMENTED_CODE_CACHE);
  76                 // ... and when we're trying to enable it w/o TieredCompilation
  77                 testCaseExitCodeMessage = "Disabled tiered compilation should "
  78                         + "not cause startup failure w/ "
  79                         + USE_SEGMENTED_CODE_CACHE;
  80 
  81                 CommandLineOptionTest.verifySameJVMStartup(
  82                         /* expectedMessages */ null, UNEXPECTED_MESSAGES,
  83                         testCaseExitCodeMessage, testCaseWarningMessage,
  84                         ExitCode.OK, USE_SEGMENTED_CODE_CACHE,
  85                         CommandLineOptionTest.prepareBooleanFlag(
  86                                 TIERED_COMPILATION, false));
  87                 // ... and even w/ Xint.
  88                 testCaseExitCodeMessage = "It should be possible to use "
  89                         + USE_SEGMENTED_CODE_CACHE + " in interpreted mode "
  90                         + "without any errors.";
  91 
  92                 CommandLineOptionTest.verifyJVMStartup(
  93                         /* expected messages */ null, UNEXPECTED_MESSAGES,
  94                         testCaseExitCodeMessage, testCaseWarningMessage,
  95                         ExitCode.OK, false, INT_MODE, USE_SEGMENTED_CODE_CACHE);
  96             }
  97         },
  98         OPTION_VALUES_GENERIC {
  99             @Override
 100             public void run() throws Throwable {
 101                 // SCC is disabled w/o TieredCompilation by default
 102                 String errorMessage = SEGMENTED_CODE_CACHE
 103                         + " should be disabled by default  when tiered "
 104                         + "compilation is disabled";
 105 
 106                 CommandLineOptionTest.verifyOptionValueForSameVM(
 107                         SEGMENTED_CODE_CACHE, "false", errorMessage,
 108                         CommandLineOptionTest.prepareBooleanFlag(
 109                                 TIERED_COMPILATION, false));
 110                 // SCC is disabled by default when ReservedCodeCacheSize is too
 111                 // small
 112                 errorMessage = String.format("%s should be disabled bu default "
 113                         + "when %s value is too small.", SEGMENTED_CODE_CACHE,
 114                         BlobType.All.sizeOptionName);
 115 
 116                 CommandLineOptionTest.verifyOptionValueForSameVM(
 117                         SEGMENTED_CODE_CACHE, "false", errorMessage,
 118                         CommandLineOptionTest.prepareNumericFlag(
 119                                 BlobType.All.sizeOptionName,
 120                                 BELOW_THRESHOLD_CC_SIZE));
 121                 // SCC could be explicitly enabled w/ Xint
 122                 errorMessage = String.format("It should be possible to "
 123                                 + "explicitly enable %s in interpreted mode.",
 124                         SEGMENTED_CODE_CACHE);
 125 
 126                 CommandLineOptionTest.verifyOptionValue(SEGMENTED_CODE_CACHE,
 127                         "true", errorMessage, false, INT_MODE,
 128                         USE_SEGMENTED_CODE_CACHE);
 129                 // SCC could be explicitly enabled w/o TieredCompilation and w/
 130                 // small ReservedCodeCacheSize value
 131                 errorMessage = String.format("It should be possible to "
 132                                 + "explicitly enable %s with small %s and "
 133                                 + "disabled tiered comp.", SEGMENTED_CODE_CACHE,
 134                         BlobType.All.sizeOptionName);
 135 
 136                 CommandLineOptionTest.verifyOptionValueForSameVM(
 137                         SEGMENTED_CODE_CACHE, "true", errorMessage,
 138                         CommandLineOptionTest.prepareBooleanFlag(
 139                                 TIERED_COMPILATION, false),
 140                         CommandLineOptionTest.prepareNumericFlag(
 141                                 BlobType.All.sizeOptionName,
 142                                 BELOW_THRESHOLD_CC_SIZE),
 143                         USE_SEGMENTED_CODE_CACHE);
 144             }
 145         },
 146         OPTION_VALUES_SERVER_SPECIFIC {
 147             @Override
 148             public boolean isApplicable() {
 149                 return Platform.isServer() && Platform.isTieredSupported();
 150             }
 151 
 152             @Override
 153             public void run() throws Throwable {
 154                 // SCC is enabled by default when TieredCompilation is on and
 155                 // ReservedCodeCacheSize is large enough
 156                 String errorMessage = String.format("Large enough %s and "
 157                                 + "enabled tiered compilation should enable %s "
 158                                 + "by default.", BlobType.All.sizeOptionName,
 159                         SEGMENTED_CODE_CACHE);
 160 
 161                 CommandLineOptionTest.verifyOptionValueForSameVM(
 162                         SEGMENTED_CODE_CACHE, "true", errorMessage,
 163                         CommandLineOptionTest.prepareNumericFlag(
 164                                 BlobType.All.sizeOptionName,
 165                                 THRESHOLD_CC_SIZE_VALUE),
 166                         CommandLineOptionTest.prepareBooleanFlag(
 167                                 TIERED_COMPILATION, true));
 168             }
 169         };
 170 
 171         TestCase() {
 172         }
 173 
 174         public boolean isApplicable() {
 175             return true;
 176         }
 177 
 178         public abstract void run() throws Throwable;
 179     }
 180 
 181     public static void main(String args[]) throws Throwable {
 182         for (TestCase testCase : TestCase.values()) {
 183             if (testCase.isApplicable()) {
 184                 System.out.println("Running test case: " + testCase.name());
 185                 testCase.run();
 186             } else {
 187                 System.out.println("Test case skipped: " + testCase.name());
 188             }
 189         }
 190     }
 191 }