Path: blob/master/test/hotspot/jtreg/gc/g1/TestMixedGCLiveThreshold.java
40942 views
/*1* Copyright (c) 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 TestMixedGCLiveThreshold27* @summary Test G1MixedGCLiveThresholdPercent. Fill up a region to at least 1/3 region-size,28* the region should not be selected for mixed GC cycle if liveness is above threshold.29* @requires vm.gc.G130* @library /test/lib31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run driver gc.g1.TestMixedGCLiveThreshold34*/3536import java.util.ArrayList;37import java.util.Collections;38import java.util.regex.Pattern;39import java.util.regex.Matcher;4041import jdk.test.lib.process.OutputAnalyzer;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.Asserts;44import sun.hotspot.WhiteBox;4546public class TestMixedGCLiveThreshold {47private static final String pattern = "Remembered Set Tracking update regions total ([0-9]+), selected ([0-9]+)$";4849public static void main(String[] args) throws Exception {50// -XX:G1MixedGCLiveThresholdPercent=051testMixedGCLiveThresholdPercent(0, false);5253// -XX:G1MixedGCLiveThresholdPercent=2554testMixedGCLiveThresholdPercent(25, false);5556// -XX:G1MixedGCLiveThresholdPercent=10057testMixedGCLiveThresholdPercent(100, true);58}5960private static void testMixedGCLiveThresholdPercent(int liveThresholdPercent, boolean expectedRebuild) throws Exception {61OutputAnalyzer output = testWithMixedGCLiveThresholdPercent(liveThresholdPercent);6263boolean regionsSelected = regionsSelectedForRebuild(output.getStdout());6465Asserts.assertEquals(regionsSelected, expectedRebuild,66(expectedRebuild ?67"No Regions selected for rebuild. G1MixedGCLiveThresholdPercent=" + liveThresholdPercent +68" at least one region should be selected" :69"Regions selected for rebuild. G1MixedGCLiveThresholdPercent=" + liveThresholdPercent +70" no regions should be selected")71);72output.shouldHaveExitValue(0);73}7475private static OutputAnalyzer testWithMixedGCLiveThresholdPercent(int percent) throws Exception {76ArrayList<String> basicOpts = new ArrayList<>();77Collections.addAll(basicOpts, new String[] {78"-Xbootclasspath/a:.",79"-XX:+UseG1GC",80"-XX:+UnlockDiagnosticVMOptions",81"-XX:+UnlockExperimentalVMOptions",82"-XX:+WhiteBoxAPI",83"-Xlog:gc+remset+tracking=trace",84"-Xms10M",85"-Xmx10M"});8687basicOpts.add("-XX:G1MixedGCLiveThresholdPercent=" + percent);8889basicOpts.add(GCTest.class.getName());9091ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(basicOpts);92OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());93return analyzer;94}9596private static boolean regionsSelectedForRebuild(String output) throws Exception {97Matcher m = Pattern.compile(pattern, Pattern.MULTILINE).matcher(output);9899if (!m.find()) {100throw new Exception("Could not find correct output for Remembered Set Tracking in stdout," +101" should match the pattern \"" + pattern + "\", but stdout is \n" + output);102}103return Integer.parseInt(m.group(2)) > 0;104}105106public static class GCTest {107public static void main(String args[]) throws Exception {108WhiteBox wb = WhiteBox.getWhiteBox();109// Allocate some memory less than region size.110Object used = allocate();111112// Trigger the full GC using the WhiteBox API.113wb.fullGC(); // full114115// Memory objects have been promoted to old by full GC.116// Concurrent cycle may select regions for rebuilding117wb.g1StartConcMarkCycle(); // concurrent-start, remark and cleanup118119// Sleep to make sure concurrent cycle is done120while (wb.g1InConcurrentMark()) {121Thread.sleep(1000);122}123System.out.println(used);124}125126private static Object allocate() {127final int objectSize = WhiteBox.getWhiteBox().g1RegionSize() / 3;128Object ret = new byte[objectSize];129return ret;130}131}132}133134135