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