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