1 /*
2  * Copyright (c) 2015, 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.g1;
25 
26 /*
27  * @test TestPLABSizeBounds
28  * @bug 8134857
29  * @summary Regression test to ensure that G1 supports PLAB sizes of half a region size.
30  * @requires vm.gc.G1
31  * @key gc
32  * @library /test/lib
33  * @modules java.base/jdk.internal.misc
34  *          java.management
35  * @run main gc.g1.TestPLABSizeBounds
36  */
37 
38 import java.util.ArrayList;
39 
40 import jdk.test.lib.Platform;
41 import jdk.test.lib.process.OutputAnalyzer;
42 import jdk.test.lib.process.ProcessTools;
43 
44 public class TestPLABSizeBounds {
45 
46     public static final int M = 1024 * 1024;
47 
48     /**
49      * Starts the VM with the given region size and the given PLAB size arguments. The VM start should
50      * succeed if shouldSucceed is true, otherwise it should fail.
51      *
52      * @param regionSize The region size the VM should be started with in bytes.
53      * @param plabSize The young and old gen PLAB sizes the VM should be started with in machine words.
54      * @param shouldSucceed The expected result of the VM invocation.
55      */
runTest(int regionSize, int plabSize, boolean shouldSucceed)56     public static void runTest(int regionSize, int plabSize, boolean shouldSucceed) throws Exception {
57         ArrayList<String> testArguments = new ArrayList<String>();
58 
59         testArguments.add("-XX:+UseG1GC");
60         testArguments.add("-Xmx256M");
61         testArguments.add("-XX:G1HeapRegionSize=" + regionSize);
62         testArguments.add("-XX:YoungPLABSize=" + plabSize);
63         testArguments.add("-XX:OldPLABSize=" + plabSize);
64         testArguments.add(GCTest.class.getName());
65 
66         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(testArguments.toArray(new String[0]));
67         OutputAnalyzer output = new OutputAnalyzer(pb.start());
68 
69         if (shouldSucceed) {
70             output.shouldHaveExitValue(0);
71         } else {
72             output.shouldHaveExitValue(1);
73         }
74     }
75 
runRegionTest(int regionSize)76     public static void runRegionTest(int regionSize) throws Exception {
77         final int regionSizeInBytes = regionSize * M;
78         final int wordSize = Platform.is32bit() ? 4 : 8;
79 
80         runTest(regionSizeInBytes, (regionSizeInBytes / wordSize) / 2 - 1, true);
81         runTest(regionSizeInBytes, (regionSizeInBytes / wordSize) / 2, true);
82         runTest(regionSizeInBytes, (regionSizeInBytes / wordSize) / 2 + 1, false);
83     }
84 
main(String[] args)85     public static void main(String[] args) throws Exception {
86         runRegionTest(1);
87         runRegionTest(2);
88         runRegionTest(4);
89         runRegionTest(8);
90         runRegionTest(16);
91         runRegionTest(32);
92     }
93 
94     static class GCTest {
main(String [] args)95         public static void main(String [] args) {
96             System.out.println("Test completed.");
97         }
98     }
99 }
100 
101