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