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 optionsvalidation;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.StringTokenizer;
36 import java.util.function.Predicate;
37 import jdk.test.lib.OutputAnalyzer;
38 import jdk.test.lib.Platform;
39 import jdk.test.lib.ProcessTools;
40
41 public class JVMOptionsUtils {
42
43 /* Java option which print options with ranges */
44 private static final String PRINT_FLAGS_RANGES = "-XX:+PrintFlagsRanges";
45
46 /* StringBuilder to accumulate failed message */
47 private static final StringBuilder finalFailedMessage = new StringBuilder();
48
49 /* Used to start the JVM with the same type as current */
50 static String VMType;
51
52 static {
53 if (Platform.isServer()) {
54 VMType = "-server";
55 } else if (Platform.isClient()) {
56 VMType = "-client";
57 } else if (Platform.isMinimal()) {
58 VMType = "-minimal";
59 } else if (Platform.isGraal()) {
60 VMType = "-graal";
61 } else {
62 VMType = null;
63 }
64 }
65
66 /**
67 * Add dependency for option depending on it's name. E.g. enable G1 GC for
68 * G1 options or add prepend options to not hit constraints.
69 *
70 * @param option option
71 */
72 private static void addNameDependency(JVMOption option) {
73 String name = option.getName();
74
75 if (name.startsWith("G1")) {
76 option.addPrepend("-XX:+UseG1GC");
77 }
78
79 if (name.startsWith("CMS")) {
80 option.addPrepend("-XX:+UseConcMarkSweepGC");
81 }
82
83 switch (name) {
84 case "MinHeapFreeRatio":
85 option.addPrepend("-XX:MaxHeapFreeRatio=100");
86 break;
87 case "MaxHeapFreeRatio":
88 option.addPrepend("-XX:MinHeapFreeRatio=0");
89 break;
90 case "MinMetaspaceFreeRatio":
91 option.addPrepend("-XX:MaxMetaspaceFreeRatio=100");
92 break;
93 case "MaxMetaspaceFreeRatio":
94 option.addPrepend("-XX:MinMetaspaceFreeRatio=0");
95 break;
96 case "CMSOldPLABMin":
97 option.addPrepend("-XX:CMSOldPLABMax=" + option.getMax());
98 break;
99 case "CMSOldPLABMax":
100 option.addPrepend("-XX:CMSOldPLABMin=" + option.getMin());
101 break;
102 case "CMSPrecleanNumerator":
103 option.addPrepend("-XX:CMSPrecleanDenominator=" + option.getMax());
104 break;
105 case "CMSPrecleanDenominator":
106 option.addPrepend("-XX:CMSPrecleanNumerator=" + ((new Integer(option.getMin())) - 1));
107 break;
108 case "InitialTenuringThreshold":
109 option.addPrepend("-XX:MaxTenuringThreshold=" + option.getMax());
110 break;
111 default:
112 /* Do nothing */
113 break;
114 }
115
116 }
117
118 /**
119 * Parse JVM Options. Get input from "inputReader". Parse using
120 * "-XX:+PrintFlagsRanges" output format.
121 *
122 * @param inputReader input data for parsing
123 * @param withRanges true if needed options with defined ranges inside JVM
124 * @param acceptOrigin predicate for option origins. Origins can be
125 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates
126 * to true.
127 * @return map from option name to the JVMOption object
128 * @throws IOException if an error occurred while reading the data
129 */
130 private static Map<String, JVMOption> getJVMOptions(Reader inputReader,
131 boolean withRanges, Predicate<String> acceptOrigin) throws IOException {
132 BufferedReader reader = new BufferedReader(inputReader);
133 String type;
134 String line;
135 String token;
|
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 optionsvalidation;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.math.BigDecimal;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.LinkedHashMap;
35 import java.util.Map;
36 import java.util.StringTokenizer;
37 import java.util.function.Predicate;
38 import jdk.test.lib.OutputAnalyzer;
39 import jdk.test.lib.Platform;
40 import jdk.test.lib.ProcessTools;
41
42 public class JVMOptionsUtils {
43
44 /* Java option which print options with ranges */
45 private static final String PRINT_FLAGS_RANGES = "-XX:+PrintFlagsRanges";
46
47 /* StringBuilder to accumulate failed message */
48 private static final StringBuilder finalFailedMessage = new StringBuilder();
49
50 /* Used to start the JVM with the same type as current */
51 static String VMType;
52
53 private static Map<String, JVMOption> optionsAsMap;
54
55 static {
56 if (Platform.isServer()) {
57 VMType = "-server";
58 } else if (Platform.isClient()) {
59 VMType = "-client";
60 } else if (Platform.isMinimal()) {
61 VMType = "-minimal";
62 } else if (Platform.isGraal()) {
63 VMType = "-graal";
64 } else {
65 VMType = null;
66 }
67 }
68
69 public static boolean fitsRange(String optionName, BigDecimal number) throws Exception {
70 JVMOption option;
71 String minRangeString = null;
72 String maxRangeString = null;
73 boolean fits = true;
74
75 if (optionsAsMap == null) {
76 optionsAsMap = getOptionsWithRangeAsMap();
77 }
78
79 option = optionsAsMap.get(optionName);
80 if (option != null) {
81 minRangeString = option.getMin();
82 if (minRangeString != null) {
83 fits = (number.compareTo(new BigDecimal(minRangeString)) >= 0);
84 }
85 maxRangeString = option.getMax();
86 if (maxRangeString != null) {
87 fits &= (number.compareTo(new BigDecimal(maxRangeString)) <= 0);
88 }
89 }
90
91 return fits;
92 }
93
94 public static boolean fitsRange(String optionName, String number) throws Exception {
95 String lowerCase = number.toLowerCase();
96 String multiplier = "1";
97 if (lowerCase.endsWith("k")) {
98 multiplier = "1024";
99 lowerCase = lowerCase.substring(0, lowerCase.length()-1);
100 } else if (lowerCase.endsWith("m")) {
101 multiplier = "1048576"; //1024*1024
102 lowerCase = lowerCase.substring(0, lowerCase.length()-1);
103 } else if (lowerCase.endsWith("g")) {
104 multiplier = "1073741824"; //1024*1024*1024
105 lowerCase = lowerCase.substring(0, lowerCase.length()-1);
106 } else if (lowerCase.endsWith("t")) {
107 multiplier = "1099511627776"; //1024*1024*1024*1024
108 lowerCase = lowerCase.substring(0, lowerCase.length()-1);
109 }
110 BigDecimal valueBig = new BigDecimal(lowerCase);
111 BigDecimal multiplierBig = new BigDecimal(multiplier);
112 return fitsRange(optionName, valueBig.multiply(multiplierBig));
113 }
114
115 public static String getMinOptionRange(String optionName) throws Exception {
116 JVMOption option;
117 String minRange = null;
118
119 if (optionsAsMap == null) {
120 optionsAsMap = getOptionsWithRangeAsMap();
121 }
122
123 option = optionsAsMap.get(optionName);
124 if (option != null) {
125 minRange = option.getMin();
126 }
127
128 return minRange;
129 }
130
131 public static String getMaxOptionRange(String optionName) throws Exception {
132 JVMOption option;
133 String maxRange = null;
134
135 if (optionsAsMap == null) {
136 optionsAsMap = getOptionsWithRangeAsMap();
137 }
138
139 option = optionsAsMap.get(optionName);
140 if (option != null) {
141 maxRange = option.getMax();
142 }
143
144 return maxRange;
145 }
146
147 /**
148 * Add dependency for option depending on it's name. E.g. enable G1 GC for
149 * G1 options or add prepend options to not hit constraints.
150 *
151 * @param option option
152 */
153 private static void addNameDependency(JVMOption option) {
154 String name = option.getName();
155
156 if (name.startsWith("G1")) {
157 option.addPrepend("-XX:+UseG1GC");
158 }
159
160 if (name.startsWith("CMS")) {
161 option.addPrepend("-XX:+UseConcMarkSweepGC");
162 }
163
164 if (name.startsWith("Shared")) {
165 option.addPrepend("-XX:+UnlockDiagnosticVMOptions");
166 String fileName = "Test" + name + ".jsa";
167 option.addPrepend("-XX:SharedArchiveFile=" + fileName);
168 option.addPrepend("-Xshare:dump");
169 }
170
171 switch (name) {
172 case "MinHeapFreeRatio":
173 option.addPrepend("-XX:MaxHeapFreeRatio=100");
174 break;
175 case "MaxHeapFreeRatio":
176 option.addPrepend("-XX:MinHeapFreeRatio=0");
177 break;
178 case "MinMetaspaceFreeRatio":
179 option.addPrepend("-XX:MaxMetaspaceFreeRatio=100");
180 break;
181 case "MaxMetaspaceFreeRatio":
182 option.addPrepend("-XX:MinMetaspaceFreeRatio=0");
183 break;
184 case "CMSOldPLABMin":
185 option.addPrepend("-XX:CMSOldPLABMax=" + option.getMax());
186 break;
187 case "CMSOldPLABMax":
188 option.addPrepend("-XX:CMSOldPLABMin=" + option.getMin());
189 break;
190 case "CMSPrecleanNumerator":
191 option.addPrepend("-XX:CMSPrecleanDenominator=" + option.getMax());
192 break;
193 case "CMSPrecleanDenominator":
194 option.addPrepend("-XX:CMSPrecleanNumerator=" + ((new Integer(option.getMin())) - 1));
195 break;
196 case "InitialTenuringThreshold":
197 option.addPrepend("-XX:MaxTenuringThreshold=" + option.getMax());
198 break;
199 default:
200 /* Do nothing */
201 break;
202 }
203 }
204
205 /**
206 * Parse JVM Options. Get input from "inputReader". Parse using
207 * "-XX:+PrintFlagsRanges" output format.
208 *
209 * @param inputReader input data for parsing
210 * @param withRanges true if needed options with defined ranges inside JVM
211 * @param acceptOrigin predicate for option origins. Origins can be
212 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates
213 * to true.
214 * @return map from option name to the JVMOption object
215 * @throws IOException if an error occurred while reading the data
216 */
217 private static Map<String, JVMOption> getJVMOptions(Reader inputReader,
218 boolean withRanges, Predicate<String> acceptOrigin) throws IOException {
219 BufferedReader reader = new BufferedReader(inputReader);
220 String type;
221 String line;
222 String token;
|