Path: blob/master/test/hotspot/jtreg/gc/g1/TestLargePageUseForAuxMemory.java
40942 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.g1;2425/*26* @test TestLargePageUseForAuxMemory.java27* @summary Test that auxiliary data structures are allocated using large pages if available.28* @bug 8058354 807920829* @modules java.base/jdk.internal.misc30* @library /test/lib31* @requires vm.gc.G132* @requires vm.opt.LargePageSizeInBytes == null33* @build sun.hotspot.WhiteBox34* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox35* @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+IgnoreUnrecognizedVMOptions -XX:+UseLargePages gc.g1.TestLargePageUseForAuxMemory36*/3738import java.lang.Math;39import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.process.ProcessTools;41import jdk.test.lib.Asserts;42import jdk.test.lib.Platform;43import jtreg.SkippedException;44import sun.hotspot.WhiteBox;4546public class TestLargePageUseForAuxMemory {47static final long HEAP_REGION_SIZE = 1 * 1024 * 1024;48static long largePageSize;49static long smallPageSize;50static long allocGranularity;5152static boolean largePagesEnabled(OutputAnalyzer output) {53// The gc+init logging includes information about large pages.54String lp = output.firstMatch("Large Page Support: (\\w*)", 1);55return lp != null && lp.equals("Enabled");56}5758static boolean largePagesAllocationFailure(OutputAnalyzer output, String pattern) {59// Check if there is a large page failure associated with the data structure60// being checked. In case of a large page allocation failure the output will61// include logs like this for the affected data structure:62// [0.048s][debug][gc,heap,coops] Reserve regular memory without large pages63// [0.048s][info ][pagesize ] Next Bitmap: ... page_size=4K ...64//65// The pattern passed in should match the second line.66String failureMatch = output.firstMatch("Reserve regular memory without large pages\\n.*" + pattern, 1);67if (failureMatch != null) {68return true;69}70return false;71}7273static void checkSize(OutputAnalyzer output, long expectedSize, String pattern) {74// First check the output for any large page allocation failure associated with75// the checked data structure. If we detect a failure then expect small pages.76if (largePagesAllocationFailure(output, pattern)) {77// This should only happen when we are expecting large pages78if (expectedSize == smallPageSize) {79throw new RuntimeException("Expected small page size when large page failure was detected");80}81expectedSize = smallPageSize;82}8384// Now check what page size is traced.85String pageSizeStr = output.firstMatch(pattern, 1);86if (pageSizeStr == null) {87output.reportDiagnosticSummary();88throw new RuntimeException("Match from '" + pattern + "' got 'null' expected: " + expectedSize);89}9091long size = parseMemoryString(pageSizeStr);92if (size != expectedSize) {93output.reportDiagnosticSummary();94throw new RuntimeException("Match from '" + pattern + "' got " + size + " expected: " + expectedSize);95}96}9798static void checkSmallTables(OutputAnalyzer output, long expectedPageSize) throws Exception {99checkSize(output, expectedPageSize, "Block Offset Table: .*page_size=([^ ]+)");100checkSize(output, expectedPageSize, "Card Counts Table: .*page_size=([^ ]+)");101}102103static void checkBitmaps(OutputAnalyzer output, long expectedPageSize) throws Exception {104checkSize(output, expectedPageSize, "Prev Bitmap: .*page_size=([^ ]+)");105checkSize(output, expectedPageSize, "Next Bitmap: .*page_size=([^ ]+)");106}107108static void testVM(String what, long heapsize, boolean cardsShouldUseLargePages, boolean bitmapShouldUseLargePages) throws Exception {109System.out.println(what + " heapsize " + heapsize + " card table should use large pages " + cardsShouldUseLargePages + " " +110"bitmaps should use large pages " + bitmapShouldUseLargePages);111ProcessBuilder pb;112// Test with large page enabled.113pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",114"-XX:G1HeapRegionSize=" + HEAP_REGION_SIZE,115"-Xmx" + heapsize,116"-Xlog:pagesize,gc+init,gc+heap+coops=debug",117"-XX:+UseLargePages",118"-XX:+IgnoreUnrecognizedVMOptions", // there is no ObjectAlignmentInBytes in 32 bit builds119"-XX:ObjectAlignmentInBytes=8",120"-version");121122OutputAnalyzer output = new OutputAnalyzer(pb.start());123// Only expect large page size if large pages are enabled.124if (largePagesEnabled(output)) {125checkSmallTables(output, (cardsShouldUseLargePages ? largePageSize : smallPageSize));126checkBitmaps(output, (bitmapShouldUseLargePages ? largePageSize : smallPageSize));127} else {128checkSmallTables(output, smallPageSize);129checkBitmaps(output, smallPageSize);130}131output.shouldHaveExitValue(0);132133// Test with large page disabled.134pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",135"-XX:G1HeapRegionSize=" + HEAP_REGION_SIZE,136"-Xmx" + heapsize,137"-Xlog:pagesize",138"-XX:-UseLargePages",139"-XX:+IgnoreUnrecognizedVMOptions", // there is no ObjectAlignmentInBytes in 32 bit builds140"-XX:ObjectAlignmentInBytes=8",141"-version");142143output = new OutputAnalyzer(pb.start());144checkSmallTables(output, smallPageSize);145checkBitmaps(output, smallPageSize);146output.shouldHaveExitValue(0);147}148149private static long gcd(long x, long y) {150while (x > 0) {151long t = x;152x = y % x;153y = t;154}155return y;156}157158private static long lcm(long x, long y) {159return x * (y / gcd(x, y));160}161162public static void main(String[] args) throws Exception {163// Size that a single card covers.164final int cardSize = 512;165WhiteBox wb = WhiteBox.getWhiteBox();166smallPageSize = wb.getVMPageSize();167largePageSize = wb.getVMLargePageSize();168allocGranularity = wb.getVMAllocationGranularity();169final long heapAlignment = lcm(cardSize * smallPageSize, largePageSize);170171if (largePageSize == 0) {172throw new SkippedException("Large page support does not seem to be available on this platform.");173}174if (largePageSize == smallPageSize) {175throw new SkippedException("Large page support does not seem to be available on this platform."176+ "Small and large page size are the same.");177}178179// To get large pages for the card table etc. we need at least a 1G heap (with 4k page size).180// 32 bit systems will have problems reserving such an amount of contiguous space, so skip the181// test there.182if (!Platform.is32bit()) {183final long heapSizeForCardTableUsingLargePages = largePageSize * cardSize;184final long heapSizeDiffForCardTable = Math.max(Math.max(allocGranularity * cardSize, HEAP_REGION_SIZE), largePageSize);185186Asserts.assertGT(heapSizeForCardTableUsingLargePages, heapSizeDiffForCardTable,187"To test we would require to use an invalid heap size");188testVM("case1: card table and bitmap use large pages (barely)", heapSizeForCardTableUsingLargePages, true, true);189testVM("case2: card table and bitmap use large pages (extra slack)", heapSizeForCardTableUsingLargePages + heapSizeDiffForCardTable, true, true);190testVM("case3: only bitmap uses large pages (barely not)", heapSizeForCardTableUsingLargePages - heapSizeDiffForCardTable, false, true);191}192193// Minimum heap requirement to get large pages for bitmaps is 128M heap. This seems okay to test194// everywhere.195final int bitmapTranslationFactor = 8 * 8; // ObjectAlignmentInBytes * BitsPerByte196final long heapSizeForBitmapUsingLargePages = largePageSize * bitmapTranslationFactor;197final long heapSizeDiffForBitmap = Math.max(Math.max(allocGranularity * bitmapTranslationFactor, HEAP_REGION_SIZE),198Math.max(largePageSize, heapAlignment));199200Asserts.assertGT(heapSizeForBitmapUsingLargePages, heapSizeDiffForBitmap,201"To test we would require to use an invalid heap size");202203testVM("case4: only bitmap uses large pages (barely)", heapSizeForBitmapUsingLargePages, false, true);204testVM("case5: only bitmap uses large pages (extra slack)", heapSizeForBitmapUsingLargePages + heapSizeDiffForBitmap, false, true);205testVM("case6: nothing uses large pages (barely not)", heapSizeForBitmapUsingLargePages - heapSizeDiffForBitmap, false, false);206}207208public static long parseMemoryString(String value) {209long multiplier = 1;210211if (value.endsWith("B")) {212multiplier = 1;213} else if (value.endsWith("K")) {214multiplier = 1024;215} else if (value.endsWith("M")) {216multiplier = 1024 * 1024;217} else if (value.endsWith("G")) {218multiplier = 1024 * 1024 * 1024;219} else {220throw new IllegalArgumentException("Expected memory string '" + value + "'to end with either of: B, K, M, G");221}222223long longValue = Long.parseUnsignedLong(value.substring(0, value.length() - 1));224225return longValue * multiplier;226}227}228229230