1 /*
2  * Copyright (c) 2014, 2019, 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 package gc.arguments;
25 
26 import jdk.test.lib.Platform;
27 import jdk.test.lib.process.ExitCode;
28 import jdk.test.lib.cli.CommandLineOptionTest;
29 
30 /**
31  * @test
32  * @bug 8031323
33  * @summary Verify SurvivorAlignmentInBytes option processing.
34  * @library /test/lib
35  * @requires vm.opt.SurvivorAlignmentInBytes == null
36  *           & vm.opt.ObjectAlignmentInBytes == null
37  *           & vm.opt.UnlockExperimentalVMOptions == null
38  *           & (vm.opt.IgnoreUnrecognizedVMOptions == null
39  *              | vm.opt.IgnoreUnrecognizedVMOptions == "false")
40  * @modules java.base/jdk.internal.misc
41  *          java.management
42  * @run main gc.arguments.TestSurvivorAlignmentInBytesOption
43  */
44 public class TestSurvivorAlignmentInBytesOption {
main(String args[])45     public static void main(String args[]) throws Throwable {
46         String optionName = "SurvivorAlignmentInBytes";
47         String unlockExperimentalVMOpts = "UnlockExperimentalVMOptions";
48         String optionIsExperimental
49                 = CommandLineOptionTest.getExperimentalOptionErrorMessage(
50                 optionName);
51         String valueIsTooSmall= ".*SurvivorAlignmentInBytes.*must be greater"
52                 + " than or equal to ObjectAlignmentInBytes.*";
53         String mustBePowerOf2 = ".*SurvivorAlignmentInBytes.*must be "
54                 + "power of 2.*";
55 
56         // Verify that without -XX:+UnlockExperimentalVMOptions usage of
57         // SurvivorAlignmentInBytes option will cause JVM startup failure
58         // with the warning message saying that that option is experimental.
59         String shouldFailMessage = String.format("JVM option '%s' is "
60                 + "experimental.%nJVM startup should fail without "
61                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
62         CommandLineOptionTest.verifyJVMStartup(
63                 new String[]{optionIsExperimental}, null,
64                 shouldFailMessage, shouldFailMessage,
65                 ExitCode.FAIL, false,
66                 "-XX:-UnlockExperimentalVMOptions",
67                 CommandLineOptionTest.prepareBooleanFlag(
68                         unlockExperimentalVMOpts, false),
69                 CommandLineOptionTest.prepareNumericFlag(optionName, 64));
70 
71         // Verify that with -XX:+UnlockExperimentalVMOptions passed to JVM
72         // usage of SurvivorAlignmentInBytes option won't cause JVM startup
73         // failure.
74         String shouldPassMessage = String.format("JVM option '%s' is "
75                 + "experimental.%nJVM startup should pass with "
76                 + "-XX:+UnlockExperimentalVMOptions option", optionName);
77         String noWarningMessage = "There should be no warnings when use "
78                 + "with -XX:+UnlockExperimentalVMOptions option";
79         CommandLineOptionTest.verifyJVMStartup(
80                 null, new String[]{optionIsExperimental},
81                 shouldPassMessage, noWarningMessage,
82                 ExitCode.OK, false,
83                 CommandLineOptionTest.prepareBooleanFlag(
84                         unlockExperimentalVMOpts, true),
85                 CommandLineOptionTest.prepareNumericFlag(optionName, 64));
86 
87         // Verify that if specified SurvivorAlignmentInBytes is lower than
88         // ObjectAlignmentInBytes, then the JVM startup will fail with
89         // appropriate error message.
90         if (Platform.is64bit()) {
91             shouldFailMessage = String.format("JVM startup should fail with "
92                     + "'%s' option value lower than ObjectAlignmentInBytes", optionName);
93             CommandLineOptionTest.verifyJVMStartup(
94                     new String[]{valueIsTooSmall}, null,
95                     shouldFailMessage, shouldFailMessage,
96                     ExitCode.FAIL, false,
97                     CommandLineOptionTest.prepareBooleanFlag(
98                             unlockExperimentalVMOpts, true),
99                     CommandLineOptionTest.prepareNumericFlag(optionName, 8),
100                     CommandLineOptionTest.prepareNumericFlag("ObjectAlignmentInBytes", 16));
101         }
102 
103         // Verify that if specified SurvivorAlignmentInBytes value is not
104         // a power of 2 then the JVM startup will fail with appropriate error
105         // message.
106         shouldFailMessage = String.format("JVM startup should fail with "
107                 + "'%s' option value is not a power of 2", optionName);
108         CommandLineOptionTest.verifyJVMStartup(
109                 new String[]{mustBePowerOf2}, null,
110                 shouldFailMessage, shouldFailMessage,
111                 ExitCode.FAIL, false,
112                 CommandLineOptionTest.prepareBooleanFlag(
113                         unlockExperimentalVMOpts, true),
114                 CommandLineOptionTest.prepareNumericFlag(optionName, 127));
115 
116         // Verify that if SurvivorAlignmentInBytes has correct value, then
117         // the JVM will be started without errors.
118         shouldPassMessage = String.format("JVM startup should pass with "
119                 + "correct '%s' option value", optionName);
120         noWarningMessage = String.format("There should be no warnings when use "
121                 + "correct '%s' option value", optionName);
122         CommandLineOptionTest.verifyJVMStartup(
123                 null, new String[]{".*SurvivorAlignmentInBytes.*"},
124                 shouldPassMessage, noWarningMessage,
125                 ExitCode.OK, false,
126                 CommandLineOptionTest.prepareBooleanFlag(
127                         unlockExperimentalVMOpts, true),
128                 CommandLineOptionTest.prepareNumericFlag(optionName, 128));
129 
130         // Verify that we can setup different SurvivorAlignmentInBytes values.
131         for (int alignment = 32; alignment <= 128; alignment *= 2) {
132             shouldPassMessage = String.format("JVM startup should pass with "
133                     + "'%s' = %d", optionName, alignment);
134             CommandLineOptionTest.verifyOptionValue(optionName,
135                     Integer.toString(alignment), shouldPassMessage,
136                     CommandLineOptionTest.prepareBooleanFlag(
137                             unlockExperimentalVMOpts, true),
138                     CommandLineOptionTest.prepareNumericFlag(
139                             optionName, alignment));
140         }
141     }
142 }
143