1 /*
2  * Copyright (c) 2016, 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 TestSmallInitialHeapWithLargePageAndNUMA
28  * @bug 8023905
29  * @requires os.family == "linux"
30  * @requires vm.gc.Parallel
31  * @summary Check large pages and NUMA are working together via the output message.
32  * @library /test/lib
33  * @library /
34  * @modules java.base/jdk.internal.misc
35  * @modules java.management/sun.management
36  * @build TestSmallInitialHeapWithLargePageAndNUMA
37  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
38  * @run main/othervm -Xbootclasspath/a:. -XX:+UseHugeTLBFS -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.arguments.TestSmallInitialHeapWithLargePageAndNUMA
39 */
40 
41 import jdk.test.lib.process.ProcessTools;
42 import jdk.test.lib.process.OutputAnalyzer;
43 import sun.hotspot.WhiteBox;
44 
45 public class TestSmallInitialHeapWithLargePageAndNUMA {
46 
47   private static final String MSG_EXIT_TOO_SMALL_HEAP = "Failed initializing NUMA with large pages. Too small heap size";
48   private static final String MSG_GC_TRIGGERED_BEFORE_INIT = "GC triggered before VM initialization completed.";
49 
main(String[] args)50   public static void main(String[] args) throws Exception {
51 
52     WhiteBox wb = WhiteBox.getWhiteBox();
53     long heapAlignment = wb.getHeapAlignment();
54 
55     // When using large pages, Linux does not support freeing parts of reserved and committed memory.
56     // And current Linux implementation uses page size as a condition to actually freeing memory.
57     // If we allocate pages less than NUMA node, NUMA will try to use default page size and
58     // this will free the memory which Linux does not support.
59     // Assume the minimum NUMA node as 2.
60     long initHeap = heapAlignment;
61     long maxHeap = heapAlignment * 2;
62 
63     String[] vmArgs = {"-XX:+UseParallelGC",
64                        "-Xms" + String.valueOf(initHeap),
65                        "-Xmx" + String.valueOf(maxHeap),
66                        "-XX:+UseNUMA",
67                        "-XX:+UseHugeTLBFS",
68                        "-XX:+PrintFlagsFinal",
69                        "-version"};
70 
71     ProcessBuilder pb_enabled = GCArguments.createJavaProcessBuilder(vmArgs);
72     OutputAnalyzer analyzer = new OutputAnalyzer(pb_enabled.start());
73 
74     if (largePageOrNumaEnabled(analyzer)) {
75       // We reach here, if both NUMA and HugeTLB are supported.
76       // However final flags will not be printed as NUMA initialization will be failed.
77       checkAnalyzerValues(analyzer, 1, MSG_EXIT_TOO_SMALL_HEAP);
78     }
79   }
80 
81   // If both NUMA and large pages are enabled, VM will exit during NUMA initialization
82   // under the small heap configuration. So final flags will not be printed.
largePageOrNumaEnabled(OutputAnalyzer analyzer)83   private static boolean largePageOrNumaEnabled(OutputAnalyzer analyzer) {
84     String output = analyzer.getOutput();
85 
86     return !output.contains("[Global flags]");
87   }
88 
89   // We need to test with small heap but fastdebug binary fails to initialize because of the small heap.
90   // So skip that case.
checkAnalyzerValues(OutputAnalyzer analyzer, int expectedExitValue, String expectedMessage)91   private static void checkAnalyzerValues(OutputAnalyzer analyzer, int expectedExitValue, String expectedMessage) {
92     String output = analyzer.getOutput();
93 
94     // If the VM exits because of the small heap, skip checking the exit value.
95     if (!output.contains(MSG_GC_TRIGGERED_BEFORE_INIT)) {
96       analyzer.shouldHaveExitValue(expectedExitValue);
97     }
98     if (expectedMessage != null) {
99       analyzer.shouldContain(expectedMessage);
100     }
101   }
102 }
103