1 /*
   2  * Copyright (c) 2015, 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 TestLargePageUseForAuxMemory.java
  26  * @summary Test that auxiliary data structures are allocated using large pages if available.
  27  * @bug 8058354 8079208
  28  * @key gc
  29  * @library /testlibrary /test/lib
  30  * @requires (vm.gc=="G1" | vm.gc=="null")
  31  * @build jdk.test.lib.* sun.hotspot.WhiteBox
  32  * @build TestLargePageUseForAuxMemory
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+IgnoreUnrecognizedVMOptions -XX:+UseLargePages TestLargePageUseForAuxMemory
  36  */
  37 
  38 import java.lang.Math;
  39 
  40 import jdk.test.lib.*;
  41 import jdk.test.lib.Asserts;
  42 import sun.hotspot.WhiteBox;
  43 
  44 public class TestLargePageUseForAuxMemory {
  45     static final long HEAP_REGION_SIZE = 1 * 1024 * 1024;
  46     static long largePageSize;
  47     static long smallPageSize;
  48     static long allocGranularity;
  49 
  50     static void checkSmallTables(OutputAnalyzer output, long expectedPageSize) throws Exception {
  51         output.shouldContain("G1 'Block offset table': pg_sz=" + expectedPageSize);
  52         output.shouldContain("G1 'Card counts table': pg_sz=" + expectedPageSize);
  53     }
  54 
  55     static void checkBitmaps(OutputAnalyzer output, long expectedPageSize) throws Exception {
  56         output.shouldContain("G1 'Prev Bitmap': pg_sz=" + expectedPageSize);
  57         output.shouldContain("G1 'Next Bitmap': pg_sz=" + expectedPageSize);
  58     }
  59 
  60     static void testVM(String what, long heapsize, boolean cardsShouldUseLargePages, boolean bitmapShouldUseLargePages) throws Exception {
  61         System.out.println(what + " heapsize " + heapsize + " card table should use large pages " + cardsShouldUseLargePages + " " +
  62                            "bitmaps should use large pages " + bitmapShouldUseLargePages);
  63         ProcessBuilder pb;
  64         // Test with large page enabled.
  65         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
  66                                                    "-XX:G1HeapRegionSize=" + HEAP_REGION_SIZE,
  67                                                    "-Xms" + heapsize,
  68                                                    "-Xmx" + heapsize,
  69                                                    "-XX:+TracePageSizes",
  70                                                    "-XX:+UseLargePages",
  71                                                    "-XX:+IgnoreUnrecognizedVMOptions",  // there is no ObjectAlignmentInBytes in 32 bit builds
  72                                                    "-XX:ObjectAlignmentInBytes=8",
  73                                                    "-version");
  74 
  75         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  76         checkSmallTables(output, (cardsShouldUseLargePages ? largePageSize : smallPageSize));
  77         checkBitmaps(output, (bitmapShouldUseLargePages ? largePageSize : smallPageSize));
  78         output.shouldHaveExitValue(0);
  79 
  80         // Test with large page disabled.
  81         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
  82                                                    "-XX:G1HeapRegionSize=" + HEAP_REGION_SIZE,
  83                                                    "-Xms" + heapsize,
  84                                                    "-Xmx" + heapsize,
  85                                                    "-XX:+TracePageSizes",
  86                                                    "-XX:-UseLargePages",
  87                                                    "-XX:+IgnoreUnrecognizedVMOptions",  // there is no ObjectAlignmentInBytes in 32 bit builds
  88                                                    "-XX:ObjectAlignmentInBytes=8",
  89                                                    "-version");
  90 
  91         output = new OutputAnalyzer(pb.start());
  92         checkSmallTables(output, smallPageSize);
  93         checkBitmaps(output, smallPageSize);
  94         output.shouldHaveExitValue(0);
  95     }
  96 
  97     private static long gcd(long x, long y) {
  98         while (x > 0) {
  99             long t = x;
 100             x = y % x;
 101             y = t;
 102         }
 103         return y;
 104     }
 105 
 106     private static long lcm(long x, long y) {
 107         return x * (y / gcd(x, y));
 108     }
 109 
 110     public static void main(String[] args) throws Exception {
 111         if (!Platform.isDebugBuild()) {
 112             System.out.println("Skip tests on non-debug builds because the required option TracePageSizes is a debug-only option.");
 113             return;
 114         }
 115 
 116         // Size that a single card covers.
 117         final int cardSize = 512;
 118         WhiteBox wb = WhiteBox.getWhiteBox();
 119         smallPageSize = wb.getVMPageSize();
 120         largePageSize = wb.getVMLargePageSize();
 121         allocGranularity = wb.getVMAllocationGranularity();
 122         final long heapAlignment = lcm(cardSize * smallPageSize, largePageSize);
 123 
 124         if (largePageSize == 0) {
 125             System.out.println("Skip tests because large page support does not seem to be available on this platform.");
 126             return;
 127         }
 128         if (largePageSize == smallPageSize) {
 129             System.out.println("Skip tests because large page support does not seem to be available on this platform." +
 130                                "Small and large page size are the same.");
 131             return;
 132         }
 133 
 134         // To get large pages for the card table etc. we need at least a 1G heap (with 4k page size).
 135         // 32 bit systems will have problems reserving such an amount of contiguous space, so skip the
 136         // test there.
 137         if (!Platform.is32bit()) {
 138             final long heapSizeForCardTableUsingLargePages = largePageSize * cardSize;
 139             final long heapSizeDiffForCardTable = Math.max(Math.max(allocGranularity * cardSize, HEAP_REGION_SIZE), largePageSize);
 140 
 141             Asserts.assertGT(heapSizeForCardTableUsingLargePages, heapSizeDiffForCardTable,
 142                              "To test we would require to use an invalid heap size");
 143             testVM("case1: card table and bitmap use large pages (barely)", heapSizeForCardTableUsingLargePages, true, true);
 144             testVM("case2: card table and bitmap use large pages (extra slack)", heapSizeForCardTableUsingLargePages + heapSizeDiffForCardTable, true, true);
 145             testVM("case3: only bitmap uses large pages (barely not)", heapSizeForCardTableUsingLargePages - heapSizeDiffForCardTable, false, true);
 146         }
 147 
 148         // Minimum heap requirement to get large pages for bitmaps is 128M heap. This seems okay to test
 149         // everywhere.
 150         final int bitmapTranslationFactor = 8 * 8; // ObjectAlignmentInBytes * BitsPerByte
 151         final long heapSizeForBitmapUsingLargePages = largePageSize * bitmapTranslationFactor;
 152         final long heapSizeDiffForBitmap = Math.max(Math.max(allocGranularity * bitmapTranslationFactor, HEAP_REGION_SIZE),
 153                                                     Math.max(largePageSize, heapAlignment));
 154 
 155         Asserts.assertGT(heapSizeForBitmapUsingLargePages, heapSizeDiffForBitmap,
 156                          "To test we would require to use an invalid heap size");
 157 
 158         testVM("case4: only bitmap uses large pages (barely)", heapSizeForBitmapUsingLargePages, false, true);
 159         testVM("case5: only bitmap uses large pages (extra slack)", heapSizeForBitmapUsingLargePages + heapSizeDiffForBitmap, false, true);
 160         testVM("case6: nothing uses large pages (barely not)", heapSizeForBitmapUsingLargePages - heapSizeDiffForBitmap, false, false);
 161     }
 162 }