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