Path: blob/master/test/hotspot/jtreg/gc/g1/TestLargePageUseForHeap.java
40942 views
/*1* Copyright (c) 2019, 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 TestLargePageUseForHeap.java27* @summary Test that Java heap is allocated using large pages of the appropriate size if available.28* @bug 822151729* @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:+WhiteBoxAPI36-XX:+IgnoreUnrecognizedVMOptions -XX:+UseLargePages gc.g1.TestLargePageUseForHeap37*/3839import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.process.ProcessTools;41import jtreg.SkippedException;42import sun.hotspot.WhiteBox;4344public class TestLargePageUseForHeap {45static long largePageSize;46static long smallPageSize;4748static void checkSize(OutputAnalyzer output, long expectedSize, String pattern) {49String pageSizeStr = output.firstMatch(pattern, 1);5051if (pageSizeStr == null) {52output.reportDiagnosticSummary();53throw new RuntimeException("Match from '" + pattern + "' got 'null' expected: " + expectedSize);54}5556long size = parseMemoryString(pageSizeStr);57if (size != expectedSize) {58output.reportDiagnosticSummary();59throw new RuntimeException("Match from '" + pattern + "' got " + size + " expected: " + expectedSize);60}61}6263static boolean checkLargePageEnabled(OutputAnalyzer output) {64String lp = output.firstMatch("Large Page Support: (\\w*)", 1);65// Make sure large pages really are enabled.66if (lp == null || lp.equals("Disabled")) {67return false;68}69// This message is printed when tried to reserve a memory with large page but it failed.70String errorStr = "Reserve regular memory without large pages";71String heapPattern = ".*Heap: ";72// If errorStr is printed just before heap page log, reservation for Java Heap is failed.73String result = output.firstMatch(errorStr + "\n" +74"(?:.*Heap address: .*\n)?" // Heap address: 0x00000000f8000000, size: 128 MB, Compressed Oops mode: 32-bit75+ heapPattern);76if (result != null) {77return false;78}79return true;80}8182static void checkHeap(OutputAnalyzer output, long expectedPageSize) throws Exception {83checkSize(output, expectedPageSize, "Heap: .*page_size=([^ ]+)");84}8586static void testVM(long regionSize) throws Exception {87ProcessBuilder pb;88// Test with large page enabled.89pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",90"-XX:G1HeapRegionSize=" + regionSize,91"-Xmx128m",92"-Xlog:gc+init,pagesize,gc+heap+coops=debug",93"-XX:+UseLargePages",94"-version");9596OutputAnalyzer output = new OutputAnalyzer(pb.start());97boolean largePageEnabled = checkLargePageEnabled(output);98checkHeap(output, largePageEnabled ? largePageSize : smallPageSize);99output.shouldHaveExitValue(0);100101// Test with large page disabled.102pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",103"-XX:G1HeapRegionSize=" + regionSize,104"-Xmx128m",105"-Xlog:gc+init,pagesize,gc+heap+coops=debug",106"-XX:-UseLargePages",107"-version");108109output = new OutputAnalyzer(pb.start());110checkHeap(output, smallPageSize);111output.shouldHaveExitValue(0);112}113114public static void main(String[] args) throws Exception {115WhiteBox wb = WhiteBox.getWhiteBox();116smallPageSize = wb.getVMPageSize();117largePageSize = wb.getVMLargePageSize();118119if (largePageSize == 0) {120throw new SkippedException("Large page support does not seem to be available on this platform.");121}122if (largePageSize == smallPageSize) {123throw new SkippedException("Large page support does not seem to be available on this platform."124+ "Small and large page size are the same.");125}126127// G1HeapRegionSize=1MB128testVM(1 * 1024 * 1024);129130// G1HeapRegionSize=2MB131testVM(2 * 1024 * 1024);132133// G1HeapRegionSize=8MB134testVM(8 * 1024 * 1024);135}136137public static long parseMemoryString(String value) {138long multiplier = 1;139140if (value.endsWith("B")) {141multiplier = 1;142} else if (value.endsWith("K")) {143multiplier = 1024;144} else if (value.endsWith("M")) {145multiplier = 1024 * 1024;146} else if (value.endsWith("G")) {147multiplier = 1024 * 1024 * 1024;148} else {149throw new IllegalArgumentException("Expected memory string '" + value + "'to end with either of: B, K, M, G");150}151152long longValue = Long.parseUnsignedLong(value.substring(0, value.length() - 1));153154return longValue * multiplier;155}156}157158159