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 /*
27  * @test TestArrayAllocatorMallocLimit
28  * @summary Sanity check that the ArrayAllocatorMallocLimit flag can be set.
29  * The test helps verifying that size_t flags can be set/read.
30  * @bug 8054823
31  * @key gc
32  * @library /test/lib
33  * @library /
34  * @modules java.base/jdk.internal.misc
35  *          java.management
36  * @run driver gc.arguments.TestArrayAllocatorMallocLimit
37  */
38 
39 import jdk.test.lib.Asserts;
40 import jdk.test.lib.process.OutputAnalyzer;
41 import jdk.test.lib.process.ProcessTools;
42 import java.math.BigInteger;
43 
44 public class TestArrayAllocatorMallocLimit {
main(String [] args)45   public static void main(String [] args) throws Exception {
46     testDefaultValue();
47     testSetValue();
48   }
49 
50   private static final String flagName = "ArrayAllocatorMallocLimit";
51 
52   //     size_t ArrayAllocatorMallocLimit                 = 18446744073709551615{experimental}
53   private static final String printFlagsFinalPattern = " *size_t *" + flagName + " *:?= *(\\d+) *\\{experimental\\} *";
54 
testDefaultValue()55   public static void testDefaultValue()  throws Exception {
56     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(
57       "-XX:+UnlockExperimentalVMOptions", "-XX:+PrintFlagsFinal", "-version");
58 
59     OutputAnalyzer output = new OutputAnalyzer(pb.start());
60     String value = output.firstMatch(printFlagsFinalPattern, 1);
61 
62     try {
63       Asserts.assertNotNull(value, "Couldn't find size_t flag " + flagName);
64 
65       // A size_t is not always parseable with Long.parseValue,
66       // use BigInteger instead.
67       BigInteger biValue = new BigInteger(value);
68 
69       // Sanity check that we got a non-zero value.
70       Asserts.assertNotEquals(biValue, "0");
71 
72       output.shouldHaveExitValue(0);
73     } catch (Exception e) {
74       System.err.println(output.getOutput());
75       throw e;
76     }
77   }
78 
testSetValue()79   public static void testSetValue() throws Exception {
80     long flagValue = 2048;
81 
82     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(
83       "-XX:+UnlockExperimentalVMOptions", "-XX:" + flagName + "=" + flagValue, "-XX:+PrintFlagsFinal", "-version");
84 
85     OutputAnalyzer output = new OutputAnalyzer(pb.start());
86     String value = output.firstMatch(printFlagsFinalPattern, 1);
87 
88     try {
89       Asserts.assertNotNull("Couldn't find size_t flag " + flagName);
90 
91       long longValue = Long.parseLong(value);
92 
93       Asserts.assertEquals(longValue, flagValue);
94 
95       output.shouldHaveExitValue(0);
96     } catch (Exception e) {
97       System.err.println(output.getOutput());
98       throw e;
99     }
100   }
101 
102 }
103