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